parse - edit array in parse user class - ios

I have a little problem with loading and sending data to parse.com.
I want to read an Array from parse User Class, adding it and send it back to parse.com.
The following code shows how it would work for a "normal" Class to send an array:
PFQuery *query = [PFQuery queryWithClassName:#"myOwnClass"];
[query getObjectInBackgroundWithId:#"myObjectID"
block:^(PFObject *gameScore, NSError *error) {
[gameScore addUniqueObjectsFromArray:#[#"Name1", #"Name1"] forKey:#"Friends"];
[gameScore saveInBackground]; }];

To Get The Array...Change your PFQuery Method:
-(void)FetchFromPFUserClass{
PFQuery*query = [PFUser query];
[query getObjectInBackgroundWithId:#"myObjectID"
block:^(PFObject *gameScore, NSError *error) {
if(error==nil){
[gameScore addUniqueObjectsFromArray:#[#"Name1", #"Name1"] forKey:#"Friends"];
[gameScore saveInBackground];
}
}];
}

Related

Retrieve all guildmembers in the same guild as you using Parse

how do i retrieve all the guildmembers in your guild using parse?
Here is my code:
PFUser *currentuser = [PFUser currentUser];
PFQuery *query = [PFQuery queryWithClassName:#"User"];
[query whereKey:#"connectedGuild" equalTo:currentuser[#"connectedGuild"]];
[query findObjectsInBackgroundWithBlock:^(NSArray *comments, NSError *error) {
NSLog(#"There are %d guildmembers. Error:%#", comments.count, error);
}];
My log:
There are 0 guild members. Error:(null)
connectedGuild is a pointer to a guild class where i store all the guilds.
Queries of the PFUser class must be instantiated a little differently. Try this:
PFUser *currentuser = [PFUser currentUser];
PFQuery *query = [PFUser query];
[query whereKey:#"connectedGuild" equalTo:currentuser[#"connectedGuild"]];
[query findObjectsInBackgroundWithBlock:^(NSArray *comments, NSError *error) {
NSLog(#"There are %d guildmembers. Error:%#", comments.count, error);
}];
For more information, see the Parse.com website here:
https://www.parse.com/questions/get-pfuser-in-pfquery-using-ios-api

Retrieving data from parse.com and showing them

I'm trying to retrieve data from parse.com which i have already sent.
I'm using following code to send the data:
-(IBAction)msgSend:(id)sender {
PFObject *appMsg = [PFObject objectWithClassName:#"appMsg"];
appMsg[#"besked"] = msg.text;
[appMsg saveInBackground];
msg.text = #"";
}
And to get the data I'm trying this:
PFQuery *query = [PFQuery queryWithClassName:#"appMsg"];
[query getObjectInBackgroundWithId:#"*******" block:^(PFObject *appMsg, NSError *error) {
NSString *besked = appMsg[#"besked"];
msgRecieved.text = besked;
}];
At last I'm trying to display the data into a textView.
Im not really loading any data?
PFQuery *query = [PFQuery queryWithClassName:#"tempClass"];
[query orderByDescending:#"createdAt"];
query.limit =10;
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// Retrive values like below,object id is retrieved by objects.objectId
NSLog(#"Object values %#",[objects valueForKey:#"columnName"]);
} else {
// Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];

Fetch objectId from a User in parse

i'm new to parse and i'm trying to fetch a user objectId, but whatever i cant seem to return the objectId. I can easily return the username. this is my query:
PFQuery *query = [PFUser query];
[query whereKey:#"username" equalTo:[[PFUser currentUser] objectForKey:#"username"]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
if (objects.count) {
NSLog(#"%#", [objects lastObject]);
}
}
}];
This returns something like this:
<PFUser:9dcc65tsdr:(null)> {
username = AEleQFdBx9jdtypfsQmLtzAvW;
}
How do i return the objectId which is between PFUser and (null)?
You can use object.objectId like this:
PFQuery *query = [PFUser query];
[query whereKey:#"username" equalTo:[[PFUser currentUser] objectForKey:#"username"]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
if (objects.count) {
for (PFObject *object in objects){
NSLog(#"Object ID: %#", object.objectId);
}
}
}
You can also use this: [[PFUser currentUser]objectId] much faster and works much better because you don't have to run a whole PFQuery.

PFQuery on PFUser returns nil

I'm new to Parse and I'm trying to retrieve a list of friends for my current user, but my result is always nil.
Here's how my data is on the server:
Class User (auto-created when first user signed up using the PFUser class)
Inside the class User I have a field called "array_friends_id" which is an array and it contains multiple objectIds inside. This field already has an array in it with 1 string inside
Here's my code for iOS:
- (IBAction)button_login:(id)sender
{
[PFUser logInWithUsernameInBackground:[textField_username text]
password:[textField_password text]
block:^(PFUser *user, NSError *error)
{
if (!error)
{
NSLog(#"logged in!");
// find friends
PFQuery *query = [PFQuery queryWithClassName:#"Users"];
[query whereKey:#"user" equalTo:[PFUser currentUser]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error)
{
NSLog(#"objects = %#", objects);
}
}];
}
else
{
NSLog(#"can't login. Error = %# %#", error, [error userInfo]);
}
}];
}
My idea is to allow the user to log into the app, and as soon as the user logs in I want to retrieve his list of friends, even before pushing to the next view controller.
What am I doing wrong?
This is how I query in Parse,
PFQuery *query = [PFUser query];
[query orderByAscending:#"username"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSString *errorString = [error userInfo][#"error"];
NSLog(#"Log %#", errorString);
}
else {
// all good
}
}];

What should I put on the whereKey:#"objectId"?

I'm using iOS.
If I insert #"objectId" in the whereKey:#"". I get an error saying: bad special key: objectId.
This is my code:
PFQuery *findFriends = [PFUser query];
[findFriends whereKey:#"objectId" equalTo:friendsID];
[findFriends selectKeys:#[#"firstname",#"lastname"]];
[findFriends findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSLog(#"%#", objects);
}
}];
friendsID is an NSString with the objectId from the user the current user is following.
Thank you!
The problem is that your friendsID is not actually a string, my guess is that it's a PFUser. If this is the case, then one way to do it would be to user this.
PFUser * toUser = [friends[0] objectForKey:#"toUser"];
PFQuery * findFriends = [PFUser query];
[findFriends whereKey:#"objectId" equalTo:toUser.objectId];
[findFriends selectKeys:#[#"firstname",#"lastname"]];
[findFriends findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSLog(#"%#", objects);
}
}];

Resources