0

I have two tables in my parse database. The first table is the users table, that contains users info like username, email, password,profile pic etc. The second table contains posts added by users. I want to fetch a post along with the details of the user who added that post. Can any one suggest me how to do that in swift language.

I have tried the following code :

let innerQuery = PFQuery(className: "Posts")
    innerQuery.whereKey("pickUpLocation", equalTo: firstSectionLbls[0] as! String)
    innerQuery.whereKey("destination", equalTo: firstSectionLbls[1] as! String)
    innerQuery.limit = 10
    innerQuery.skip = skip
    innerQuery.orderByAscending("DepartureDate")

    let query = PFUser.query()
    query!.whereKey("username", matchesQuery: innerQuery)

    query!.findObjectsInBackgroundWithBlock {
        (results: [PFObject]?, error: NSError?) -> Void in

I have retried using pointer. These are the steps I used to create pointer. 1. created a column named user in users table and set its type to pointer. 2. Created another column with same name as "user" in another class named "Posts" as set its type to pointer.

Then I used the below code to fetch the user data along with post data.

   let innerQuery = PFQuery(className: "Rides")
    innerQuery.whereKey("pickUpLocation", equalTo: firstSectionLbls[0] as! String)
    innerQuery.whereKey("destination", equalTo: firstSectionLbls[1] as! String)
    innerQuery.limit = 10
    innerQuery.skip = skip
    innerQuery.includeKey("user")
    innerQuery.orderByAscending("DepartureDate")

    innerQuery.findObjectsInBackgroundWithBlock {
        (results: [PFObject]?, error: NSError?) -> Void in

But now again I didnot receive the users data, though I received the posts data

7
  • 1
    So what have you tried? StackOverflow isn't a code writing service Commented Dec 20, 2015 at 10:07
  • why you people always under estimate others. You think I haven't tried. I have tried the above code. Commented Dec 20, 2015 at 10:12
  • Does the posts class include a pointer to the posting user? If so then just use includeKey for the user pointer column in your posts query. Commented Dec 20, 2015 at 10:22
  • I haven't used a pointer. But I am trying right now. Commented Dec 20, 2015 at 10:24
  • Hello, I have tried using pointer. But may be I am not aware how to use a pointer. Can you please let me know the right way? Commented Dec 20, 2015 at 10:45

1 Answer 1

2

To relate posts to a user, create a column in your Post table, calling it user (or author or whatever). Use a Pointer column. Then, when you create a post, associate it with the current user, i.e. like this:

newPost["user"] = PFUser.currentUser()

Then, when you save it, Parse will store a pointer to the current user's user record in the user column.

Now, when you do your query, you can get the user object together with the post object by using includeKey the way you did.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.