Retrieving data from parse.com and showing them - ios

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

Related

parse - edit array in parse user class

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];
}
}];
}

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
}
}];

Displaying Username - Parse

I have looked ALL over the internet and found nothing, Please help me. Im trying to figure out how I can display a username from parse in my table view (detailTextLabel). When I do the code below my app only shows the PFUser Id not the username. It will display: <PFUser:H2AhEbYGal:(null)> { but I'm trying to display the username of that post.
Heres me saving the object:
PFUser *user = [PFUser currentUser];
PFObject *quoteNew = [PFObject objectWithClassName:#"New"];
quoteNew[#"author"] = user;
[quoteNew setObject:[[self quoteText] text] forKey:#"quoteText"];
Here is me trying to retrieve the user in a PFQuery:
PFQuery *query = [PFQuery queryWithClassName:#"New"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// The find succeeded. The first 100 objects are available in objects'
[query includeKey:#"author"];
// PFObject *testObject = [query findObjects];
// PFUser *testUser = [testObject objectForKey:#"author"];
// NSLog(#"username: %#",testUser.username);
} else {
// Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
Here is me trying to display it:
cell.detailTextLabel.text = [NSString stringWithFormat:#"%#: %#",[object objectForKey:#"author"],[dateFormat stringFromDate:updated]];
You can just worry about [object objectForKey:#"author"] in that cell.detailTextLabel.text code.
This:
PFQuery *query = [PFQuery queryWithClassName:#"New"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// The find succeeded. The first 100 objects are available in objects'
[query includeKey:#"author"];
// PFObject *testObject = [query findObjects];
// PFUser *testUser = [testObject objectForKey:#"author"];
// NSLog(#"username: %#",testUser.username);
} else {
// Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
Should be:
PFQuery *query = [PFQuery queryWithClassName:#"New"];
// BEFORE WE QUERY
[query includeKey:#"author"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// Get your objects here
} else {
// Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
And this:
cell.detailTextLabel.text = [NSString stringWithFormat:#"%#: %#",[object objectForKey:#"author"],[dateFormat stringFromDate:updated]];
Should be:
PFUser *userToDisplay = object[#"author"];
cell.detailTextLabel.text = [NSString stringWithFormat:#"%#: %#",userToDisplay.username,[dateFormat stringFromDate:updated]];
Because, as it stands, problem 1 is you're saying to include the author AFTER you have already queried parse. Problem 2 is that you're trying to display the whole user, you need to get the user, and then print the username value.

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