Use RxJava to obtain data from multiple data sources

Source: Internet
Author: User

Use RxJava to obtain data from multiple data sources

Imagine that when we need some dynamic data, we only need to request the network each time. However, it is more efficient to cache the data obtained from the Internet to the disk or memory.

Specifically, the plan is as follows:

I will implement this plan by using RxJava.

Basic Mode

Create an Observable for each data source network, disk, and memory. Use the concat () and first () operators to construct a simple implementation method.

The concat () operator holds multiple Observable objects and concatenates them into a queue in sequence. The first () operator only extracts from the tandem queue and sends the first event. Therefore, if you use concat (). first (), no matter how many data sources, only the first event will be retrieved and sent.

 
  1. // Our sources (left as an exercise for the reader) 
  2. Observable memory = ...;  
  3. Observable disk = ...;  
  4. Observable network = ...; 
  5.  
  6. // Retrieve the first source with data 
  7. Observable source = Observable  
  8.   .concat(memory, disk, network) 
  9.   .first(); 
  10.  
  11. // Our sources (left as an exercise for the reader) 
  12. Observablememory = ...;   
  13. Observabledisk = ...;   
  14. Observablenetwork = ...; 
  15.  
  16. // Retrieve the first source with data 
  17. Observablesource = Observable   
  18.   .concat(memory, disk, network) 
  19.   .first(); 

The key to this mode is that the concat () operator subscribes to all Observable data sources only when data is required. Because the first () operator will stop the query queue earlier, if there is cached data, there is no need to access a slow data source. In other words, if memory returns results, you do not have to worry about disk and network access. On the contrary, if neither the memory nor the disk has data, the network request is executed.

Note that the Observable data source held by concat () is retrieved one by one.

Persistent data

Obviously, the next step is to cache data. If the results of network requests are not cached to the disk and the results of disk access are cached to the memory, this is not called cache at all. The code to be written next is the persistent operation of network data.

My solution is to save or cache data for each data source after the event is sent.

 
  1. Observable networkWithSave = network.doOnNext(new Action1() { 
  2. @Override public void call(Data data) { 
  3. saveToDisk(data); 
  4. cacheInMemory(data); 
  5. } 
  6. }); 
  7.  
  8. Observable diskWithCache = disk.doOnNext(new Action1() { 
  9. @Override public void call(Data data) { 
  10.   cacheInMemory(data); 
  11. } 
  12. }); 
  13.  
  14.  ObservablenetworkWithSave = network.doOnNext(new Action1() { 
  15. @Overridepublic void call(Datadata) { 
  16. saveToDisk(data); 
  17. cacheInMemory(data); 
  18. } 
  19. }); 
  20.  
  21. ObservablediskWithCache = disk.doOnNext(new Action1() { 
  22. @Overridepublic void call(Datadata) { 
  23.   cacheInMemory(data); 
  24. } 
  25. }); 

Now, if networkWithSave and diskWithCache are used, the data will be automatically saved after being loaded.

Another advantage of this policy is that networkWithSave and diskWithCache can be used anywhere, not limited to our multi-data mode .)

Old Data

Unfortunately, the code that saves the data is too much executed. Whether or not the data is outdated, it always returns the same data. We hope to occasionally connect to the server to capture the latest data.

The solution is to use the first () Operator for filtering. It is set to refuse to receive valuable data.

 
  1. Observable source = Observable 
  2.     .concat(memory, diskWithCache, networkWithSave) 
  3.     .first(new Func1() { 
  4.       @Override public Boolean call(Data data) { 
  5.         return data.isUpToDate(); 
  6.       } 
  7.     }); 
  8.  
  9. Observablesource = Observable 
  10.     .concat(memory, diskWithCache, networkWithSave) 
  11.     .first(new Func1() { 
  12.       @Overridepublic Boolean call(Datadata) { 
  13.         return data.isUpToDate(); 
  14.       } 
  15.     }); 

Now, we only need to send the event that is determined to be the latest data. Therefore, as long as the data of one data source expires, the next data source will be retrieved until the latest data is found.

Comparison between the first () and takeFirst () Operators

For this design pattern, the first () and takeFirst () operators can be either of them.

The difference between the two call methods is that if data from all data sources expires and no valid data is sent as an event, first () will throw a NoSuchElementException exception. Note: first () return false), while the takeFirst () operator calls the operation directly to complete the operation. No exception is thrown.

Which operator is used depends entirely on whether or not to explicitly process the missing data.

Sample Code

It can be checked out from here, the implementation example of all the code above: https://github.com/dlew/rxjava-multiple-sources-sample.

If you need a real example to check out the Gfycat App, it uses this mode when obtaining data. The project does not use all the functions shown above because they are not required. However, it demonstrates the basic usage of concat (). first.


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.