Parse.com 的奇怪问题,其中没有包含密钥 [英] Strange issue with Parse.com with key not being included

查看:22
本文介绍了Parse.com 的奇怪问题,其中没有包含密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个与此非常相似的问题 一个

I'm having an issue very similar to this one

基本上我使用 Parse.com 来加载一些具有 PFUser 指针的对象,然后我还使用 includeKey 来包含那些 PFUser,这是代码...

Basically I'm using Parse.com to load some objects, which have PFUser pointers, and then I'm also using includeKey to include those PFUsers, here's the code...

PFQuery *query = [PFQuery queryWithClassName:@"GameVillageObject"];

    [query whereKey:@"region" equalTo:[NSNumber numberWithInt:region]];
    [query includeKey:@"pfUser"];
    query.limit = 100;

    [sharedInstance requestSentWithDesc:@"Get all village objects in region"];

    [query findObjectsInBackgroundWithBlock:^(NSArray *PUObjects, NSError *error) {
        if (!error) {
            // The find succeeded.
            NSLog(@"Successfully retrieved %d village objects from server.", PUObjects.count);

            if(PUObjects.count > 0)
            {
                villageObjects = PUObjects;

                for (int i=0; i<[villageObjects count]; i++)
                {

                    PFObject *villageItem = [villageObjects objectAtIndex:i];

                    PFUser *user = [villageItem objectForKey:@"pfUser"];

                    NSString *userName = [NSString stringWithFormat:@"%@",[user objectForKey:@"username"]];

                    NSLog(@"User name is: %@.", userName);


                }


                [self setupVillageList];

                [sharedInstance centerImage:marketItemsContainer xChoice:YES yChoice:NO];

            }

        } else {
            // Log details of the failure
            NSLog(@"Getting village objects Error: %@ %@", error, [error userInfo]);

        }
    }];

现在出于某种原因,时不时地,可能 10 次中有 1 次,游戏崩溃,并出现此错误

Now for some reason, everynow and again, maybe 1 time out of 10, the game is crashing, with this error

键用户名"没有数据.在获取它之前调用 fetchIfNeeded价值.

Key "username" has no data. Call fetchIfNeeded before getting its value.

虽然我无法判断它是在我尝试使用上面的用户名"时崩溃,还是稍后尝试使用用户名"时崩溃,但无论哪种方式,我都不明白为什么大多数时候它有包括那些额外的对象没问题,然后几次就没有了.有什么想法吗?

Although I can't tell if it's crashing when I try to use "username" above, or a little later when I try to use "username" but either way, I don't get why most of the time it has no problem including those extra objects and then a few times it doesn't. Any ideas?

推荐答案

听起来好像关联的 user 没有被加载,但在大多数情况下可能会被缓存(因此可用).我可以想到两种方法来解决这个问题.

It sounds as though the associated user is not being loaded, but may be cached in most instances (and thus available). I can think of two ways to resolve this.

1) 按照错误消息的建议调用 fetchIfNeeded:

1) Call fetchIfNeeded as suggested by the error message:

PFQuery *query = [PFQuery queryWithClassName:@"GameVillageObject"];

[query whereKey:@"region" equalTo:[NSNumber numberWithInt:region]];
[query includeKey:@"pfUser"];
...
  PFObject *villageItem = [villageObjects objectAtIndex:i];

  PFUser *user = [villageItem objectForKey:@"pfUser"];
  [user fetchIfNeeded];
  NSString *userName = [NSString stringWithFormat:@"%@",[user objectForKey:@"username"]];

  NSLog(@"User name is: %@.", userName);

2) 在includeKey:调用

[query whereKey:@"region" equalTo:[NSNumber numberWithInt:region]];
[query includeKey:@"pfUser.username"];
query.limit = 100;
...

这应该告诉 Parse 在执行查询时加载 username.

This should tell Parse to load username when the query is executed.

这篇关于Parse.com 的奇怪问题,其中没有包含密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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