How to use PFQuery for array of PFObject stored as

2019-02-15 21:43发布

I'm using parse.com in an iOS app to store data to the parse cloud service. I'm having trouble with a query of nested objects. I have the following data model:

Class "Game" contains "winners"

"winners" is an array of NSDictionary, each item in the dictionary is a mapping of 1 Player to N Powers

playerPowers value is an array of PFObjects (powers only have a name currently) for key:objectId of a player (PFObject)

For each winner, I add to "winners" (there can be multiple winners) an NSDictionary object, like so:

NSDictionary * winnerWithPowers = [NSDictionary dictionaryWithObject:tempPowers
                                                forKey:[aWinnerPFObject objectId]];
[newGame addObject:winnerWithPowers forKey:@"winners"];

For each item in the dictionary, the key is an existing objectId of a player and the value is an array of PFObjects (powers) also on the server. When i query for the "winners" i'd like to retrieve all the data populated, all winners and their respective power PFObjects with all their data. When i query for the "winners" the details of each power PFObject is incomplete (value for key:name is null). following is the query, then the code that prints results, followed by output of a dictionary containing two winners:

// In viewWillAppear:

PFQuery * gamesQuery = [PFQuery queryWithClassName:@"Game"];
[gamesQuery orderByDescending:@"createdAt"];
[gamesQuery findObjectsInBackgroundWithBlock:^(NSArray * theGames, NSError * error) {
    if (error) {
        NSLog(@"ERROR: There was an error with the Query for Games!");
    } else {
        _gamesPF = [[NSMutableArray alloc] initWithArray:theGames];
        [_tableView reloadData];
    }
}];

// in the tableview cellForRowAtIndexPath: method (it's my own TableViewController)

NSArray * testArray = [[_gamesPF objectAtIndex:row] objectForKey:@"winners"];
if ([testArray count] > 0) {
    // print contents of first dictionary winners entry
    NSLog(@"TestDictfromPF %@", [testArray objectAtIndex:0]);
}

Log:

2013-01-18 09:42:26.430 GamesTourney[20972:19d03] TestDictfromPF {

jchtlqsuIY =     (
    "<Power:OlJfby1iuz:(null)> {\n}",  // problem is {\n}. Data exists on server but not in local structure after query
    "<Power:eQkMUThGOh:(null)> {\n}"   // ditto
);
}

2条回答
够拽才男人
2楼-- · 2019-02-15 22:24

When you retrieve a PFObject (Game) that is related to other PFObjects (an array of Powers), the value of those Powers are not retrieved. You will have to fetch all the values of those Powers in subsequent fetch requests.

From the Parse documentation:

By default, when fetching an object, related PFObjects are not fetched. These objects' values cannot be retrieved until they have been fetched like so:

PFObject *post = [fetchedComment objectForKey:@"parent"];
[post fetchIfNeededInBackgroundWithBlock:^(PFObject *object, NSError *error) {
  NSString *title = [post objectForKey:@"title"];
}];

Clarification on Fetch vs Find: Fetches are called on PFObjects (docs) whereas Finds are used in PFQueries (docs).

Fetches require PFObjects as input, and don't return anything. Fetches simply update the values on PFObjects you've already retrieved from Parse. Finds, on the other hand, will retrieve PFObjects from Parse.

Since you have an array of Powers (which are PFObjects), use the following to retrieve all the values from Parse:

[PFObject fetchAllIfNeeded:(NSArray *)myPowers];

or fetchAllIfNeededInBackground: if you want it to be asynchronous.

查看更多
劫难
3楼-- · 2019-02-15 22:40
PFQuery *query = [PFQuery queryWithClassName:@"Cars"];
[query findObjectsInBackgroundWithBlock:^(NSArray *comments, NSError    *error) {
    for (PFObject *comment in comments)
    {
        PFObject *post = [comment objectForKey:@"name"];
        NSLog(@"%@",post);
查看更多
登录 后发表回答