ios - Swift 无法更新 Parse 对象

标签 ios swift parse-platform

我有一个问题,我真的需要一些帮助..

我有下面的方法,一切正常,直到第907行..当涉及object3.saveInBackgroundWithBlock时,它什么也不做..甚至没有错误!它永远不会保存对象,也永远不会进入 block 内..

知道为什么吗?

func addUserToThoseIFollow(sender: UIButton) {
    //self.navigationItem.rightBarButtonItem?.enabled = false
    sender.enabled = false

    let userQuery = PFQuery(className: "_User")
    let userQuery2 = PFQuery(className: "_User")

    userQuery.getObjectInBackgroundWithId(PFUser.currentUser().objectId) { (object: PFObject!, error: NSError!) -> Void in
        if error == nil {

            // If I already follow some users, make
            // an array with them, add the user I
            // want to follow and save. Else,
            // just save an array, with that one user.

            if object["following"] != nil {
                var thoseIFollow = object["following"] as! [String]
                thoseIFollow.append(self.userID!)

                object["following"] = thoseIFollow
            }
            else {
                var myUsers = [String]()
                myUsers.append(self.userID!)

                object["following"] = myUsers 
            }

            object.saveInBackgroundWithBlock({ (ok: Bool, error2: NSError!) -> Void in
                if error2 == nil {
                    self.followButton.setTitle("Unfollow", forState: .Normal)
                    self.followButton.backgroundColor = UIColor(red: 1, green: 0, blue: 0, alpha: 0.7)

                    sender.enabled = true

                    self.doIFollow = true
                }
            })
        }
    }

    // Add me to his followers

    userQuery2.getObjectInBackgroundWithId(self.userID) { (object3: PFObject!, error3: NSError!) -> Void in
        if error3 == nil {
            // If the user I just followed, has already followers, make
            // an array with them and add the current user to
            // them. Else, just save an array, with the current user.

            if object3["followers"] != nil {
                var hisFollowers = object3["followers"] as! [String]

                hisFollowers.append(PFUser.currentUser().objectId)
                object3["followers"] = hisFollowers

               /* Line 907 */ object3.saveInBackgroundWithBlock({ (ok7: Bool, error7: NSError?) -> Void in // Line 907
                    if error7 == nil {
                        print("ok")
                    }
                    else {
                        print(error7)
                    }
                })
            }
            else {
                var hisFollowers = [String]()

                hisFollowers.append(PFUser.currentUser().objectId)
                object3["followers"] = hisFollowers

                object3.saveInBackgroundWithBlock( { (ok5: Bool, error7: NSError!) -> Void in
                    print("otinanai")

                    if error7 != nil {
                        print(error7.localizedDescription)
                    }
                })
            }
        }
    }
}

最佳答案

尝试#1

PFUser.currentUser().objectId 返回什么?如果它返回nil,那么它不起作用。

尝试#2

Available Parse Types

到目前为止,我们已经使用了 NSString、NSNumber 和 PFObject 类型的值。 Parse 还支持 NSDate 和 NSNull。 您可以嵌套 NSDictionary 和 NSArray 对象,以在单个 PFObject 中存储更多结构化数据。

尝试使用 var hisFollowers = [NSString]() 而不是 var hisFollowers = [String]()

关于ios - Swift 无法更新 Parse 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34596878/

相关文章:

ios - 使用 AVAudioSession 时背景音频安静

android - 解析安装未获取 DeviceToken

ios - 解析 .deleteInBackground 不起作用(Swift)

ios - 选项卡式 UINavigationBar iOS 8

android - Flutter In App Purchase : InAppPurchaseConnection. instance.queryProductDetails 返回空列表

swift - Xcode/plist - 在 macOS 上始终以深色/浅色主题运行应用程序

json - swift 4.2 : Type 'T' does not conform to protocol 'Decodable'

xcode - 检测碰撞 SpriteKit Swift

javascript - 嵌入 JW Player 使用 Parse.com 创建空白屏幕

ios - 如何清空 UIPageViewController(移除里面的 ViewControllers)