我正在尝试使用对象更新 Parse 类中的数组列(即列名称为 FriendsRequestList)。我搜索后只能找到一种方法:
let query = PFQuery(className: "FriendsConnections")
query.getObjectInBackgroundWithId(self.friendObject.objectId!) {
(user: PFObject?, error: NSError?) -> Void in
if error != nil {
print(error)
} else if let user = user {
user.addObject(self.userObject, forKey: "friendsRequestList")
user.saveInBackground()
}
}
这里唯一的问题是函数 getObjectInBackgroundWithId 需要对象 ID,而我想要一个 query.wherekey,因为它更适合我在 Parse 中拥有的数据。
如何在不使用 objectID 而是使用 whereKey 的情况下执行上述操作。
谢谢
更新:基于提供的答案的新代码但仍然不起作用:
let query = PFQuery(className: "FriendsConnections")
query.whereKey("appUsername", equalTo: self.friendObject.username!)
query.findObjectsInBackgroundWithBlock {
(objects: [PFObject]?, error: NSError?) -> Void in
if error == nil {
// Do something with the found objects
if let objects = objects {
for object in objects {
print("Processing Object")
object.addObject(PFUser.currentUser()!["appUsername"], forKey: "friendsRequestList")
object.saveInBackground()
}
}
} else {
// Log details of the failure
print("Error: \(error!) \(error!.userInfo)")
}
}
最佳答案
我想我明白你在说什么,所以这是我的想法:
let query = PFQuery(className: "FriendsConnections")
query.whereKey("username", equalTo: mySuppliedArgument)
query.findObjectsInBackgroundWithBlock {
(objects: [PFObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
print("Successfully retrieved \(objects!.count) scores.")
// Do something with the found objects
if let objects = objects {
for object in objects {
object.addObject(self.userObject, forKey: "friendsRequestList")
object.saveInBackground()
}
}
} else {
// Log details of the failure
print("Error: \(error!) \(error!.userInfo)")
}
}
关于ios - Swift Parse - 将对象保存在关键位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34839787/