7

I have an Android database hosted in Parse and I have been trying to find the best way/practices to update my LocalStorage with the latest remote changes.

I'm not asking for code or syntax, I would just be looking for a set of steps to follow, I'm sure I would need to use a worker thread because I don't want to block the UI, I would love to have the transition as smooth as possible for the user (he wouldn't even realise an update is happening) and I want it to happen when the app is on the foreground. The ideas I've gotten so far:

  • Use multithreading (Loopers, Handlers, Thread) and update each table in a separate thread.
  • Try to throttle the request to minimize the CPU usage and get a table at a time.
  • Use a Loader and just listen for the changes.
  • Call an IntentService to get table by table also.
  • Create an Unbound Service and let the database update after the app is closed.
  • Don't update the database in the background and wait until the user actually needs the data and the retrieve it.

I've already tried the IntentService one, it's not as smooth as I would like:

public class DataService extends IntentService {
    public DataService() {
        super(DataService.class.getName());
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        String[] entities = {
                "Img",
                "Pack",
                "Technology",
                "Term",
                "TermHierarchy",
                "TermImplementation",
                "TermTerm",
                "UserTechnology",
                "UserTerm",
        };

        for (String entity : entities) {
            updateLocalTable(entity);
        }
    }

    private void updateLocalTable(String entity) {
        ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(entity);
        query.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(List<ParseObject> objects, ParseException e) {
                if (e == null){
                    ParseObject.pinAllInBackground(objects);
                }else {
                    Log.e(TAG_, e.toString());
                }
            }
        });
    }
}

Also, I'm calling it the first time a connection change occur (Wi-Fi available). Should I instead just call it on the onCreate, or create an Alarm to update once every certain amount of time, or just when the Activity is finishing?

I've seen a few post related:

Update data in Background Periodically (Android)

Syncronizing Android client with remote SQL-server database, using web service

Send database data through socket connection

sync remote database (mysql) with sqlite database and vice versa

But none of them have good and detailed answers and they don't really tackle the problem.

I will also add that Parse handles all the RESTful calls and the JSON conversion so I don't have to worry about that.


Edit.

I just found a video that explains these concepts (and supports the answer provided by @gsgsgs) in less than 2 minutes:

https://www.youtube.com/watch?v=Expbi0lHLRE

Community
  • 1
  • 1
Evin1_
  • 12,292
  • 9
  • 45
  • 47

2 Answers2

3

You can try SyncAdapter:

The sync adapter component in your app encapsulates the code for the tasks that transfer data between the device and a server

It's not very simple to implement, but have some advantages:

  • Background sync scheduled by system with other network requests to save battery
  • It can be configured by user in sync settings (disable or enable background sync)
  • Adapter retry sync automatically if request failed with exponential backoff
  • You can simple use it with yours ContentProvider
  • Adapter can sync in a separate process if you want

Here some tutorial.

Kirill
  • 7,580
  • 6
  • 44
  • 95
3

@Evin1_,

In my opinion the important things are:

1/ Think about the order:

If some tables nedds informations from another, start by synchronize them in a correct order

2/ Use indexes:

Indexes are special lookup tables that the database search engine can use to speed up data retrieval

3/ Use transactions:

It will give you an opportunity to rollback if something goes wrong

4/ Multithreading:

The speed and efficiency of a long-running, data-intensive operation often improves when you split it into smaller operations running on multiple threads

5/ Prevent connection lost:

If the user disables 3G or Wifi you have to be able to know what happend, display error messages or something else.

6/ The amount of datas:

If you have a huge amount of datas don't send them in a sinle Json, cut it in smaller packets

Hope it helps.

Nicolas Cortell
  • 659
  • 4
  • 16
  • Both are very good answers, but I will pick the one from @gsgsgs because it is a little bit more Android oriented. Thanks! – Evin1_ Jan 24 '16 at 09:28