Getting username for PFObject - ios

I have a PFObject class where each object has a user attached to it. I can call each object and display it in a tableview, however when I try to call the associated PFUser to each object it just crashes.
PFObject *owner = [self.objectArray objectAtIndex:indexPath.row];
NSString *ownerString = [owner objectForKey:#"user"];
cell.detailTextLabel.text = ownerString;
Is there another way I need to call the user to get the username? In the NSLog the user looks like this: user = \"<PFUser:dDeHnc33EE>\";\n

PFUser is not an NSString, it's a subclass of PFObject which again is a subclass of NSObject. PFUser has a property called username which returns a string. Do this:
PFUser *user = [owner objectForKey:#"user"];
NSString *username = user.username;
to get the username of the user. Check of the PFUser documentation here.

Related

objectForKey not working for pointer

In the docs https://www.parse.com/docs/ios/guide#relations-using-pointers
it says that in the example provided you can find the User who created the game
// say we have a Game object
PFObject *game = ...
// getting the user who created the Game
PFUser *createdBy = [game objectForKey:#"createdBy"];
But when I use the exact syntax since I want to populate the pointer in my "user" column
PFUser *user = [PFUser currentUser];
NSString *username = user.username;
// Inside my queryForTable method so PFObject is returned in
// tableView:cellForRowAtIndexPath:object
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"fromUser = %#", user];
PFQuery *query = [PFQuery queryWithClassName:#"Activities" predicate:predicate];
// Inside tableView:cellForRowAtIndexPath:object
PFUser *createdBy = [object objectForKey:#"user"];
NSLog(#"User to user ---%#", createdBy);
But All I get back is
User to user ---<PFUser: 0x7ff9024da860, objectId: aOisP569e3, localId: (null)> {
// Nothing here
}
If I'm understanding correctly, am I also supposed to get back username, email etc in my user object?
---UPDATE 1---
looking at the Anypic app provided by parse it should return something like this
<PFUser: 0x7f7ff9fafc00, objectId: LfADtx1K2J, localId: (null)> {
// Stuff appears here
displayName = "poopiu";
facebookId = 130941340571204;
profilePictureMedium = "<PFFile: 0x7f7ff9faf500>";
profilePictureSmall = "<PFFile: 0x7f7ff9faf7f0>";
username = I34MBM3WYSB5tjWIIvUvhH5fq;
}
but mine is empty even though I have a column called username that isn't undefined so I should get that inside my PFUser object
--UPDATE 2--
Here's what I get back from logging object like so...
NSLog(#"Object---%#", object);
<Activities: 0x7fbbebf42d20, objectId: rDwYI5Inuk, localId: (null)> {
user = "<PFUser: 0x7fbbee1367e0, objectId: SFL0kVZ17x>";
status = 0;
>
Add the following method call after you instantiated your query.
[query includeKey: "user"];
By default, queries do not grab information past the immediate object that was queried.
Are you sure your user isn't nil? According to the docs you must be logged in for currentUser to return a user object.
[createdBy fetch];
NSLog(#"User to user ---%#", createdBy);
Try this hope this will help

PFUser Not returning objectID value on iOS

I am trying to get the objectId key from a PFUser using Parse.com, but it keeps returning null.
PFUser *me = [PFUser currentUser];
NSString *theObject = me[#"objectId"];
NSLog(#"Return %#", theObject);
Every time I run this, it comes back as "Return null". I can put username in where I type objectId, and will get a result, but not for objectId.
Try:
NSString *theObject = [me objectId];
From:
Getting objectId from parse.com

Why is a PFRelation saved by saving the PFUser object?

so I am having trouble understanding how a PFRelation saves, I have this code:
PFUser *user = [PFUser currentUser];
PFRelation *relation = [user relationForKey:#"likes"];
[relation addObject:post];
[user saveInBackground];
Why is it that updating the user i.e. [user saveInBackground] updates the PFRelation field "likes" for that user? Does this use a single API call or does [relation addObject: post]; also require an API call?
Any help would be greatly appreciated.
Thanks
In your case 1 API request is used under circumstances.
Take a look at PFObject [straight from Parse.com]:
// Create the post
PFObject *myPost = [PFObject objectWithClassName:#"Post"];
myPost[#"title"] = #"I'm Hungry";
myPost[#"content"] = #"Where should we go for lunch?";
// Create the comment
PFObject *myComment = [PFObject objectWithClassName:#"Comment"];
myComment[#"content"] = #"Let's do Sushirrito.";
// Add a relation between the Post and Comment
myComment[#"parent"] = myPost;
Here you are setting attributes or properties to the PFObject but nothing ever happens until you save it, you can do anything to the object like change it, update it, doesn't matter, but backend wise, it won't update unless you tell it to which is where save comes in play :
[myComment saveInBackground];
In short, you can add relations, pointers and numerous parameters all day long, but nothing happens until you tell it to happen : [saveInBackground];
Because you made it a direct correlation to user it saves it to that user because you told it to. Because you specified a relation to the user, once you save the user properties the relation will also be saved. However, this doesn't create more API requests.

Cannot save User class objects in background

- (IBAction)savePasswordButton:(id)sender {
PFObject *myPassword = [PFObject objectWithClassName:#"_User"];
myPassword[#"password"] = self.myPasswordfield.text;
[myPassword saveInBackground];
}
The problem is here: For the _User class I can't see anything in the databrowser, but when I try with other classes it works successfully
Does anyone could explain me where am I wrong ?
According to Parse documentation, 'User' is a built-in class.
PFUser *user = [PFUser currentUser];
user.password = self.myPasswordfield.text;
[user saveInBackground];

Null output for objectId in parse.com ios

I am trying to get objectId of babyInfo Class and want to link it to the User class of parse.com. But null objectId is being returned in all of these cases.
PFObject *babyInfo = [PFObject objectWithClassName:#"BabyInfo"];
babyInfo[#"babyname"] = babyname;
babyInfo[#"gender"] = gender;
babyInfo[#"dob"] = date;
NSLog(#"Object ID: %#", babyInfo.objectId); //First method tried unsuccessfully
[babyInfo saveInBackground];
NSString *objectid=[babyInfo objectId];
NSLog(#"%#",objectid); // Second method tried unsuccessfully
NSString *objectid= [babyInfo valueForKey:#"objectId"];
NSLog(#"%#",objectid); // Third method tried unsuccessfully
I think this is happening because you are using saveInBackground, so the log statements are logging null as the object hasn't saved yet. Also, your first log statement will definitely log null because it comes before the save statement. Try this:
PFObject *babyInfo = [PFObject objectWithClassName:#"BabyInfo"];
babyInfo[#"babyname"] = babyname;
babyInfo[#"gender"] = gender;
babyInfo[#"dob"] = date;
[babyInfo save];
NSString *objectid=[babyInfo objectId];
NSLog(#"%#",objectid);

Resources