我的 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/