PFQuery on PFUser returns nil - ios

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

Related

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.

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.

Mix two PFQuery results

I want to have an mix of two PFQuery results. In my code below, I'm getting the results of the intersection of the two results (so all users who's username AND fullName contain self.searchText), I want to to include all the results (so all users who's username equals self.searchText and all users who's fullName equals self.searchText).
PFQuery *query = [PFUser query];
[query whereKey:#"username" containsString:self.searchText];
[query whereKey:#"fullName" containsString:self.searchText];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSLog(#"%# %#", error, [error userInfo]);
} else {
self.searchResults = [[NSMutableArray alloc] initWithArray:objects];
NSLog(#"%#", objects);
[self.tableView reloadData];
}
}];
What you are trying to do is get the Union (OR) of the two sets not their intersection (AND)
use the following code should bring you all users that match either of those criterias
PFQuery *firstQuery = [PFUser query];
[firstQuery whereKey:#"username" containsString:self.searchText];
PFQuery *secondQuery = [PFUser query];
[secondQuery whereKey:#"fullName" containsString:self.searchText];
PFQuery *query = [PFQuery orQueryWithSubqueries:#[firstQuery ,secondQuery ]];
[query findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {
if (error) {
NSLog(#"%# %#", error, [error userInfo]);
} else {
self.searchResults = [[NSMutableArray alloc] initWithArray:objects];
NSLog(#"%#", objects);
[self.tableView reloadData];
}
}];
and here is the link to the guide
https://parse.com/docs/ios_guide#queries-compound/iOS
When you say
so all users who's username equals self.searchText and all users who's fullName equals self.searchText
what you really mean is OR, instead of AND (because AND is what your current code does).
To create an OR relationship you will need to create 2 different queries and then use orQueryWithSubqueries: to combine them.

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