objective-c - PFUser 在 fetchAllIfNeeded 之后调用 objectForKey 时抛出 NSInternalInconsistencyException

标签 objective-c ios ios5 parse-platform

我的 PFUser“firstName”和“lastName”上有两个附加列。他们储蓄得当;我可以在数据浏览器中看到数据。

我有另一个 PFObject“类”,它有一个带有 PFUsers 的 NSArray 的属性。我在 PFObject 上使用类方法 +fetchAllIfNeededInBackground:block: 来获取 PFUsers 数组。在回调 block 中,我在数组中的每个 PFUser 上调用 objectForKey:,但我通过拥有的 PFObject 访问它们。

// invited is the NSArray of PFUsers
self.whoCell.mainLabel.text = [[self.plan.invited objectAtIndex:0] 
                                                  objectForKey:@"firstName"];

调试器在 objectForKey 调用之前的断点处输出:

(lldb) po self.plan.invited
(NSArray *) $4 = 0x06e62a60 <__NSArrayM 0x6e62a60>(
<PFUser:GCCdPjCU2J> {
    firstName = Fake;
    lastName = Account;
    username = yyajnbafv53qw4yhjm9sfoiis;
}
)

编辑:添加 self.plan.invited 的实现,因为上述内容具有误导性。

- (NSArray*)invited
{
  // self.dataSource is a PFObject*
  return [self.dataSource objectForKey:INVITED_PROP];
}

然而,当上面的调用对 objectForKey: 进行调用时,抛出了这个异常:

'NSInternalInconsistencyException', reason: 'Key "firstName" has no data.  Call fetchIfNeeded before getting its value.'

编辑: 访问传递给 +fetchAllIfNeededInBackground block 回调的 fetchedObjects 数组不会抛出异常,但访问最初传递给 的实际数组strong>+fetchAllIfNeededInBackground 抛出。

在调用解决问题之前调用fetchIfNeeded,但是为什么呢?数据已经在那里了。我是否想念 +fetchAllIfNeededInBackground 因为它没有更新拥有 PFUsers 集合的 PFObject?

最佳答案

我知道发生了什么。让我用代码来解释:

PFQuery *query = [PFQuery queryWithClassName:@"TestClass"];
PFObject *testObj = [query getObjectWithId:@"xWMyZ4YEGZ"];

// an array of PFUser "pointers" (pointers in Parse parlance)
NSLog(@"%@", [testObj objectForKey:@"userArrayProp"]);
[PFObject fetchAll:[testObj objectForKey:@"userArrayProp"]];

// now userArrayProp contains fully fetched PFObjects
NSLog(@"%@", [testObj objectForKey:@"userArrayProp"]);

对我来说,一段时间后 userArrayProp 会恢复为“指针”数组,这让我很困惑。我的问题是在 PFObject 上调用刷新会将获取的数组 BACK 恢复为指针数组。像这样:

PFQuery *query = [PFQuery queryWithClassName:@"TestClass"];
PFObject *testObj = [query getObjectWithId:@"xWMyZ4YEGZ"];

// an array of PFUser "pointers" (pointers in Parse parlance)
NSLog(@"%@", [testObj objectForKey:@"userArrayProp"]);
[PFObject fetchAll:[testObj objectForKey:@"userArrayProp"]];

// now userArrayProp contains fully fetched PFObjects
NSLog(@"%@", [testObj objectForKey:@"userArrayProp"]);

[testObj refresh];

// now userArrayProp contains pointers again :(
NSLog(@"%@", [testObj objectForKey:@"userArrayProp"]);

希望它说 [PFObject refresh] 在文档中做到了....

关于objective-c - PFUser 在 fetchAllIfNeeded 之后调用 objectForKey 时抛出 NSInternalInconsistencyException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10274913/

相关文章:

android - Airwatch 目录和 Airwatch Tunnen URL 方案?

iOS CorePlot 点转换

iphone - 当用户从日期选择器中选择日期时,如何在下拉列表中获取特定的星期几?

ios - 正确的保存日期和值(value)的结构

objective-c - NSClassFromString() 安全问题

iphone - MapView:使用MKUserTrackingBarButtonItem切换 map 跟踪状态

ios - 在没有打开版本控制的情况下更改核心数据模型

ios - UIViewAnimation setAnimationDidStopSelector 未声明的选择器,会崩溃吗?

ios - 如何在 UIViewController 中与 UITableView 进行交互?

iphone - iOS 5 SDK 以不同方式对待 UIView