Parse尝试在Swift 3中使用带有findObjectsInBackgroundWithBlock的新未保存子项保存对象

时间:2016-12-06 08:38:30

标签: ios swift parse-platform

viewDidLoad中,我查询了当前VC的用户

//query for current page user
    let userQuery = PFUser.query()
    userQuery!.whereKey("username", equalTo: userUsername)

    print(PFUser.current()!)

    PFUser.current()?.saveInBackground(block: { (done:Bool, error:Error?) in
        userQuery!.getFirstObjectInBackground { (result:PFObject?, error:Error?) in
            self.currentProfilePageUser = result as! PFUser
        }
    })

然后在viewDidAppear我写了这个来获取与当前VC用户相关的一些信息。

let query = PFQuery(className: "Follow")
        query.whereKey("followFrom", equalTo: PFUser.current()!)
        query.whereKey("followingTo", equalTo: self.currentProfilePageUser)

        if PFUser.current()?.objectId == nil{
            PFUser.current()?.saveInBackground(block: { (done:Bool, error:Error?) in
                query.getFirstObjectInBackground { (result:PFObject?, error:Error?) in
                    if error==nil{
                        if (result != nil){
                            self.followButtonTitle.setTitle("Unfollow", for: UIControlState())
                            self.followButtonTitle.setTitleColor(UIColor(red: 93/255, green: 215/255, blue: 217/255, alpha: 1), for: UIControlState())
                            self.canFollow = false
                        }
                    }else{
                        //                print("Failed with error \(error)")
                        self.followButtonTitle.setTitle("Follow", for: UIControlState())
                        self.followButtonTitle.setTitleColor(UIColor(red: 252/255, green: 105/255, blue: 134/255, alpha: 1), for: UIControlState())
                        self.canFollow = true
                    }
                }
            })
        }
        else{
            query.getFirstObjectInBackground { (result:PFObject?, error:Error?) in
                if error==nil{
                    if (result != nil){
                        self.followButtonTitle.setTitle("Unfollow", for: UIControlState())
                        self.followButtonTitle.setTitleColor(UIColor(red: 93/255, green: 215/255, blue: 217/255, alpha: 1), for: UIControlState())
                        self.canFollow = false
                    }
                }else{
                    self.followButtonTitle.setTitle("Follow", for: UIControlState())
                    self.followButtonTitle.setTitleColor(UIColor(red: 252/255, green: 105/255, blue: 134/255, alpha: 1), for: UIControlState())
                    self.canFollow = true
                }
            }
        }

根据StackOverflow上其他类似问题的解决方案,人们建议检查当前用户对象是否已保存,然后执行findObjectsInBackgroundWithBlock。但是,我仍然试图使用此代码保存一个带有未保存的新子对象错误的对象。

0 个答案:

没有答案