2015-10-15 69 views
-1

我正在尝试更新名为“currentUploaded”的Parse.com表中的一个值。更新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

一个小例子

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 - 它只是要更新原始的对象ID值。我不知道你在哪里存储它。

“NameOfTheFieldYouWantToChange” - 此领域的新价值 - 你想改变:)

NEWVALUE字段的名称。

P.S.此外,你似乎试图把你的解析代码放到你的ViewController类中。编译器不会在意,所以它会起作用,但这不是一个好习惯。将所有这些数据库操作放到不同的类(MVC模式)会更加简单。

+0

我不知道我会怎么做不同的课堂?你的意思是继承或什么?我不知道如何从我的方法做到这一点。 – IdaEmilie

+0

我不明白一些class.objectid,要放在那里? – IdaEmilie

+0

你想改变表中的某一行(对象)。这一行有objectId。使用这个objectId是告诉表什么对象你想改变的最简单的方法。 – lithium