user2707185
user2707185

Reputation:

How to retrieve Parse object created by a User

user I'm integrating Parse in my application and at this point I have the login function up and running. Now I'm trying to understand/learn how I create and connect objects in a another class unique to the current logged in user and how to retrieve those objects.

So I need some help to figure this out. I found this question and it's the same thing I want to achieve however I can't figure out the "connection" so to speak, and if I need to add parameters to my User class and/or my Object class I want to retrieve from.

So for simplicity let's say I have logged in with my User with a unique objectId that has been created to the Parse User class. Then when I'm logged in I do this:

PFObject *list = [PFObject objectWithClassName:@"Lists"];
    list[@"name"] = @"My list";

    [list saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            // The object has been saved.
            NSLog(@"Success");
        } else {
            // There was a problem, check error.description
            NSLog(@"Fail");
        }
    }];

The list is created and stored in my List class on my Parse account for the app. Now if let's say I log out and then in again with the user that created that list, how do I retrieve that unique list for that user?

In the question (linked earlier) this is suggested and marked as working:

PFQuery *innerQuery = [PFQuery queryWithClassName:@"User"];
[innerQuery whereKey:@"userType" equalTo:@"X"];  // fix with your real user type

PFQuery *query = [PFQuery queryWithClassName:@"Post"];
[query whereKey:@"user" matchesQuery:innerQuery];
[query findObjectsInBackgroundWithBlock:^(NSArray *posts, NSError *error) {
     // posts are posts where post.user.userType == X
}];

In this example, what is @"userType", @"X" and @"user"? Can someone explain so that I get the hang of the "flow" or connection between the queries so to speak. So I can implement the functionality I'm after.

Upvotes: 0

Views: 347

Answers (1)

Trond Kristiansen
Trond Kristiansen

Reputation: 2446

Before you can make a query to Parse and retrieve user information, you have to store some. this is done by creating a PFObject with some properties attached to it. Once saved, you can retrieve this information. Here is an example where you create a User object which acts as a parent to Favorite objects.

PFObject *user = [PFObject objectWithClassName:@"User"];
[user saveEventually];

PFObject *favorite = [PFObject objectWithClassName:@"Favorites"];
favorite[@"numberOfFavorites"] = @(10);
favorite.ACL = [PFACL ACLWithUser:[PFUser currentUser]];
favorite[@"parent"] = user;

[favorite saveEventually];

After saving you can retrieve the objects again for the given user:

PFQuery *query = [PFQuery queryWithClassName:@"User"];
PFQuery *innerQuery = [PFQuery queryWithClassName:@"Favorites"];
[innerQuery whereKey:@"parent" matchesQuery:query];

[[[innerQuery findObjectsInBackground] continueWithBlock:^id(BFTask *task) {
    if (task.error){
        return nil;
    }
 }];

Parse have very good documentation and examples that can help you along. Hope this helps. T

Upvotes: 2

Related Questions