Danial Kosarifar
Danial Kosarifar

Reputation: 37

deleting the data both from the table and the parse data source using swift

Hi there I've been trying to create a parse connected to do list . And the part that bothers me is when I want to remove an element from a table and I want the same Object get deleted in the database . The problem is I can delete any object in the simulator but it deletes the very next object from the database instead of the one that was triggered .

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if (editingStyle == UITableViewCellEditingStyle.Delete){

            LM.myList.removeAtIndex(indexPath.row)
            let myQuery = PFQuery(className: "list")
            myQuery.findObjectsInBackgroundWithBlock({ (Objects , error ) -> Void in
                if (error != nil){
                    print("Error was Found")
                }else if let Object = Objects {
                    for MyObject in Object {
                        if (MyObject["title"] as! String == LM.myList[indexPath.row].title  && MyObject["desc"] as! String  == LM.myList[indexPath.row].Description)

                        {
                         //print(LM.myList[indexPath.row ].Description)
                        //print(LM.myList[indexPath.row ].title)
                       //print(MyObject["title"] as! String)
                      //print(MyObject["desc"] as! String)
                       MyObject.deleteInBackground()
                       MyObject.saveInBackground()

                        }
                    }
                }
            }

            )

            tableView.deleteRowsAtIndexPaths( [indexPath] , withRowAnimation: UITableViewRowAnimation.Automatic)
        }
    }

Thanks in advance :)

Upvotes: 0

Views: 45

Answers (1)

Wain
Wain

Reputation: 119031

LM.myList.removeAtIndex(indexPath.row)

This line deletes the target object from your local data source, so when you later query for it to compare the name you find the wrong object.

You don't need to make the query and iterate. You should be getting the object from the local data source, delete it (removing it from the server) and then delete it from the local data source and table.

Upvotes: 1

Related Questions