Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Volley or Service with cursor loader

I almost always use a Service when I download data from a web service. I store the result in a database and displays the result in my view using a cursor loader. But after Google released the network library Volley I have become a bit confused. The volley library uses async tasks instead of a Service and it doesn't use cursors. I thought that I was supposed to avoid async task and store my data in a database so that I could handle orientation changes properly - without loosing data and having the need to download the data again.

So my question is, when should I use Volley instead of my own download strategy?

like image 984
Kasper Finne Nielsen Avatar asked Aug 04 '13 14:08

Kasper Finne Nielsen


People also ask

What is the use of cursorloader?

Loader, more specifically, CursorLoader queries the Content Resolver in the background thread so that the application's User Interface is not blocked and returns the loaded Cursor to the Activity or Fragment. CursorLoader implements Loader protocol for querying cursors. CursorLoader handles the life cycle of the cursor.

Is it possible to use the cursors loader with the controller?

Re-Introducing the "Cursors Loader" with Playstation Controller Button by DrexomXander! Well of course. Absolutely. Other user's assets Some assets in this file belong to other authors.

What is the difference between asynctaskloader and cursorloader?

AsyncTaskLoader - an abstract loader that provides an AsyncTask to perform load operations on a separate thread. CursorLoader - a concrete subclass of AsyncTaskLoader for asynchronously loading data from a ContentProvider.

How to get data from API using Gradle volley?

Below is the dependency for Volley which we will be using to get the data from API. For adding this dependency navigate to the app > Gradle Scripts > build.gradle (app) and add the below dependency in the dependencies section. We have used the Picasso dependency for image loading from the URL.


1 Answers

Traditional Arch

Personally, in the past I've found using a service to be cumbersome to implement but at the end of the day was well structured and was a good consistent experience. But threading performance... hard to manage.

Service query - > database load -> notify

UI initiate query and cursor load - > update ui.

Volley Only

With volley it is tempting to skip the entire component that was previously handled within a service and database.

UI Volley request -> Volley response -> update ui

However, this falls apart quite quickly depending on the data you are trying to display and perhaps even the server you are querying against for any of the following requirements

  • the data being displayed is not fully described by the same URL (eg. pages)

The user might scroll down and pull in more pages. However when the user comes back to the activity or even simply rotates, the call to Volley will return a result for only the initial page unless you specifically remember all of the other queries that the page consists. This becomes a lot of work for an architecture that is meant to be more convenient. Or even a page that is slightly different, this can be an inconsistent experience to the user if all they did was rotate the phone.

  • the data has to be modified

It's easier to apply the change locally and then apply to the server whenever. With volley only, you would have to do an REST update and requery (of all previous queries) synchronously.

  • Speed and persistence

Volley is really fast. However, it lacks any persistence apart from cache hits where available which can depend on the server you are querying. Aggressive caching can even wreak havoc on your app with stale data. Pulling from a local database provides a consistent and fast experience when navigating through multiple activities that might actually be referencing data in past queries. A pure volley experience might require you to query for data you technically already have from previous queries but have no central data store to get that data from.

Hybrid Volley and Cursors

These days I actually skip the service part, but everything else remains from the traditional arch

UI initiate volley query and cursor load - > update ui

Volley query -> update database -> notify

Also, there is nothing stopping you from having the ui ping a service and then the service uses volley... It will look more traditional there might be value moving more control logic to somewhere more centralised, but that fact that it's run from within a "service" actually offers no technical advantage.

Summary

I hope that helps. Basically, don't attempt to go volley only, I tried and it would be a very specific and simple app if that's what worked for you and hopefully I've identified the main pitfalls.

Also, I found the same pitfalls with robospice despite it being in a service... But... YMMV

like image 196
Pork 'n' Bunny Avatar answered Sep 20 '22 20:09

Pork 'n' Bunny