我試圖從在線後端以及從本地數據存儲中加載對象。因此我使用了兩個不同的查詢。首先,網上查詢:使用[PFUser currentUser]在本地數據存儲上查詢時不返回對象
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個對象。我也檢查過當前用戶不是零。如果我刪除行[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");
}
}];
當前用戶是一個做固定的人,那麼爲什麼你需要交叉引用呢?另一個用戶不能把東西釘到你的本地數據存儲 – soulshined 2015-03-02 16:36:09
這實際上是真的:)沒有想到這一點......感謝。 – gpichler 2015-03-02 16:37:48