Search code examples
iosparse-platformlocal-datastore

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


I'm trying to load objects from the online backend, as well as from my local datastore. Therefore I'm using two different queries. First the online query:

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

The query for local datastore looks like this:

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

The problem is that if I do the online query it returns all objects related to the current user. But the local query returns 0 objects for the same user. I've also checked that the currentUser is not nil. If I remove the line [localQuery whereKey:@"users" equalTo:[PFUser currentUser]]; the local query returns all objects, that means they were saved succesfully. Also the method when saving an object to the local datastore returns that it was saved succesfully.

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

Solution

  • As per our discussion above;

    Provided you have an active PFUser session, then that user will autonomously be designated as the owner. Even if you don't have a current session and there is no [PFUser currentUser] local datastore is owned by the device. For now, local datastore is prejudice. Since apps are sandboxed and you have no writing privileges outside of your own resource bundle, another user can not change alter/update your local datastore. Even with the backend, you can not update any users information in the User class, unless your the current user or have writing privileges. With local datastore, it's always assumed you are the "current user", regardless of the session. However, if you do want to ignore Roles for protected information in local datastore you can see their notes here :

    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

    However, I don't feel you need this considering your circumstance.