如何将PFQuery用于存储为NSDictionary值的PFObject数组 [英] How to use PFQuery for array of PFObject stored as values of NSDictionary

查看:62
本文介绍了如何将PFQuery用于存储为NSDictionary值的PFObject数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iOS应用中使用parse.com将数据存储到解析云服务.我在查询嵌套对象时遇到了麻烦.我有以下数据模型:

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"

获胜者"是NSDictionary的数组,字典中的每个项目都是1个玩家到N个力量的映射.

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

playerPowers值是播放器(PFObject)的 key:objectId PFObjects数组(当前电源仅具有名称)

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

对于每个获胜者,我都会向获胜者"(可以有多个获胜者)添加一个NSDictionary对象,如下所示:

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

对于字典中的每个项目,键是播放器的现有objectId,并且值也是服务器上PFObjects(电源)的数组.当我查询获奖者"时,我想检索所有填充的数据,所有,所有获奖者及其各自的力量PFObjects及其所有数据.当我查询优胜者"时,每个功效PFObject的详细信息都不完整(key:name的值为null).以下是查询,然后是打印结果的代码,然后输出包含两个优胜者的字典:

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:

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

//在表格视图中的 cellForRowAtIndexPath:方法(这是我自己的TableViewController)

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

日志:

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

推荐答案

当您检索与其他PFObject(幂数组)相关的PFObject(游戏)时,不会检索这些幂的值.您将必须在后续的提取请求中提取这些Power的所有值.

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.

从解析"文档中:

默认情况下,在获取对象时,相关的PFObject不会 拿来.这些对象的值只有在具有以下值时才能被检索 如此获取:

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

关于抓取与查找的说明: 在PFObjects( docs )上调用提取,而在PFQueries中使用查找(文档).

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

访存需要PFObjects作为输入,并且不返回任何内容.提取仅更新您已经从Parse中检索到的PFObject上的值.另一方面,查找将从解析中检索PFObject.

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.

由于您具有Powers数组(即PFObjects),请使用以下命令从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];

fetchAllIfNeededInBackground:,如果您希望它是异步的.

or fetchAllIfNeededInBackground: if you want it to be asynchronous.

这篇关于如何将PFQuery用于存储为NSDictionary值的PFObject数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆