我试图确定当前用户是否与我的活动有关系。
换句话说,我有一些用户可以加入的事件。我将用户的PFRelation存储在事件表中。
我试过了
self.event.users.query().getObjectInBackground(withId: currentUser.objectId!, block: { (object, error) in
if let error = error {
//The query returned an error
print(error.localizedDescription)
} else {
//The object has been retrieved
print(object)
}
})
和
do {
let user = try self.event.users.query().getObjectWithId(currentUser.objectId!)
if user == nil {
currentUser.saveInBackground { (success: Bool, error: Error?) in
eventsRelation.add(object)
}
usersRelation.add(currentUser)
object.saveInBackground()
}
} catch {
print("Unexpected error: \(error).")
}
但是这些无论如何都会回报用户。即使它们不在关系中。
就像他们在整个用户表上运行查询一样。
如何仅在子集中运行它?
最佳答案
答案是使用该关系进行查询。
See here for more documentation
因此,代码如下所示:
let usersRelation = event.relation(forKey: "users")
let usersRelationQuery = usersRelation.query()
usersRelationQuery.whereKey("objectId", equalTo: currentUser.objectId!)
usersRelationQuery.findObjectsInBackground(block: { (objects: [PFObject]?, error: Error?) in
if let error = error {
print(error.localizedDescription)
} else if let objects = objects {
// The find succeeded.
print("Successfully retrieved \(objects.count) scores.")
// Do something with the found objects
for object in objects {
print(object.objectId as Any)
}
}
})
请注意,上面的
event
变量来自数据库。如果我们不包括
whereKey
部分,那么它将得到那个关系中的那些。关于ios - 从Parse获取关系用户,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55822275/