Reputation: 608
I'm trying to retrieve data from the current User's row and display it in their profile. (Data such as firstName, lastName, email, postalCode etc - Which are all in separate columns). I can retrieve all the data by using:
PFUser *currentUser = [PFUser currentUser];
if (currentUser != nil) {
NSLog(@"Current user: %@", currentUser.username);
PFQuery *query = [PFUser query];
[query whereKey:@"username" equalTo:currentUser.username];
NSArray *data = [query getObjects];
NSLog(@"%@", data);
}
but I don't think I can separate the data by this method. It only displays everything at once. I would like it to assign to separate labels to display firstName, lastName etc.
Upvotes: 0
Views: 1074
Reputation: 595
Whatever method you use to query for the currentUser, you (hopefully) will be returning one PFObject. Since the PFObject is essentially a Dictionary, you just than have all of the data for that user with access to object's Key-Value pairs.
I prefect KVC rather than just calling methods on the currentUser class because it's possible you have custom fields that can't easily be queried for.
Below is my solution to query for currentUser and set up their profile.
ProfileVC.h
@property (nonatomic, strong) PFUser *profileToSet;
ProfileVC.m
-(void)setProfile{
PFQuery *query = [PFUser query];
[query whereKey:@"objectId" equalTo:[[PFUser currentUser]objectId]];
[query findObjectsInBackgroundWithBlock:^(NSArray * objects, NSError * _Nullable error) {
if (error) {
NSLog(@"error: %@",error);
} else {
self.profileToSet = [objects firstObject];
// Do the rest of the setup with the profileToSet PFUser PFObject.
}
}];
Upvotes: 1
Reputation: 9911
What you're doing there is printing out the full array of results from your query, which is why it looks like all the fields are being put together (when they're not really). If you want to retrieve/refresh the data from Parse for the current user there's a better way. Use fetch
.
PFUser *currentUser = [PFUser currentUser];
if (currentUser != nil) {
NSLog(@"Current user: %@", currentUser.username);
[currentUser fetch];
// Now your currentUser will be refreshed...
NSLog(@"First: %@, postcode: %@", currentUser[@"firstName"], currentUser[@"postalCode"]); // Assuming those keys exist.
}
It'll ensure that only the current user is returned, in a simpler way than running a PFQuery
directly. You don't need to check the array length, or grab the first object out.
As noted in comments, you should look at using fetchInBackgroundWithBlock:
as it'll (1) not block and (2) give you an error if something goes wrong.
Upvotes: 0