facebookarchive/network-connection-class

startSampling stopSampling for every network request?

ralph-bergmann opened this issue · 3 comments

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;
    }
}

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

@shobhitpuri currently I use this NetworkInterceptor for OkHttp

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.