Search code examples
iosswiftxcodemongodbparse-platform

Inner Query Parse


enter image description here Im trying to link a ticket that a user creates that is linked to a recipient but I am not getting anything back, I am able to get the tickets from the tickets table but the matching of the innerquery doesn't seem to link. Going off the parse docs, I tried using an inner query to use as a link or a bridge as follows:

let innerQuery = PFQuery(className:"Tickets")
        let query = PFQuery(className: "Messages")

        //get all tickets for current user
        innerQuery.whereKey("user", equalTo: PFUser.current()?.objectId as Any)
        //objectId is ticketId in tickets table
        innerQuery.includeKey("objectId")
        //match to ticketId in messages table
        query.whereKey("ticketId", matchesQuery: innerQuery)

        do {
            let returnedObjects = try query.findObjects()
            for object in returnedObjects {
                let sender = object["sender"] as? String
                }
            }
        } catch {

        }

Solution

  • First of all, when we need to do Inner query in Parse Server we will save the column as Pointer object. So here you need to save the ticketId column in the Message table as a Pointer Class of Tickets class. You can save it like this:

    let pointerObj = [“__type": "Pointer", "className": "Tickets", "objectId": "yourClassObjectId"]
    messagePraseObj.setValue(pointerObj, forKey: “ticketId")
    

    After that, you can use the below code for achieving your task. I used it on my projects. I have used iOS SDK version 1.17.3 and swift 4.

        let innerQuery = PFQuery.init(className: "Tickets")
        innerQuery.whereKey("user", equalTo: PFUser.current()?.objectId as Any)
    
        let query = PFQuery.init(className: "Messages")
        query.whereKey("ticketId", matchesQuery: innerQuery)
        query.includeKey("ticketId")
    
        query.findObjectsInBackground { (returnedObjects, error) in
            if error == nil && returnedObjects != nil {
                for object in returnedObjects! {
                    let ticket = object.value(forKey: "ticketId") as! PFObject
                    let sender = ticket.value(forKey: "user") as? String ?? ""
                    print("User ID: \(sender)")
                }
            }
        }
    

    For more information: https://docs.parseplatform.org/ios/guide/#relational-queries

    Hope it will help you.