This is using Parse btw.
I have information saved into the users class using this code here
-(IBAction)saveButtonAction:(id)sender {
PFUser *currentUserSave = [PFUser currentUser];
userBioString = userBio.text;
genderFieldString = genderField.text;
ageFieldString = ageField.text;
currentUserSave[#"userBioParse"] = userBioString;
currentUserSave[#"userGenderParse"] = genderFieldString;
currentUserSave[#"ageFieldParse"] = ageFieldString;
[[PFUser currentUser] saveInBackground];
[[PFUser currentUser] fetch];
Now I am trying to call back the information using a query. I researched how to do it and this is my result as of now.
-(IBAction) userProfileQuery {
NSString *bioQueryString = [[PFUser currentUser] objectForKey:#"userBioParse"];
userBio.text = bioQueryString;
NSString *ageQueryString = [[PFUser currentUser] objectForKey:#"ageFieldParse"];
ageField.text = ageQueryString;
NSString *genderQueryString = [[PFUser currentUser] objectForKey:#"genderFieldParse"];
genderField.text = genderQueryString;
the information is being saved into the users class successfully. I am just unsure how to retrieve it... Thanks in advance for any feedback!
There are more than one way to get the currentUsers information in parse. Try this code out. Its the most complicated one, but reading the code its easier to understand what you are doning.
PFQuery *query= [PFUser query];
[query whereKey:#"username" equalTo:[[PFUser currentUser]username]];
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error){
if (!error) {
userBio.text = [object valueForKey:#"userBioParse"];
ageField.text = [object valueForKey:#"ageFieldParse"];
genderField.text = [object valueForKey:#"genderFieldParse"];
}
else {
// Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
If you are doing it the way you are doing it, try, thats a litte bit of a shortcut:
PFUser *user = [[PFUser currentUser];
userBio.text = user.userBioParse;
Parse already gives you most of the code for currentUser. And the fastest way to get currentUser information that parse offers is:
userBio.text = currentUser.userBioParse;
Related
I have a Follower class where stores all following/follower relations of my app. However, when I was trying to retrieve the pointer of each PFUser, I can't get the attributes of each PFUser. Here is my code
PFQuery *query = [PFQuery queryWithClassName:#"Follower"];
[query whereKey:#"from" equalTo:[PFUser currentUser]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
{
if (!error)
{
for(PFObject *o in objects)
{
PFUser *otherUser = (PFUser *)[o objectForKey:#"to"];
NSLog(#"%#",otherUser);
NSLog(#"%#",otherUser.username);
NSString *nickName = otherUser[#"nickName"];
cell.friendNameLabel.text = nickName;
NSLog(#"%#", otherUser[#"nickName"]);
//cell.friendUsrnameLabel.text = otherUser.username;
PFFile *imgFile = otherUser[#"profilePhoto"];
profileView.file = imgFile;
[profileView loadInBackground];
}
}
else
{
NSLog(#"error");
}
}];
when I tried to print every user, here is what i got from the console:
<PFUser: 0x7fc102ec9f70, objectId: GiOIGiNHjK, localId: (null)> {
}
so it didn't find the attributes of each user. Anyone knows the solution? Thanks.
Try to add this line before executing the query.
[query includeKey:#"to"];
I am using the parse framework and would like to know how I can query a column that is located in the PFUser table.
Here is some example code:
//Adds athlete_id column to roster table
PFObject *roster = [PFObject objectWithClassName:#"Roster"];
roster[#"athlete_id"] = answer;
[roster save];
//Adds the rosters objectId to an array (athlete_id) in the User table.
PFUser *currentUser = [PFUser currentUser];
[currentUser addObject:roster.objectId forKey:#"athlete_id"];
[currentUser saveInBackground];
With the above code end up getting an array of objectsID's within the User class in a column named "athlete_id".
Im having a problem actually retrieving this array from the User class. Here is how I am attempting to get the array from the user:
FQuery *query = [PFUser query];;
[query whereKey:#"username" equalTo:[PFUser currentUser].username];
[query whereKeyExists:#"athelete_id"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSLog(#"athlete %#", objects);
} else {
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
I want to grab the array that is contained in athlete_id column for the current user, but the objects array is empty on this query.
You don't need a query.
Just do this:
NSString *athleteId = [[PFUser currentUser] objectForKey:#"athelete_id"];
NSLog(#"The athlete id is %#", athleteId);
So this is my first time working with parse I have simple application which creates a user and allows them to sign in. I'm currently working on something that will allow them to fill in details about themselves using PFObjects, I don't have a problem with that. My issue is I need to get user specific data print out on an UILabel.
Here's my code creating a PFObject this works fine:
- (IBAction)saveProfile:(id)sender {
PFObject *profile = [PFObject objectWithClassName:#"Profile"];
[profile setObject: self.name.text forKey:#"name"];
[profile setObject:[PFUser currentUser] forKey:#"author"];
[profile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
NSLog(#"No Error");
}else NSLog(#"Yeah you got an error bro");
}];
}
Here's what I'm having an issue with my PFQuery:
PFQuery *query = [PFQuery queryWithClassName:#"Profile"];
[query whereKey:#"name" equalTo:[PFUser currentUser]];
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
if (!error) {
NSLog(#"Success");
self.nameLabel.text = [NSString stringWithFormat:#"%#", query];
}
else {
NSLog(#"Fail");
}
}];
}
So basicly I want the user to enter their name have it save, and have that specific user's name print out on a label. This is as far as I got so, if you have any suggestions I'm all ears. Thanks!
Updated:
PFQuery *query = [PFQuery queryWithClassName:#"Profile"];
[query whereKey:#"author" equalTo:[PFUser currentUser]];
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
if (!error) {
NSLog(#"Success");
self.nameLabel.text = object[#"name"];
}
else {
NSLog(#"Fails");
}
}];
}
Looks like you're setting the PFObject "name" key to self.name.text, but in your PFQuery you're querying the class and asking for values where "name" is equal to [PFUser currentUser]. You're setting the value one way then using a completely different value in an attempt to query the object; so getFirstObjectInBackgroundWithBlock isn't returning an object since there's no Profile object where "name" equals [PFUser currentUser].
I think you're confusing your "name" and "author" properties...
Edit (in response to your comment):
OK, so in saveProfile: you're creating a PFObject where you're setting "name" to the name string and "author" to the user's PFUser object. When you're using whereKey: to perform a query on this class in an effort to retrieve the object using getFirstObjectInBackgroundWithBlock:, the result returned to you will be the full first PFObject where the object associated with the key is the one specified in the whereKey: criteria. So you don't have to specify which key of the PFObject you want to read before performing getFirstObjectInBackgroundWithBlock:. The query returns the whole object -- name, author, etc.
So in order to access the returned PFObject's "name" within the query block, change:
self.nameLabel.text = [NSString stringWithFormat:#"%#", query];
to (dispatch_aync added to force the label change onto the main thread):
dispatch_async(dispatch_get_main_queue(),^{
self.nameLabel.text = object[#"name"];
});
This line
[query whereKey:#"name" equalTo:[PFUser currentUser]];
only works if the "name" column is a pointer or relation to the User class. If it is the username you're after, you need to use
[query whereKey:#"name" equalTo:[PFUser currentUser][#"username"]];
But why are you querying for the object you just saved?
Your last, updated example should work for your need.
You could fire up a query like the one below
PFQuery *query = [PFQuery queryWithClassName:#"Profile"];
NSString *nameStr = [NSString stringWithFormat:#"%#",[[PFUser currentUser]objectForKey:#"name"]];
[query whereKey:#"name" containsString:nameStr];
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error)
{
if (!error)
{
/*object contains all columns and you need only key to obtain value*/
nameLabel = [NSString stringWithFormat:#"%#",object[#"job"];
.
.
.
}
else
{
NSLog(#"Error: %#", [error localizedDescription]);
}
}];
Now you will have object of current user along with its all details. Also you could do a thing, i.e., At time of user filling up profile details save it in a dictionary as below :
NSDictionary *signupDetail = [NSDictionary dictionaryWithObjectsAndKeys:self.userRegisterTextField.text, #"username",
self.nameTextField.text, #"Name",
[ResponseDict objectForKey:#"sessionToken"] ,#"sessionToken",
[ResponseDict objectForKey:#"objectId"], #"objectId",
nil];
//ResponseDict is dictionary you get in response for successful signup.
Then you could store it using [NSUserDefaults standardUserDefaults] so you will have all info of user at one place and call it wherever needed.
I am using Parse for iOS. https://www.parse.com/docs/downloads/
However, i have faced difficulties for updating the bool value at specified objectID as shown in this picture.
http://tinypic.com/view.php?pic=8y4zt2&s=5
I can add new row by setting like this. But, now, I want to update the bool value at specified objectID. I would like to know how to do.
PFObject *gameScore = [PFObject objectWithClassName:#"GameScore"];
[gameScore setObject:[NSNumber numberWithBool:NO] forKey:#"cheatMode"];
I have found a way to do.
PFQuery *query = [PFQuery queryWithClassName:#"test"];
[query whereKey:#"objectId" equalTo:objectIDForReport];
[query getFirstObjectInBackgroundWithBlock:^(PFObject * reportStatus, NSError *error) {
if (!error) {
// Found UserStats
[reportStatus setObject:[NSNumber numberWithBool:YES] forKey:#"report"];
// Save
[reportStatus saveInBackground];
} else {
// Did not find any UserStats for the current user
NSLog(#"Error: %#", error);
}
}];
I am trying to filter and sort an NSMutableArray containing a list of Chat Messages. I am trying to get the Last Message of a conversation between 2 users. For example if user1 has 2 different conversations with 2 separate users, I want to get the last message of each of those conversations. I am using Parse.com as backend and this how I saved & retrieved the messages.
Saving the Messages
PFUser *user = [[PFUser currentUser] objectForKey:#"displayName"];
PFObject *newMessage = [PFObject objectWithClassName:#"Messages"];
[newMessage setObject:messageStr forKey:#"body"];
[newMessage setObject:self.mySelectedUser forKey:#"toUser"];
[newMessage setObject:user forKey:#"fromUser"];
[newMessage saveInBackground];
myMessageField.text = #"";
[self getTheNewMessages];
[self.myTView reloadData];
Retrieving The Messages
-(void)GetmyNewMessages
{
PFQuery *query1 = [PFQuery queryWithClassName:#"Messages"];
[query1 whereKey:#"toUser" equalTo:[[PFUser currentUser] objectForKey:#"displayName"]];
[query1 whereKeyExists:#"date"];
PFQuery *query2 = [PFQuery queryWithClassName:#"Messages"];
[query2 whereKey:#"fromUser" equalTo:[[PFUser currentUser] objectForKey:#"displayName"]];
[query2 whereKeyExists:#"date"];
PFQuery *query = [PFQuery orQueryWithSubqueries:[NSArray arrayWithObjects:query1, query2, nil]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSLog(#"Successfully retrieved %d chats.", objects.count);
[premessagesList removeAllObjects];
[premessagesList addObject:objects];
} else {
//Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
}
NSLog of the NSMutableArray (premessageList)
( "<Messages:aS8tVIlsHc:(null)> {\n
body = hi;\n fromUser = cnn;\n toUser =FNC;\n
date = \"2012-09-19 02:40:29 +0000\";\n}",
"<Messages:CPCa6VBmf7:(null)> {\n
body = hello;\n fromUser = FNC;\n toUser =cnn;\n
date = \"2012-09-20 05:06:05 +0000\";\n}",
"<Messages:Jz1cILt18Y:(null)> {\n
body = whatsgood;\n fromUser = sleepy;\n toUser =cnn;\n
date =\"2012-09-20 05:06:05 +0000\";\n}",
"<Messages:lXretmE1uK:(null)> {\n
body = lol;\n fromUser = cnn;\n toUser =sleepy;\n
date =\"2012-09-20 05:13:16 +0000\";\n}",
I tried to use NSSortDescriptor and NSPredicate but I got an empty tableview.
In this type of problem, I would use some variables and fast iteration to slog through the info and get what you need:
id lastChat = ni;
... temps
for(OneChat in allChats) {
do tests and set variables
}
Now I know the last chat.
PS: I decided not to post this, but when I saw after a long time no one had responded felt it was better than nothing. This is how I would solve this.
You could limit the results and sort through the PFQuery itself like this:
[query1 orderByDescending:#"createdAt"];
query1.limit = 1;
[query2 orderByDescending:#"createdAt"];
query2.limit = 1;