7

I have the following situation:

I have a Service that checks periodically for new data over the internet,

  • when new data are available they are downloaded and saved on sqlite.
  • when the save to db is complete the service broadcasts an intent so that the activity knows to pull the new data from the db.

The user might want to request an immediate update...

...in that case I use a Messenger to request the Service to look for new data

Here is the problem:

the user is notified that a request is ongoing, but it might take a while, can be unsuccessful, could never return...

currently I get a message (using a Messenger) back from the Service to the Activity informing of the result of the request, or, if I get no message, in x seconds I inform the user that the request was unsuccessful.

  1. Please can you suggest a different approach?
  2. I don't like to wait for a message and if after x seconds none is received inform the user, is there a better way?
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Lisa Anne
  • 4,482
  • 17
  • 83
  • 157
  • "but it might take a while, can be unsuccessful, could never return..." these are all common sceneries right? If there is low network or server respond with delay for some reason, client had to handle and inform the user accordingly. Correct me if I'm misunderstanding your question. – cgr Dec 03 '15 at 08:43
  • Are you using a SyncAdapter?? – Lucas Crawford Dec 04 '15 at 22:56
  • @LucasCrawford nope Lucas – Lisa Anne Dec 05 '15 at 11:35
  • You should really use a SyncAdapter for the automatic updates as it optimizes the network usage so that the Android radio is doing work in an optimal way on battery usage. It is a pretty simple upgrade from the way you have described your setup already, and improves your app. Onto your question, I think there isn't much more you can do. The DB either returns data or doesn't, and depdning on how you handle the query time that' all you can do for the user is tell them that the request was a failure. Best practices say to have appropriate messages for all particular cases (network, server, input) – Lucas Crawford Dec 06 '15 at 22:09
  • You can show a notification. – JiTHiN Dec 07 '15 at 10:40
  • @LucasCrawford a SyncAdapter to read data?? – Lisa Anne Dec 09 '15 at 17:14

3 Answers3

3

You've got the basics, so not much else to recommend. I'll just show you a few alternatives:

  • You can use ContentObserver and update the UI once there is new data in the database (no need to wait for a message from the service).
  • If you've got a lot of communication between Service <-> UI components, it might be easier if you take a look at Otto, EventBus or just restructure your code around Observables / RxJava.
  • You can move the Timeout logic to the service (it will be easier this way since all the error handling will be in a single place) and just return the error message to the UI. Most network frameworks allow you to set a Connection Timeout parameter and will fail the request after this time is reached. If you haven't looked at network frameworks yet - Retrofit + OkHttp is a great starting point.
Samuil Yanovski
  • 2,837
  • 17
  • 19
2

You might consider optimistic rendering/optimistic updates- A pattern in which you update the UI on client as if it has being successful on the server. Once you get response from the server you update the UI accordingly.You can refer apps with new designs like google hangouts.

For more info refer this discussions:

I guess using this approach will give better usability to your app.

The current implementation looks ok. However you can improve it by following this talk-https://www.youtube.com/watch?v=BlkJzgjzL0c

Community
  • 1
  • 1
rupesh jain
  • 3,410
  • 1
  • 14
  • 22
1

If you think about this as a Model View Controller problem, the issue here is the lack of a model to represent the state of the Service. When the Service is performing the refresh, this "state" needs to reflected in your UI. Thus the Service needs to record this somewhere that the UI can access.

One option is simply a piece of shared memory, such as a Singleton object or even a static member variable (not recommended). Another option is to keep that state in your database.

Another issue is notifying the UI when this state changes. As mentioned by other posts there are multiple ways of doing this such as a LocalBroadcast, message bus like Otto, ContentObservers, etc.

Ilan Klinghofer
  • 860
  • 8
  • 10