我正在尝试链接用户创建的票证,该票证链接到收件人,但是我什么也没回来,我能够从票证表中获取票证,但是内部查询的匹配似乎没有链接。在解析文档之后,我尝试使用内部查询将其用作链接或桥梁,如下所示:
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 {
}
最佳答案
首先,当我们需要在Parse Server中进行内部查询时,我们会将列另存为Pointer对象。因此,您需要在Message表中将ticketId列保存为Tickets的Pointer Class类。您可以这样保存它:
let pointerObj = [“__type": "Pointer", "className": "Tickets", "objectId": "yourClassObjectId"]
messagePraseObj.setValue(pointerObj, forKey: “ticketId")
之后,您可以使用以下代码完成任务。我在项目中使用了它。
我已使用iOS SDK版本1.17.3和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)")
}
}
}
有关更多信息:https://docs.parseplatform.org/ios/guide/#relational-queries
希望对您有帮助。