简体   繁体   English

从解析中删除对象的问题

[英]Issue with deleting object from parse

I am facing an issue when trying to delete objects from Parse after having queried them. 在查询对象后尝试从Parse中删除对象时,我遇到了一个问题。

My code: 我的代码:

        var query = PFQuery(className:"sendMessage")
        query.whereKey("messageSent", equalTo: PFUser.currentUser()!.username!)
        query.whereKey("messageReceived", equalTo: self.nameLabel!.text!)
        query.findObjectsInBackgroundWithBlock({ (objects, NSError) -> Void in

            if  objects != nil {

                if let objects = objects as? [PFObject] {
                    for object in objects {


                        print(object["message"])

                   /// here I would go: object.deleteInBackground()
                                        object.save()

                      }
                 }
               }
         })

But it seems that I cannot find the right way to do so. 但是似乎我找不到正确的方法。 Any insights ? 有什么见解吗?

var query = PFQuery(className:"sendMessage")
let username = PFUser.currentUser()?.username
    query.whereKey("messageSent", equalTo: username)
    query.whereKey("messageReceived", equalTo: self.nameLabel!.text!)
    query.findObjectsInBackgroundWithBlock({ (objects:[AnyObject]?, error:NSError) -> Void in
        if  error == nil {
            if let objects = objects as? [PFObject] {
                for object in objects {
                 let deletemessage = object["message"] as! String
                    print(deletemessage)
                    object.delete()
                  }
             }
           } 
            else {
                    println("Error")
               }
     })

I have used deleteEventually() with success before, together with PFObject(withoutDataWithClassName: YourClassName, objectId: YourObjectID) . 我之前已经成功使用deleteEventually()以及PFObject(withoutDataWithClassName: YourClassName, objectId: YourObjectID)

If that works I wouldn't know why, but well :) 如果可以的话,我不知道为什么,但是:)

(as stated by Hector in this Parse Question (Objective-C): https://www.parse.com/questions/delete-row ) (如赫克托在本解析问题(目标C)中所述: https : //www.parse.com/questions/delete-row

for object in objects {
    print(object["message"]
    var toDelete = PFObject(withoutDataWithClassName: "sendMessage", objectId: object.objectID)
    toDelete.deleteEventually()
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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