在Parse.com中更新值

时间:2015-10-15 20:23:18

标签: swift parse-platform

我正在尝试更新名为“currentUploaded”的Parse.com表中的值。

这是我在Parse.com代码中的查询: http://pastebin.com/Jr0EcJuy

Parse.com“currentUploads”类:http://i.stack.imgur.com/E8tND.png

这是我想要更新值的按钮(就像现在一样,它在另一个类中创建一个新行,但我只想增加所选项目的“reportedCount”:

@IBAction func reportContentAction(sender: AnyObject) {
        let buttonPosition = sender.convertPoint(CGPointZero, toView: self.collectionView)
        let indexPath = self.collectionView.indexPathForItemAtPoint(buttonPosition)
        ////
        println(indexPath?.item)
        ////
        let post = self.arrayOfDetails[indexPath!.item]

        var alertMessage = NSString(format:"*User: %@\r *Text: %@\r *Created at %@", post.username, post.text, post.CreatedAt)

        var reportAlert = UIAlertController(title: "Report Content", message:alertMessage as String, preferredStyle: UIAlertControllerStyle.Alert)

        reportAlert.addAction(UIAlertAction(title: "Yes", style: .Default, handler: { (action: UIAlertAction!) in
            println("Handle Report Logic here")

            var currentUploads = PFObject(className: "banned")
            currentUploads["username"] = post.username
            currentUploads["imageText"] = post.text
            currentUploads["imageFile"] = post.image
            currentUploads["identifierForVendor"] = post.deviceID
            currentUploads["flaggedBy"] = PFUser.currentUser()?.username
            currentUploads["flaggedByUUID"] = UIDevice.currentDevice().identifierForVendor.UUIDString
            currentUploads.saveInBackgroundWithBlock({ (success: Bool, error: NSError?) -> Void in
                if error == nil{
                    //**Success saving, now save image.**//
                    currentUploads.saveInBackgroundWithBlock({ (success: Bool, error: NSError?) -> Void in
                        if error == nil{
                            // Take user home
                            print("Data uploaded")
                            // Show UIAlertView
                            let alert = UIAlertView()
                            alert.title = "Message"
                            alert.message = "You report has been sent. Thank you for your support."
                            alert.addButtonWithTitle("Close")
                            alert.show()

                        }
                        else{
                            print(error)
                        }
                    })
                }
                else{
                    print(error)
                }
            })
        }))

        reportAlert.addAction(UIAlertAction(title: "Cancel", style: .Default, handler: { (action: UIAlertAction!) in
            println("Handle Cancel Logic here")
        }))

        presentViewController(reportAlert, animated: true, completion: nil)
    }

当用户在弹出窗口中单击“是”时,我也尝试使用此代码,但它不起作用:

reportAlert.addAction(UIAlertAction(title: "Yes", style: .Default, handler: { (action: UIAlertAction!) in
            println("Handle Report Logic here")

            var query = PFQuery(className: "currentUploads")
            query.whereKey("imageFile", equalTo: post.image)
            query.getFirstObjectInBackgroundWithBlock {
                (myObject: PFObject?, error: NSError?) -> Void in
                if (error != nil){
                    println(error)
                    //
                }
                else{
                    query.setValue("1", forKey: "reportedCount")
                }
            }
        }))

请有人告诉我它应该如何正确吗?现在已经挣扎了好几个小时..

1 个答案:

答案 0 :(得分:2)

一个小例子

   let someQuery = PFQuery(className: "banned")
   someQuery.getObjectInBackgroundWithId(someclass.objectId) {
        (updatedObject: PFObject?, error: NSError?) -> Void in
        if error != nil {
            print(error)
        } else if let updatedObject = updatedObject {
            updatedObject["NameOfTheFieldYouWantToChange"] = newValue
            updatedObject.saveInBackground()
        }

    }

someclass.objectId - 它只是您要更新的raw的objectId值。我不知道你在哪里存放它。

“NameOfTheFieldYouWantToChange” - 您要更改的字段的名称:)

newValue - 此字段的新值。

P.S。此外,您似乎正在尝试将您的Parse代码放到ViewController类中。编译器不关心它会工作,但这不是一个好习惯。将所有这些数据库操作放到不同的类(MVC模式)会更加简单。