简体   繁体   中英

Update row in Parse.com

How can i upload a row in Parse.com?

This is my query code:

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

I want to update the "reportedCount" when the image is reported. I have a code that i have been using, but that created new rows in another class for each report, and I want only to update the "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)
    }

You should be keeping a reference to oneobject , or more broadly, all of the objects returned from the query. Now when something changes you can get the appropriate instance from the objects array and then update and save it to modify the existing 'row'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM