Reputation: 4595
I have the following situation:
I have a Service that checks periodically for new data over the internet,
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.
Upvotes: 7
Views: 261
Reputation: 890
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.
Upvotes: 1
Reputation: 3430
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
Upvotes: 2
Reputation: 2867
You've got the basics, so not much else to recommend. I'll just show you a few alternatives:
Upvotes: 3