Mandeep Chaudhary
Mandeep Chaudhary

Reputation: 65

Get data from Parse server for offline usage

I am trying to develop an android application while storing database on parse server. Everything works fine but problem is with application offlime usage. Is there any way i can store data locally including log in details and fetch data from server only when updated. plz give me some suggestion examples would be better. Thanks

Upvotes: 0

Views: 1277

Answers (1)

Maravilho Singa
Maravilho Singa

Reputation: 224

You need to use Localdatastore.

  1. in your android app enable it.

    Parse.Configuration config = new Parse.Configuration.Builder(this)
            .applicationId("XXXXXXXXXXX") 
            .clientKey("XXXXXXXXXXX")
            .server("https://api.serverused.com/")
            .enableLocalDataStore() // Acticate it here
            .build();
    
    Parse.initialize(config);  //
    
  2. This is how to save the object or list of objects offline

     ParseQuery<ParseObject> query = ParseQuery.getQuery(“GameScore");
        query.getInBackground("xWMyZ4YE", new GetCallback<ParseObject>() {
        public void done(ParseObject object, ParseException e) {
           if (e == null) {
    
        // object will be your game score so save it offline
           object.pinAllInBackground();  
           } else {
        // something went wrong
        }
      }
    });
    
  3. This is how to get your localdata

    ParseQuery<ParseObject> query = ParseQuery.getQuery(“GameScore");
        query.fromLocalDatastore();
        query.getInBackground("xWMyZ4YE", new GetCallback<ParseObject>() {
        public void done(ParseObject object, ParseException e) {
           if (e == null) {
        // object will be your game score
           } else {
        // something went wrong
        }
      }
    });
    

If you are saving a list of objects, generally used with findInBackground, you will need to use

ParseObject.pinAllInBackground(objects);

Hope this helps and check the docs Parse LocalDataStore

Upvotes: 3

Related Questions