objective-cparse-platformofflinepfuserlocal-datastore

How do I store ParseObject in both Local Datastore and Parse Cloud on Objective-C iOs 8?


I'd like to save all my objects both locally and in cloud and read from Local Datastore only. All my objects should be saved in both local and online store and local datastore should be synced to Parse Cloud.

I want my App to work Offline so as to use it Everywhere, but saving all the data in the Cloud asap the NetConnection is Available !

Thanks


Solution

  • First of all you need to enable the use of local datastore:

    [Parse enableLocalDatastore];
    

    Next, I always save a new PFObject to the local datastore using

    PFObject *userStat;
    [userStat saveEventually];
    

    This will both pin your object to the local datastore and save it to cloud (eventually). If you want to reset all of your locally stored data with what you have in the cloud, you can; first unpin all your local objects and then fetch all the remote objects and pin them locally:

    [PFObject unpinAllObjects];
    PFQuery *query = [PFQuery queryWithClassName:@"UserStats"];
    [query whereKey:@"parent" matchesQuery:query];
    
        return [[query findObjectsInBackground] continueWithBlock:^id(BFTask *task) {
            if (task.error){
                 return nil;
            }
             return [[PFObject pinAllInBackground:task.result]     continueWithBlock:^id(BFTask *task) {
                return task;
            }];
    }];
    

    Local queries can be done using:

     [query fromLocalDatastore];
    

    Parse has good documentation of these methods here.