facebookarchive / network-connection-class

Listen to current network traffic in the app and categorize the quality of the network.
Other
3.19k stars 515 forks source link

startSampling stopSampling for every network request? #28

Open ralph-bergmann opened 7 years ago

ralph-bergmann commented 7 years ago

Currently I develop a movie app which loads a lot of cover images. Especially in the overview where I show the cover images in a grid and the app has to load lots of images in parallel.

Should I start / stop sampling for every image request or can I start sampling with app start and stop it when the app goes to background?

Currently I have a NetworkInterceptor for OkHttp to do start / stop but I'm not sure if it is a good idea :-(

private static class DeviceBandwidthSamplerInterceptor implements Interceptor {

    private final DeviceBandwidthSampler deviceBandwidthSampler;

    DeviceBandwidthSamplerInterceptor(final DeviceBandwidthSampler deviceBandwidthSampler) {
        this.deviceBandwidthSampler = deviceBandwidthSampler;
    }

    @Override
    public Response intercept(final Chain chain) throws IOException {
        deviceBandwidthSampler.startSampling();
        final Response response = chain.proceed(chain.request());
        deviceBandwidthSampler.stopSampling();
        return response;
    }
}
shobhitpuri commented 7 years ago

@the4thfloor What did you end up doing? What turned out to be optimal? Thanks in advance for taking time to answer.

ralph-bergmann commented 7 years ago

@shobhitpuri currently I use this NetworkInterceptor for OkHttp

sid6mathur commented 7 years ago

Hi @the4thfloor , @shobhitpuri : You should start sampling when it comes to foreground, and stop it when it goes into background. The larger the HTTP transfers that occur between the start() and stop(), the better the odds that you don't have an "UNKNOWN" estimate.

In case your app requires quick sampling to tune video quality defaults or image loading strategies, you can also try the library that I have built : https://getfastah.com/docs/api . Since it uses its own geographically distributed ping-server network, it does a much better job estimating WiFi or 4G congestion accurately (and very fast). It's a paid API, in private beta at the moment.