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.
Related
I want to load friends with Parse, but remove currentUser Object, how can I make it ? What am I making wrong ?
I tried with this code, but it doesn't work :
- (void)viewDidLoad
{
[super viewDidLoad];
self.currentUser = [PFUser currentUser];
PFQuery *query = [PFUser query];
[query orderByAscending:#"name"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSLog(#"Error: %# %#", error, [error userInfo]);
}
else {
self.allUsers = objects;
self.mutableAllUsers = [[NSMutableArray alloc] initWithArray:self.allUsers];
[self.mutableAllUsers removeObject:self.currentUser];
[self.tableView performSelectorOnMainThread:#selector(reloadData) withObject:nil waitUntilDone:NO];
NSLog(#"%#", self.mutableAllUsers);
}
}];
Find it :
add [query whereKey:#"objectId" notEqualTo:self.currentUser.objectId]; in query. :)
Try something like:
[query whereKey:#"username" notEqualTo:currentUser.username]
That should work :)
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]);
}
}];
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
}
}];
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.
I am doing a query in parse to check friend request. The query returns objects and I could log them. Now when I convert it to PFUSER, and NSLog, it gets crashed with error: 'Key "FirstName" has no data. Call fetchIfNeeded before getting its value.' Please help me out here.
PFQuery * friendQuery = [PFQuery queryWithClassName:#"friendship"];
[friendQuery whereKey:#"toUser" equalTo:[PFUser currentUser]];
[friendQuery whereKey:#"status" equalTo:#"Pending"];
[friendQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error)
{
//NSLog error
}
else {
NSLog(#"friendRequestCount = %d", objects.count);
PFObject *object=[objects objectAtIndex:0];
NSLog(#"%#", object);
self.friendRequestArray= objects;
NSLog(#"%#",object[#"toUser"]);
PFUser *user1= object[#"toUser"];
NSString *friendName = [NSString stringWithFormat:#"%# %#",user1[#"FirstName"], user1[#"LastName"] ];
NSLog(#"name= %#",friendName);
}
}];
You can use 1 or 2, I have comment the below code, not test.
PFQuery * friendQuery = [PFQuery queryWithClassName:#"friendship"];
[friendQuery whereKey:#"toUser" equalTo:[PFUser currentUser]];
[friendQuery whereKey:#"status" equalTo:#"Pending"];
//1
[friendQuery includeKey:#"toUser"];
[friendQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error)
{
//NSLog error
}
else {
NSLog(#"friendRequestCount = %d", objects.count);
PFObject *object=[objects objectAtIndex:0];
NSLog(#"%#", object);
self.friendRequestArray= objects;
NSLog(#"%#",object[#"toUser"]);
PFUser *user1= object[#"toUser"];
//2 Change PFUser to PFObject
PFObject *user1 = object[#"toUser"];
[user1 fetchIfNeeded];
NSString *friendName = [NSString stringWithFormat:#"%# %#",user1[#"FirstName"], user1[#"LastName"] ];
NSLog(#"name= %#",friendName);
}
}];