I'd like to grab a NSString from the Parse.com and paste it into a Label in my iOS App.
Does anyone know how to do so ?
I'm having massive problems with it :/
Thanks in advance
The counter-question is "Which NSString do you want to get"? I assume it's some property of a PFObject that you want to display, right?
What makes this property so interesting?
Let's say you want to display the title of a Book whose author is Hemmingway.
First you want to find the object:
PFQuery *query = [PFQuery queryWithClassName:#"Book"]; // I want to find a book
[query whereKey:#"author" equalTo:#"Hemmingway"]; // authored by hemmingway
PFObject *book = [query findFirstObject]; // go get it!
Then you just read its properties with objectForKey
myLabel.text = [book objectForKey:#"title"];
Check out the iOS guides for more advanced questions. If you plan to display many properties of many PFObjects, you might want to check out the PFQueryTableViewController
Related
I am performing a simple PFQuery to fetch a bunch of Box objects (class name).
My Box has a pointer to a Toy object called toy.
I let my user select a bunch a toys, then the search only display the Box objects with those Toy objects.
So I end up with an NSArray of PFObjects of type Toy. I have an array of objectId strings for the objects, and I just create another array like this:
PFObject *obj = [PFObject objectWithoutDataWithClassName:#"Toy" objectId:objectId];
Now I can query the object, I would have thought. I have tried doing a query of Box objects with an NSPredicate which looks like this:
[NSPredicate predicateWithFormat:[NSString stringWithFormat:#"toy = %#", toyObject]];
My app crashes and tells me it is unable to parse that. So before adding the predicate I take the objectId instead and try doing that:
[NSPredicate predicateWithFormat:[NSString stringWithFormat:#"toy.objectId = %#", toyObject.objectId]];
However, it doesn't like that format either. So how can I create an NSPredicate that lets me only fetch objects with a specific pointer result like explained.
In other words just use
PFUser *user = [PFUser currentUser];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"fromUser = %#", user];
where in this example, "user" is a pointer stored in the table and we're searching for it under the "fromUser" column
I think you're making this too hard on yourself. If I'm reading your question correctly, there is a much simpler way to do this, and you don't have to use NSPredicate at all:
NSMutableArray *toys = [NSMutableArray array];
// Figure out some way (during selection) to get the "toy" objects into the array
PFQuery *query = [PFQuery queryWithClassName:#"Box"];
[query whereKey:#"toy" containedIn:toys];
[query findObjects... // You should know the rest here
And that's it! Nice and easy, should find all Box instances that have a toy that is contained in the toys array.
Turns out it was pretty simple.
Don't use [NSString stringWithFormat:(NSString *)] if you are creating an NSPredicate format string. NSPredicate really doesn't like it and can often fail to parse your format.
I want to retrieve an array from Parse.com. The array is saved in the user class in Parse, and I would like to retrieve only the array linked to the current user.
I know this code is not valid, but I think it illustrates exactly what I want:
NSArray *array = [[PFUser currentUser] arrayForKey:#"arrayKey"];
I know this code snippet might seem stupid for some of you, but I hope you get what I'm trying to do.
PFUser* currentUser = [PFUser currentUser];
NSArray* myArray = currentUser[#"arrayKey"];
Description = I have a project that allows users to post things. These posts will be displayed in a tableView, (the comments will not be shown in this tableView). When a user presses a post, they will segue to a screen where they can post a comment. In the tableViewCell there will be a label like "(0) comments" to display how many comments belong to the post.
2 Queries happen. One query to populate the tableView, and another to query the amount of comments from each post at each indexPath.
Desired Result = If there are 5 comments on a post, to be able to return how many comments where made on each post so I can .count them into a string to say, "(5) Comments".
Problem = I'm not sure how to query for the results of a query, or if that is even what is supposed to happen. I'm not even sure if this is going right, please help someone who is knowledgable. I attached what I have done (not a copy and paste i typed all of that [so if something isn't right or there was a typo, please do not respond telling me that that was the problem with the code]).
What I have done =
-viewDidLoad...
PFQuery *postQuery = [PFQuery queryWithClassName:#"Post"];
[userQuery whereKey:#"postedByID" equalTo:selectedUserID];
[userQuery orderByDescending:#"createdAt"];
[userQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
tableArray = objects;
[self.tableView reloadData];
}];
-cellForRowAtIndexPath...
PFObject *post = [tableArray objectAtIndex:indexPath.row];
PFFile *imgFile = post[#"postPicture"];
[imgFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
cell.picture.image = [UIImage imageWithData:data];
}];
NSString *dateAndTime = [NSString stringWithFormat:#"%# %#", post[#"postDate"],post[#"postTime"]];
cell.categoryLabel.text = post[#"category"];
cell.subCategoryLabel.text = post[#"subCategory"];
cell.dateLabel.text = dateAndTime;
cell.descriptionLabel.text = post[#"description"]];
//below is where my headaches are coming from
PFQuery *commentsQuery = [PFQuery queryWithClassName:#"Comment"];
//WHAT DO I DO HERE? (this probably isn't right, but I feel like it should be)
[commentsQuery whereKey:#"parentID" containedIn:[tableArray objectAtIndexPath:indexPath.row][#"objectId"];
[commentsQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
NSLog(#"%#", objects);
}];
This returns an empty log.
I know for a fact that there are multiple comments in relation to some of the posts, yet still returns nothing. I believe this is because my commentsQuery isn't right, or something. I dunno, please help someone.
If I understand correctly your case, you should try with:
[commentsQuery whereKey:#"parentID" equalTo:[tableArray objectAtIndexPath:indexPath.row][#"objectId"];
since you are querying all of the comments belonging to a specific post.
containedIn would allow you to get all of the comments belonging to a given set of posts.
You should consider changing your design, as counting the comments for every post is extremely inefficient.
Take a look at the documentation on Cloud Code that runs after save. It specifically talks about a comments counter on a Post class, then you get the count as a simple property when you query the Post.
It is OK to make writes (that are less frequent) a little more expensive, to make reads much cheaper.
This supposed to be a simple thing to do, but i`m having a hard time to find this answer.
X-Code / Objective-C:
I have an sheet stored at Parse.com, and I simply want to make a array with one of its columns. For example, I have a spread sheet called Places and I want to build an Array of the column Address of these places, but instead I`m getting an Array of objects. Look!
PFQuery *query = [PFQuery queryWithClassName:#"Locais"];
[query selectKeys:#[#"local"]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
self.colorArray = [NSArray arrayWithObject:objects];
NSLog(#"%#", objects);
}];
The log:
"<Locais:fEQq2qB8dJ:(null)> {\n local = \"Giga CCM/CCS\";\n}",
"<Locais:z3gHNJ7CPw:(null)> {\n local = \"Giga Santander\";\n}",
"<Locais:rNEJUHPXtl:(null)> {\n local = \"Sean Plott\";\n}"
How I want to get the log:
"Giga ccm/css"
"Giga santander"
"Sean Plott"
You can get the array of strings in a single step using Key-Value Coding (KVC),
self.colorArray = [objects valueForKeyPath:#"local"];
You should have a look on Key-Value Coding Programming Guide
You can extract the data from the array like this :
PFObject *localis = objects[1];
NSString *string = [localis objectForKey:#"local"];
This is how you can get the data out of it.
I need to query a large number of friends messages for a user in Parse.
I have a Friend_Relation object with two users, a friend and a current user.
Each user has many Message objects (i am concerned only with the most current). The relation loos like this:
[message setObject:currentUser forKey:#"userMessage"];
The user object has an attribute for the most current message objectId.
How do I get all of a user's friends and their messages?
I was thinking if I could query all of a user's friends that is easy. If I could then create an NSArray of the objectIds that I get from a users friends, I could another single query to get back all of those user's messages. How could I create this type of query on the fly?
You can probably use an inner query to achieve this:
PFQuery *getAllFriendsInnerQuery = [[currentUser relationForKey:#"friend"] query];
PFQuery *messageQuery = [PFQuery queryWithClassName:#"Message"];
[messageQuery whereKey:#"userMessage" matchesQuery:getAllFriendsInnerQuery];
[messageQuery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
// Here the parameter array contains all the messages from current user's friends
}];
The answer is definitely to use the contained in method like so:
NSArray *names = [self.user objectForKey:#"friends"];
[query whereKey:#"userMessaga" containedIn:names];