I am using parse for the backend part of my iPhone App.
We can have a one to many relation in Parse which described in Relational Data.
This code works fine to retrive the data:
PFQuery *query = [PFQuery queryWithClassName:@"Comment"];
PFObject *fetchedComment = [query getObjectWithId:@"0PprArjYi3"];
NSString *content = [fetchedComment objectForKey:@"content"];
printf("%s", [content UTF8String]);
But when I use their codes provided in the Link, it returns null:
PFObject *post = [fetchedComment objectForKey:@"parent"];
[post fetchIfNeededInBackgroundWithBlock:^(PFObject *object, NSError *error) {
title = [post objectForKey:@"title"];
}];
printf("%s", [title UTF8String]); // RETURN NULL
Can anybody tell me what is wrong in this code? The problem could be fetchedcomment.
Addenda
This one also got an exception:
PFQuery *query = [PFQuery queryWithClassName:@"Comment"];
PFObject *fetchedComment = [query getObjectWithId:@"0PprArjYi3"];
PFObject *post = [fetchedComment objectForKey:@"parent"];
NSString *title = [post objectForKey:@"title"];
printf("%s", [title UTF8String]);
The method
fetchIfNeededInBackgroundWithBlockis, as the name suggests, performed in background. Your code asks for a fetch and then immediately continues to theprintf. At that point in time, the data hasn’t been fetched yet, sotitleis probably stillnil.You have two options –
Use a synchronous method, such as [post fetch]: instead of [parent fetchInBackground…].
Print the title inside the block that gets executed when the
fetchInBackground...method finishes.