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"];
Related
so I am having trouble understanding how a PFRelation saves, I have this code:
PFUser *user = [PFUser currentUser];
PFRelation *relation = [user relationForKey:#"likes"];
[relation addObject:post];
[user saveInBackground];
Why is it that updating the user i.e. [user saveInBackground] updates the PFRelation field "likes" for that user? Does this use a single API call or does [relation addObject: post]; also require an API call?
Any help would be greatly appreciated.
Thanks
In your case 1 API request is used under circumstances.
Take a look at PFObject [straight from Parse.com]:
// Create the post
PFObject *myPost = [PFObject objectWithClassName:#"Post"];
myPost[#"title"] = #"I'm Hungry";
myPost[#"content"] = #"Where should we go for lunch?";
// Create the comment
PFObject *myComment = [PFObject objectWithClassName:#"Comment"];
myComment[#"content"] = #"Let's do Sushirrito.";
// Add a relation between the Post and Comment
myComment[#"parent"] = myPost;
Here you are setting attributes or properties to the PFObject but nothing ever happens until you save it, you can do anything to the object like change it, update it, doesn't matter, but backend wise, it won't update unless you tell it to which is where save comes in play :
[myComment saveInBackground];
In short, you can add relations, pointers and numerous parameters all day long, but nothing happens until you tell it to happen : [saveInBackground];
Because you made it a direct correlation to user it saves it to that user because you told it to. Because you specified a relation to the user, once you save the user properties the relation will also be saved. However, this doesn't create more API requests.
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.
in my project I have a user relation for the key userPosts. I want to add to this relation different objects with different class names, in this example a category from a UIPicker, something like this:
PFObject *post = [PFObject objectWithClassName:selectedCategory];
//do stuff with the object...
[object save];
PFRelation *relation = [PFUser currentUser] relationForKey:#"userPosts"];
[relation addObject:post];
[PFUser currentUser] saveInBackground];
The problem is that when I'm trying to execute this code it throws me a runtime error saying basically that I can only add objects to a relation with one class name. How do I override/fix it? Thanks :)
I have an NSMutableArray object that contains NSString objects. The mutable array object is called _usersToAddToFriendsList. When I am done adding NSString objects to it, I run the following code for Parse.com:
[PFUser currentUser];
PFQuery *query = [PFQuery queryWithClassName:#"_User"];
[query whereKey:#"username" equalTo:_userSubmittedUsername];
[query getObjectInBackgroundWithId:_objectId block:^(PFObject *object, NSError *error) {
object[#"friends"] = _usersToAddToFriendsList;
[object saveInBackground];
}];
The most important part is this statement: object[#"friends"] = _usersToAddToFriendsList;
This takes my NSMutable array and places it inside the "friends" column in my Parse.com database. When I created this column in Parse, I set it's "type" to "array".
All of this works perfectly and I can see that the contents of my mutable array have been placed in the "friends" column in the database.
Here's the problem though. Later on, when I query for the "friends" column I use the method call of findObjectsInBackgroundWithBlock and this places the object returned by the server into an NSArray object called "objects".
My problem is that this means that I now have an array called "objects" that contains my original mutable array with my NSStrings.
I need to loop through the string values of my original NSMutableArray, but I don't know how to get to them because my original mutable array is contained inside this new NSArray returned by the server.
I have tried playing around with various multi-dimensional array solutions that people provided in my previous stack overflow question: Need to loop through an array that is inside of another array
But it never works and it always crashes in xcode and says:
-[PFUser countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance
This makes me think that this problem needs a solution specifically tailored to how Parse.com works.
I just want to be able access and loop through the string values that I originally stored in my NSMutableArray.
My problem is that this means that I now have an array called "objects" that contains my original mutable array with my NSStrings.
Not quite. It means you have an array of user objects, each of which contains an array of friends (strings).
Try something like
for (PFUser *user in objects) {
NSArray *friends = [user objectForKey:#"friends"];
for (NSString *friend in friends) {
NSLog(#"Friend is '%#'", friend);
}
}
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