简体   繁体   中英

How structure a SQLite database for offline support?

I'm trying to give the offline support in my app, so user can read the information without the internet also. I'm creating an app to show some packages from different companies. I have created my API and following database in the remote server.

companies -- id, name
duration-- id, type (type could be, monthly, weekly, yearly)
packages -- id, name, company_id, duration_id 

My API is returning result as

{  
   "id":3,
   "package":"premimum",
   "company_id":6,
   "duration_id": 5,
}

Now I want to store all information fetched from the API to the local database SQLite. Now I want to know how I should structure my local database? should I structure same as the remote database? should I create the three tables in the local database SQLite same as?

companies -- id, name
duration-- id, type (type mean package duration, monthly, yearly, daily)
packages -- id, name, company_id, duration_id 

If yes then id will be different in local database and in the remote database, because both databases will generate own primary key how I can handle this? or something you can suggest me to keep the same ID? or should I keep the id different?

You don't necessarly need the internal Ids on your local database to match those of your cloud server database. For instance, if you use Parse as cloud server, Parse will generate its own internal object ID. Your app does not need to know them.

Having said that, I strongly suggest you have a common key attribute between your local object and cloud object, so that you can keep them synced.

For example you could add a package_id attribute on both your local and cloud object. This package_id will be the same on both side, so that you can update your local object when the remote object is updated and vice-versa.

For example, here is the code to create an object on the Parse server:

ParseObject gameScore = new ParseObject("GameScore");
gameScore.put("score", 1337);
gameScore.put("playerName", "Sean Plott");
gameScore.put("cheatMode", false);
gameScore.saveInBackground();

You can them retrieve this object from the app as follows:

ParseQuery<ParseObject> query = ParseQuery.getQuery("GameScore");
query.getInBackground("xWMyZ4YEGZ", new GetCallback<ParseObject>() {
  public void done(ParseObject object, ParseException e) {
    if (e == null) {
      // object will be your game score
      int score = object.getInt("score");
      String playerName = object.getString("playerName");
      boolean cheatMode = object.getBoolean("cheatMode");
    } else {
      // something went wrong
    }
  }
});

I suggest you read the documentation for more infos on storing object on a Parse server: https://docs.parseplatform.org/android/guide/

Also, this lib could help you to sync data between local database and a Parse server: https://github.com/ntoskrnl/DataSync

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM