Issue
I have a screen in my app which displays data from an API. When the user is on the screen they can reorder the data, which I want to persist on the server.
Rather than making an API call every time the user reorders an item, I want to just make one call when they leave the screen.
What is the best way to do this? I'd ideally put it in onPause(), but I'm not sure whether I can ensure that the API call is always sent.
Thanks!
Solution
While onPause
does always gets called it is not only called when the user leaves the screen but sometimes when they resume it as well.
It is also not a great design pattern to run I/O heavy actions on these events and you will also have many issues around getting the response to verify the API indeed worked correctly.
I would recommend that you use a started Service as your outbound point of contact with the server and APIs and since a Service also has its own process it will be alive even if your application isn't (but you still need to use a bg thread impl. because it impacts the UI thread if you don't).
A couple of friends and i have written a good example for that awhile back and you can still find it here.
Goodluck.
Answered By - Royi Benyossef
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.