I have a Swift app in which I have a PFQueryTableViewController
, and I'd like to use the local datastore with Parse. But, I'm getting confused about using the local datastore alongside live queries.
Here is what I'd like to do:
PFQueryTableViewController
is shown, I'd like it to always get data from the local datastoreHow do I achieve this?
Here's how I managed this - maybe it will put you on the right track. I'd be interested to see your solution if you have already fixed the problem.
First, I made a convenience method to create my base query:
- (PFQuery *)baseQuery
{
PFQuery *query = [PFQuery queryWithClassName:@"MyClass"];
[query orderByDescending:@"myParameter"];
return query;
}
We want queryForTable
to hit the local datastore consistently.
- (PFQuery *)queryForTable
{
return [[self baseQuery] fromLocalDatastore];
}
So now all that's left to do is populate the local datastore from the network:
- (void)refreshObjects
{
[[[self baseQuery] findObjectsInBackground] continueWithBlock:^id(BFTask *task) {
if (task.error) {
[self.refreshControl endRefreshing];
return nil;
}
return [[PFObject unpinAllObjectsInBackgroundWithName:@"cacheLabel"] continueWithSuccessBlock:^id(BFTask *unused) {
NSArray *objects = task.result;
return [[PFObject pinAllInBackground:objects withName:@"cacheLabel"] continueWithSuccessBlock:^id(BFTask *unused) {
[self.refreshControl endRefreshing];
[self loadObjects];
return nil;
}];
}];
}];
}
We can call this whenever we want: in viewDidLoad
or viewDidAppear
, in response to a pull-to-refresh event (which is why I have the UIRefreshControl
code in there) or whenever else it might be appropriate.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With