1
votes

I have subclassed PFuser and registered it. it works great.

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.

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 file

#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 file

#import "PFCustomUser.h"

@implementation PFCustomUser

@dynamic name,userPhoto,company;


+ (PFCustomUser *)currentUser {
return (PFCustomUser *)[PFUser currentUser];
}

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);

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

2
You know (1) you may just need to add fetchIfNeeded - good example - stackoverflow.com/questions/18429723 (2) HOWEVER, I'm not a fan of fetchIfNeeded, it's kind of "troubling" - it's one of those things you shouldn't have to do, and (3) in fact, just post all your code in question and I'm sure someone can help! - Fattie
Be aware that when you want to go "another table deep" .. in fact, you do just need to make another call. (this is a real "gotchya" with any DB programming, it can be confusing). For example, I get all the "posts" (typical Facebook like app), and there are 107 of them relevant to the user. For each post, I then want all the comments in question. You actually do just have to make {as a rule} 107 calls to "comments" to get those. (More likely, you'd only do that when necessary.) When you're loading just "one" thing, it's perfectly common that you have to make a call to say Users, and then.. - Fattie
.. make ANOTHER call to something like "country information" or whatever; you have to wait for two linked calls. (Wait for one, and start the next one when that finishes, and when that finishes you're done overall.) It could be this is the situation at hand. Anyway the code will reveal all and I'm sure someone will fix it! - Fattie
@JoeBlow added some code... - MichaelAngelo
Hmm .. did you get the parse.com/docs/ios_guide#subclasses-defining/iOS "+ (NSString *)parseClassName;" stuff .. ? - Fattie

2 Answers

0
votes

you have to add a setter & getter for each pointer or relation you have..

.h file

@property (retain) PFRelation *likes;

.m file

@synthesize likes = _likes;

- (void) setLikes:(PFRelation *)likes{
    _likes = likes;
}

- (PFRelation *) likes{
    if(_likes== nil) {
        _likes = [self relationforKey:@"likes"];
    }
    return _likes;
}

sample code:

PFPlace *place = [PFPlace object];
place.name = @"Taiwan";
[person save];
[place save];
[place.likes addObject:person]
[place save];

https://www.parse.com/questions/pfobject-subclassing-with-pfrelation-as-attribute

0
votes

The problem in your code is that you are not fetching the "Company" object when you are using it in currentUser.company.place

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).

To solve this :

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!