简体   繁体   中英

Updating Parse iOS Local Datastore

I am creating a game for the iPhone, and the main screen is a list of games that the current user has in progress. I want to use the parse local datastore because my queries take a long time when there are a lot of games in progress. I have a question about the datastore that does not seem to be answered clearly in the docs.

When the app opens, I query my table of games in the background, sort them based on status (your turn, their turn, over), and display them in a table. In order to speed this process up I would like to pin all of these objects to the local datastore.

When the user refreshes the tableview to see if any of the game statuses have changed, I would like to query the datastore to retrieve all of the relevant objects, but then also query the network for any changes in the objects, and for any new ones. However, it does not seem that there is anyway to make the local datastore "up-to-date". The only way I have seen is to query the network completely, unpin currently stored objects, and pin the newly retrieved objects. If this is the case, I do not see the point in using the local datastore because my queries will be just as slow.

Basically, everytime the user pulls to refresh the table, I would like the datastore to be queried, and then the network to check for any new objects or changed columns.

So, is there a way to query the local datastore and subsequently update those objects and/or add new ones from the network?

I have also realized that I am doing quite a bit of sorting. Could this have an affect on the long wait time for the table to load?

It seems you are looking for the cache policy kPFCachePolicyCacheThenNetwork which looks to the cache first to fill the table, then subsequently do a query against the network and then refresh the table again.

Note that cachePolicy could only be used without Local Datastore. If you want to go with Local Datastore, you can implement the same functionality by chaining queries.

PFQuery *queryLocal = [PFQuery queryWithClassName:@"SomeClass"];
[queryLocal fromPinWithName:@"SomeLabel"];
[[[queryLocal findObjectsInBackground] continueWithSuccessBlock:^id(BFTask *task) {
    NSArray *localObjects = task.result;
    // Fill the table here with localObjects

    // Then make an online query
    PFQuery *query = [PFQuery queryWithClassName:@"SomeClass"];
    return [query findObjectsInBackground];

}] continueWithSuccessBlock:^id(BFTask *task) {
    NSArray *objects = task.result;
    // Fill the table here with online objects
    // You should ensure that the most recent data is online

    return [[PFObject unpinAllObjectsInBackgroundWithName:@"SomeLabel"] continueWithSuccessBlock:^id(BFTask *ignored) {
        // Cache the new results.
        return [PFObject pinAllInBackground:objects withName:@"SomeLabel"];
    }];
}];

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