我正在尝试从在线后端以及本地数据存储加载对象。因此我使用了两个不同的查询。先在线查询:
PFQuery *onlineQuery = [PFQuery queryWithClassName:@"Trip"];
[onlineQuery whereKey:@"users" equalTo:[PFUser currentUser]];
[onlineQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSLog(@"Trips loaded from server!");
} else {
NSLog(@"Could not load trips from server!");
[onlineQuery cancel];
}
}];
本地数据存储的查询如下所示:
PFQuery *localQuery = [PFQuery queryWithClassName:@"Trip"];
[localQuery whereKey:@"users" equalTo:[PFUser currentUser]];
[localQuery fromLocalDatastore];
[localQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
// at this point the objects array is empty, but should contain objects
NSLog(@"Trips loaded from local datastore!");
}];
问题是,如果我进行在线查询,它会返回与当前用户相关的所有对象。但是本地查询为同一用户返回 0 个对象。我还检查了 currentUser 不为零。如果我删除[localQuery whereKey:@"users" equalTo:[PFUser currentUser]];
本地查询返回所有对象的行,这意味着它们已成功保存。此外,将对象保存到本地数据存储时的方法返回它已成功保存。
PFObject *newTrip = [PFObject objectWithClassName:@"Trip"];
PFRelation *rel = [newTrip relationForKey:@"users"];
[rel addObject:[PFUser currentUser]];
[newTrip pinInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (succeeded) {
// succeeded is YES, therefore saving was succesful
NSLog(@"Trip saved to local datastore");
}
}];