ios - Swift Parse - 将对象保存在关键位置

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

最佳答案

我想我明白你在说什么,所以这是我的想法:

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/

相关文章:

ios - 如何在 iOS 中更改状态栏文本颜色

javascript - 尝试在 Parse.com 上运行一些云代码,我得到 "Update failed with internal error"

ios - 如何找到手机屏幕距离眼睛直线的距离

ios - 无法从其 DataSource 获取单元格

ios - 导航 Controller 推送无法正常工作 ios 11

javascript - 如何使用 Angular2 初始化 Parse 并指向 serverURL

Gmail 和 parse_resource gems 之间的 Ruby 自动加载冲突

iphone - 针对 iPhone 5 进行了优化

ios - 如何禁用屏幕特定区域的滑动球功能? ( swift )(SpriteKit)

swift - 是否会使用 Swift 的 Guard 或 if Let Keywords 来解决不必使用此 checkNil 函数的问题?