2015-10-15 59 views
0

如何上傳Parse.com中的一行?Parse.com中的更新行

這是我的查詢代碼:

func queryFromParse(){ 
     self.arrayOfDetails.removeAll() 
     let query = PFQuery(className: "currentUploads") 
     query.orderByDescending("createdAt") 
     query.findObjectsInBackgroundWithBlock { (objects:[AnyObject]?, error:NSError?) -> Void in 
      if error == nil 
      { 
       if let newObjects = objects as? [PFObject] { 

        for oneobject in newObjects { 
         let text = oneobject["imageText"] as! String 
         let username = oneobject["username"] as! String 
         let deviceID = oneobject["identifierForVendor"] as! String 
         let reportedCount = oneobject["reportedCount"] as! String 
         let time = oneobject.createdAt! 

         if let userImage = oneobject["imageFile"] as? PFFile { 
          let userImage = oneobject["imageFile"] as! PFFile 

          let imageURL = userImage.url // <- Bruker nå userImage.URL, henter ikke bildefilen med en gang 
          let OneBigObject = Details(username: username, text: text, CreatedAt: time, image: imageURL!, deviceID: deviceID, reportedCount: reportedCount) 
          //let OneBigObject = Details(username: username, text: text, CreatedAt: time, image: imageURL!) 

          self.arrayOfDetails.append(OneBigObject) 

          dispatch_async(dispatch_get_main_queue()) { self.collectionView.reloadData() } 
         } 
        } 
       } 
      } 
     } 
    } 

Image here

我想更新 「reportedCount」 報道圖像時。我有我一直在使用一個代碼,但在另一個類爲每個報表創建新的行,我想只更新「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) 
    } 
+0

您不需要創建新的PFObject,而需要更新現有PFObject的值並將其保存。此外,由解析方法執行的塊已經派發到主隊列中 – Paulw11

+0

@ Paulw11 - 您能否提供一個示例解決方案? – IdaEmilie

回答

0

你應該保持一個參考oneobject,或更廣泛地說,從查詢返回的所有objects。現在,當發生某些變化時,您可以從objects陣列中獲取適當的實例,然後更新並保存它以修改現有的「行」。

+0

我到底怎麼做? – IdaEmilie