带指针的pfuser无法获取数据 [英] pfuser with pointer not getting data back
问题描述
我已经将PFuser子类化并注册了它.它很棒.
I have subclassed PFuser and registered it. it works great.
在数据库中的User表中,我指向另一个具有更多信息的表.
In my database in the User table i made a pointer to another table that has some more information.
应用程序不断因以下错误而崩溃:
the app keeps crashing with this error:
2014-06-08 15:14:59.550 App[2333:60b] *** Terminating app due to uncaught exception
'NSInternalInconsistencyException',
reason: 'Key "place" has no data. Call fetchIfNeeded
before getting its value.'
在检查调试器时,我可以看到它与另一个表建立了连接,但是所有字段均为空.
While checking the debugger i can see it made the connection to another table but all fields are empty.
我认为我需要输入以下内容:[query includeKey:@"company"];
指针,但我不查询User类...
I think i need to put something like this: [query includeKey:@"company"];
for the pointer but I don't do a query for the User class...
我需要在某个地方覆盖它吗?
do I need to override it somewhere ?
这是我的自定义用户类:
this is my custom user class:
-h文件
#import "Company.h"
@interface PFCustomUser : PFUser<PFSubclassing>
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) UIImage *userPhoto;
@property (nonatomic, retain) Company *company;
+ (PFCustomUser *)currentUser;
-m文件
#import "PFCustomUser.h"
@implementation PFCustomUser
@dynamic name,userPhoto,company;
+ (PFCustomUser *)currentUser {
return (PFCustomUser *)[PFUser currentUser];
}
在appdelegate中,我这样做
In the appdelegate i do this
[PFCustomUser registerSubclass];
所以我会在控制器中做到这一点
So technacly i would do this in a controller
PFCustomUser *currentUser = [PFCustomUser currentUser];
NSLog(@"%@",currentUser.company.place);
公司为零,所以地点为零.因此错误. 在调试器中,您可以看到它看到公司的objectId和类名,但其余的都为空
company is nill so place is nill. hence the error.. In the debugger you can see it sees the objectId of company and the classname but the rest is nill
推荐答案
代码中的问题是,当您在currentUser.company中使用"Company"对象时,您未获取该对象.地方
The problem in your code is that you are not fetching the "Company" object when you are using it in currentUser.company.place
当您获取currentUser时,它仅获取有关其Parse类Type(在这种情况下为"Company"类)的属性的基本信息,例如objectId等.这就是为什么您在调试器中获得company的objetId,而currentUser.company.place为nil的原因(因为尚未提取实际对象).
When u fetched currentUser, it only fetches the basic info like objectId,etc about its properties of Parse class Type ('Company' class in this case). That is why u are getting the objetId of company in debugger whereas currentUser.company.place is nil(because the actual object isn't fetched yet).
要解决此问题:
您必须使用"fetchIfNeeded"方法分别获取Parse类类型的每个属性(无论您希望在何处实现它->您的选择).
You have to fetch each property of Parse class type individually (however and wherever you want to implement it->ur choice) using 'fetchIfNeeded' method.
所以您的代码可能看起来像这样:
So ur code could look something like this :
PFCustomUser *currentUser = [PFCustomUser currentUser];
Company *company = currentUser.company;
[company fetchIfNeededInBackgroundWithBlock:^(PFObject * _Nullable object, NSError * _Nullable error) {
//Handle error here
}
else
{
//company.place can be fetched here
NSLog(@"%@",company.place);
}
}];
希望这行得通.希望对您有所帮助!
Hope this works. And hope it helps!
这篇关于带指针的pfuser无法获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!