How to handle REST calls, data persistence, synchronization and monitoring ContentProvider

I know that this question has been asked too many times, but I think that the problems I'm trying to set up are slightly different from each other, maybe more complicated.

I am going to develop an application that uses the RESTful Web Service and should have the following requirements:

  • the application should show some books, their authors and their editors in lists and in detail

  • the application must also allow book search

  • books, authors, and editors retrieved from the RESTful web service

  • each object must be cached, so when I open an action, I first see the old data (if any), and the new one is updated from the network.

  • every time an object is updated, stakeholders must be notified ( ContentObserver ? Regular Listener implementation?)

  • If the call is already being made (for example, api/books/1337 or api/editors ), the caller must be informed that he is downloading data and the old one (if one exists) should be indicated, as if he were the original caller.

  • some data (only books and authors) should be updated every N minutes (user-defined), and observers should be notified ( SyncAdapter ?)

Questions:

After reviewing and exploring all the components suggested by Virgil Dobrzanski in Google I / O 2010 , I doubt:

  • How can I transparently handle the concept of an update-entity for any caller? Do I have to use ContentObserver in the ContentProvider I have to implement?

  • If I use ContentObserver , I can easily set the status flag for a single object (as suggested by Dobjanschi) like UPDATING , INSERTING and soon. But how should I handle the list? Say I need a list of books, where should I put the status flag? Should I put it in a status table for lists only? If so, I should observe two Cursor s, one for status and one for the actual list (i.e. Tables / Content URI). And what if the entity I am asking for does not yet exist, or a REST call returns 404 ? How to handle a callback?

  • If I put all my REST methods in **SyncAdapter** , can I make "t24" update the list of entities / entities from the network (and therefore put it in the right table)? Thus, the status flag will be useful.

  • Can SyncAdapter work with several objects (in fact, lists of entities, since I want to periodically update books and editors), since it only has a performSync method?

  • If my implementation of SyncAdapter was disabled by the user in the device settings, it will not update anything (and this is fine). But if the user clicks the Refresh Books button in the Activity, can I still call the performSync method, or will it also be disabled?

+6
source share
2 answers

SyncAdapter is a design template that includes five components:

  • Application. To do this, use the Activity set along with Cursor and ContentObserver and, possibly, CursorAdapter , and some to provide the user interface with locally stored data from the ContentProvider .
  • ContentProvider The data ContentProvider of the local device. Handles CRUD calls, processes a SyncAdapter notification about the need for updates on the server.
  • Account The user ID on the remote server.
  • SyncAdapter A background process SyncAdapter and synchronizes the local data store with the server.
  • The server itself.

So. To the questions:

  • "Is-update" means "has local changes that have not yet been transferred to the server. This is a flag that you set in a row in your database. It is set in the ContentProvider when creating / updating / deleting a row. When SyncAdapter starts, it sees flag, pushes the update to the server, clears the flag.The flag itself does two things:
    a. Tells the user that the application is busy saving changes and when it will be done.
    b. Marks the line as changed, so the SyncAdapter knows to push it to the server.
    Read here for more details.

  • If you do not synchronize the entire directory, your client will directly query the server and cache the results by placing them in the ContentProvider. There is no status flag since they come from the server and therefore correspond to the status of the server. Write your SyncAdapter to ignore them or possibly discard them after they have been cached after a few days.

  • a. In order for your local updates to be sent to the server, you write ContentProvider to notify SyncAdapter during calls to ContentProvider Create / Update / Delete. (Read here ...)
    b. To periodically receive updates from the server, you set up an account for automatic synchronization. (Read here ...)

  • Yes. performSync is just a function call. Write it to do what you want. Ask him to get table 1 from the server and put it in one table in ContentProvider. Then take table 2 and place it in another table. Etc.

  • a. You can force the synchronization by calling ContentResolver.RequestSync() using ContentResolver.SYNC_EXTRAS_MANUAL in the extras bundle.
    b. You can manually extract something with the client code and directly paste it into the ContentProvider.

+10
source

Source: https://habr.com/ru/post/918774/


All Articles