PFUser Not returning objectID value on iOS - 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

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

Retrieve data from one column in one row

I have a class (table) in Parse.com core data called users with username, name, surname, email and password.
I want to retrieve, for example, only the name column but I can't make this work. Here is the code:
PFQuery *query = [PFQuery queryWithClassName:#"user"]; //1
[query whereKey:#"email" equalTo:_labelEmail.text];//2
[query findObjectsInBackgroundWithBlock:^(NSArray *userData, NSError *error) {//4
if (!error) {
NSLog(#"%#", usuario);
} else {
NSString *errorString = [[error userInfo] objectForKey:#"error"];
NSLog(#"Error: %#", errorString);
}
}];
I know userData is an array not a dictionary but then how can I retrieve only the information I want? I did try to pull the userData as a dictionary instead of an array but the query does not let me do that.
What I want to do is something like:
NSLog(#"%#", userData["name"])
First I would check if your returned array is empty. If it is then no matter what you do you are not going to get the information you need out of the query.
If you are getting an error you should probably think about your query class. You are trying to query on the class "user" whereas I'm guessing you would like to query the built in _User class. To do that you would use a different kind of query:
PFQuery *query = [PFUser query];
Try this query type that Parse gives for this exact purpose and see if your returned data has any valid data. If it does you can either use:
PFUser *user = [userData objectAtIndex:0];
if you know that there should be only one user object returned to you, or you can use:
for(PFUser *user in userData)
{
//Do something with the user object that got returned to you
}

PFQuery to find PFUser whose key matches NSString

I first of all apologize for the most random rambling title ever. Here is my situation. I populate a UITableView by getting a list of all the Facebook friends a user has that also use the app. I get this by:
[FBRequestConnection startForMyFriendsWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
// result will contain an array with your user's friends in the "data" key
self.friendObjects = [result objectForKey:#"data"];
self.jobsTemp = [[NSMutableArray alloc] initWithCapacity:self.friendObjects.count];
NSLog(#"%#", self.friendObjects);
for(NSDictionary *jobsInfo in self.friendObjects) {
FriendArray *jobby = [[FriendArray alloc] init];
jobby.name = [jobsInfo valueForKey:#"name"];
NSLog(#"Name%#", jobby.name);
// jobby.name = jobsInfo[#"additional"];
jobby.facebookid = jobsInfo[#"id"];
[self.jobsTemp addObject:jobby];
}
self.jobsArray = self.jobsTemp;
NSLog(#"ARRAY%#", self.jobsArray);//set #property (nonatomic, copy) NSArray *jobsArray; in the .h
[self.tableView reloadData];
}
}];
Each entry in _User has a column called fbId. What I would like to do is search all PFUsers when a row is clicked and find the PFUser that has a fbId entry that matches the id for that particular row. What I have tried so far is:
FriendArray *job = self.jobsArray[indexPath.row];
PFObject *object = [PFObject objectWithClassName:#"_User"];
PFQuery *query = [PFUser query];
[query whereKey:job.facebookid equalTo:object[#"fbId"]];
NSLog(#"Query%#", [query findObjects]);
However, I cannot figure out what I am doing wrong. Any suggestions?
You will need to create two PFQuery objects. One to represent the first user, the second to find the friend.
The Parse API query method you are looking for is:
- (instancetype)whereKey:(NSString *)key matchesKey:(NSString *)otherKey inQuery:(PFQuery *)query
You will do something like:
[friendQuery whereKey:#"id" matchesKey:#"fbId" inQuery:userQuery]

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

IncrementKey of Parse api not incrementing

I have a problem with incrementKey. I'd like to increment two keys of my user class, and they're not incrementing. Here's my code, if you want more details feel free to ask.
- (void) updateUserNbrQuestionsAnswered: (NSString*)FacebookID;
{
NSLog(#"%#", FacebookID);
PFQuery *queryUser = [PFQuery queryWithClassName:#"User"];
[queryUser whereKey:#"FacebookID" equalTo:FacebookID];
PFObject *user = [queryUser getFirstObject];
[user incrementKey:#"NbrQuestionsAnswered" byAmount:[NSNumber numberWithInt:1]];
[user incrementKey:#"Points" byAmount:[NSNumber numberWithInt:5]];
[user save];
};
I call this function every time the user hits a button, which reload the view after incrementing on parse and updating a label. Thanks in advance for your help !
Use this:
PFQuery *queryUser = [PFUser query]
Change the save method with
PFObject* user;
...
..
.
NSError* anyErr = NULL;
[user save:&anyErr];
Ensure you have the write permission to alter the values of the User table (see ACL)

Resources