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

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

问题描述

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

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:

游戏"类包含赢家"

winners"是一个NSDictionary的数组,字典中的每一项都是1Player到NPowers的映射

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

playerPowers 值是玩家 (PFObject) 的 key:objectIdPFObjects 数组(权力目前只有一个名称)

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 的值为空).以下是查询,然后是打印结果的代码,然后是包含两个获胜者的字典的输出:

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

//在tableview中的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)> {
}",  // problem is {
}. Data exists on server but not in local structure after query
    "<Power:eQkMUThGOh:(null)> {
}"   // ditto
);
}

推荐答案

当您检索与其他 PFObjects(Powers 数组)相关的 PFObject(游戏)时,这些 Powers 的值未检索.您必须在后续的获取请求中获取这些 Powers 的所有值.

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.

来自解析文档:

默认情况下,当获取一个对象时,相关的 PFObjects 不是取来的.这些对象的值无法检索,直到它们具有像这样被获取:

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

关于 Fetch 与 Find 的澄清:在 PFObjects (docs) 上调用获取,而在 PFQueries 中使用 Finds (docs="https://www.parse.com/docs/ios/api/Classes/PFQuery.html">docs).

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

获取需要 PFObjects 作为输入,并且不返回任何内容.获取只是更新您已经从 Parse 检索到的 PFObjects 上的值.另一方面,Finds 将从 Parse 中检索 PFObjects.

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天全站免登陆