How to query for objects that are not already in an array (refreshing for an array) using parse.com service?

StackOverflow https://stackoverflow.com/questions/17351983

Вопрос

I have a table view that is being populated with an array that is holding strings. This array receives its data from a cloud database on parse.com. It populates successfully in the beginning. I have a refresh button at the bottom of my view controller that is supposed to add all the new objects in the database that are not already in the array. I tried to do this by sending whereKey:notContainedIn: to a query object. But it doesn't fetch any of the new objects. Here is my code for the refresh method. What am I doing wrong?

-(void) refresh {
    PFQuery *query = [PFQuery queryWithClassName:@"Employee"];
    [query whereKey:@"Employee" notContainedIn:currentEmployees]; 
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            // The find succeeded.
            NSLog(@"Successfully retrieved %d scores.", objects.count);
            // Do something with the found objects
            for (PFObject *object in objects) {
                NSLog(@"%@", object.objectId);
                [employees addObject:object];
                dispatch_async(dispatch_get_main_queue(), ^ {
                    [hvc.tableView reloadData];
                });
            }
        } else {
            // Log details of the failure
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
    }];

edit: It's fetching objects now, but it's grabbing everything that was already in the database and adding them again. So if i had a list of

- dog 
- cat 

and I added bird to the database online, I would get:

- dog 
- cat 
- dog 
- cat 
- bird

how can I fix this?

Это было полезно?

Решение

When you use whereKey:notContainedIn:, the key should be a field on your Employee. If you're following naming conventions, what you've actually supplied is the object name itself. And it looks like you supply an array of objects. You should have an identity field (or something similar) and and you should be supplying an array of identities to whereKey:notContainedIn:. You can get the array of identities with:

NSArray *identities = [currentEmployees valueForKey:@"identity"];

(substitute the appropriate filed name in place of identity)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top