In recent years, Companies such as Google, Microsoft and Facebook have all set their sights on Africa. To better serve users with poor Internet access, the authors share how you can organize your Android app’s offline architecture.

Why do I have to think about this?

A great app has a great user experience. Users don’t care about the network, they just see that your app is stuck and uninstall it. So the real challenge is to design a good user experience for these users with poor Internet connectivity.

What should be done?

1. Determine the connection

The authors recommend using Facebook’s Network Connection Class to monitor the Network status of the current application. If you use OkHttp, you can use it in combination with the following code:

public class SamplingInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); DeviceBandwidthSampler.getInstance().startSampling(); Response response = chain.proceed(request); DeviceBandwidthSampler.getInstance().stopSampling(); return response; }}Copy the code

2. Efficient caching

The ability to cache efficiently and reuse previously acquired resources is key to optimizing performance, as you can see here for HTTP caching strategies.

3. Act locally, sync globally

Make the model persistent. When the server has new data, it first updates the Presenter layer and notifies the view of the update. EventBus is recommended. Similarly, when you need to send data to the server, update the data to the Presenter layer and update the interface before uploading it to the server. Do not display a progress bar and wait for the server to respond before updating the interface. If the device is offline, try again after coming back online. Try Evernote’s AndroidJob to simplify task scheduling.

If you don’t already know what a Presenter is, take a look at Google’s Android application architecture blueprint. 🙂

4. Efficient threads

Data interaction requires additional threads, but you want to avoid creating too many threads. There should be separate queues for network and local tasks, and you don’t want pending network tasks to become bottlenecks for local tasks. RxJava is recommended here to help with thread scheduling.


5. Image optimization

Control the image quality to be obtained according to the network condition. It is recommended to use the image compression parameter of RGB_565 by default, and request higher quality images if the network condition permits.

6. Use Big Cookie Model

To learn more about this, check out Google’s official Efficient Data Transfers video series.

Offline App Architecture, Build for the Next Billion