Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refreshing a Parse Local Datastore Read Only

We are currently looking to develop an iOS App which shows a list of products for a user to view in a list view with an associated small PDF file and we are looking to utilise Parse to store the data as we can easily log into Parse and update the product information.

I'm currently struggling with the Parse Documentation ideally when the app launches we want it to query the PFObject and get all the rows within our class to store them locally using

[PFObject pinAllInBackground:objects];

Our app would also need to check and refresh this data store on future launches to make sure the latest product information is available.

When the user views the list of products we are looking to query the Local Datastore to show the data so the app fully works offline.

What I am struggling with is how to purge or refresh the local cache on re-launch users will not be making any changes to this local data and I was thinking of using

  [PFObject unpinAllInBackground:objects];

But when we do this followed by a query to pin all the Local Datastore is empty.

Thanks Aaron

like image 715
MonkeyBlue Avatar asked Sep 28 '22 15:09

MonkeyBlue


2 Answers

I managed to solve this problem myself with the following, it now runs in a background thread and first unpin's all the class objects followed by fetching and pinning all the objects again.

- (void)updateParseCacheForClass:(NSString *)className {

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{

        PFQuery *query = [PFQuery queryWithClassName:className];
        [query fromLocalDatastore];

        NSArray *objects = [query findObjects];
        if (objects) {
            [PFObject unpinAll:objects];
        }

        PFQuery *query2 = [PFQuery queryWithClassName:className];
        NSArray *objects2 = [query2 findObjects];
        if (objects2) {
            [PFObject pinAll:objects2];
            NSLog(@"Successfully retrieved %lu records. for %@", (unsigned long)objects.count, className);
        }
    });
}
like image 191
MonkeyBlue Avatar answered Oct 13 '22 11:10

MonkeyBlue


I would like to make @MonkeyBlue's code little bit smaller.

- (void)updateParseCacheForClass:(NSString *)className {

    PFQuery *query = [PFQuery queryWithClassName:className];
    NSArray *objects = [query findObjects];
    [PFObject pinAllInBackground:objects block:^(BOOL succeeded, NSError *error) {
        if(succeeded) {
            NSLog(@"Successfully retrieved %lu records. for %@", (unsigned long)objects.count, className);
        } else if (error) {
            NSLog(@"Error");
        }
    }];
}    

The -pin; method do the synchronisation automatically for you so you don't worry about the duplication of objects. This is the cool feature of the parse.

There are different version of -pin; methods are available that can be useful.

//for pining single object
- (BOOL)pin
- (BOOL)pin:(NSError **)error;
- (BOOL)pinWithName:(NSString *)name;
- (BOOL)pinWithName:(NSString *)name error:(NSError **)error;
- (BFTask *)pinInBackground;
- (void)pinInBackgroundWithBlock:(PF_NULLABLE PFBooleanResultBlock)block;
- (BFTask *)pinInBackgroundWithName:(NSString *)name;
- (void)pinInBackgroundWithName:(NSString *)name block:(PF_NULLABLE PFBooleanResultBlock)block;

//for pining array of objects
+ (BOOL)pinAll:(PF_NULLABLE NSArray *)objects;
+ (BOOL)pinAll:(PF_NULLABLE NSArray *)objects error:(NSError **)error;
+ (BOOL)pinAll:(PF_NULLABLE NSArray *)objects withName:(NSString *)name;
+ (BOOL)pinAll:(PF_NULLABLE NSArray *)objects withName:(NSString *)name error:(NSError **)error;
+ (BFTask *)pinAllInBackground:(PF_NULLABLE NSArray *)objects;
+ (void)pinAllInBackground:(PF_NULLABLE NSArray *)objects block:(PF_NULLABLE PFBooleanResultBlock)block;
+ (BFTask *)pinAllInBackground:(PF_NULLABLE NSArray *)objects withName:(NSString *)name;
+ (void)pinAllInBackground:(PF_NULLABLE NSArray *)objects withName:(NSString *)name block:(PF_NULLABLE PFBooleanResultBlock)block;
like image 23
Mahendra Avatar answered Oct 13 '22 13:10

Mahendra