查询本地数据存储 [PFUser currentUser] returns 没有对象

Query on local datastore with [PFUser currentUser] returns no objects

我正在尝试从在线后端以及我的本地数据存储区加载对象。因此我使用了两个不同的查询。先在线查询:

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!");
}];

问题是,如果我进行在线查询,returns 与当前用户相关的所有对象。但是本地查询同一个用户returns0个对象。我还检查过 currentUser 不为零。如果我删除行 [localQuery whereKey:@"users" equalTo:[PFUser currentUser]]; 本地查询 returns 所有对象,这意味着它们已成功保存。还有将对象保存到本地数据存储 returns 时的方法,它已成功保存。

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");
    }
}];

根据我们上面的讨论;

如果您有一个活动的 PFUser 会话,那么该用户将自动被指定为所有者。即使您没有当前会话并且没有 [PFUser currentUser] 设备拥有本地数据存储。目前,本地数据存储是偏见。由于应用程序是沙盒化的,并且您在自己的资源包之外没有写入权限,因此其他用户无法更改 alter/update 您的本地数据存储。即使有后端,你也不能更新 User class 中的任何用户信息,除非你是当前用户或有写入权限。对于本地数据存储,无论会话如何,它始终假定您是“当前用户”。但是,如果您确实想忽略本地数据存储中受保护信息的角色,您可以在此处查看他们的注释:

https://parse.com/docs/ios_guide#users-lds/iOS

The same security model that applies to objects in Parse applies to objects in the Local Datastore. Read-write permissions are defined by PFACLs and a user cannot access or modify anything they don't have permission to.

The only difference is that you won't be able to access any data protected by Role based ACLs due to the fact that the Roles are stored on the server. To access this data protected by Role based ACLs, you will need to ignore ACLs when executing a Local Datastore query

不过,考虑到你的情况,我觉得你不需要这个。