Swift Parse - 将对象保存在哪里

时间:2016-01-17 15:01:58

标签: ios swift parse-platform

我正在尝试用对象更新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)")
            }
        }

1 个答案:

答案 0 :(得分:1)

我想我明白你在说什么,所以我在这里:

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)")
  }
}