Parse Unpin Does Not Remove Object From Local Datastore

This should work.

Here is one of many attempts to get this figured out

  • MFMailComposeViewController appearance setTintColor getting lost iOS 7
  • Scroll UITextView To Bottom
  • NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle
  • Is it possible to import third-party Objective-C code into a Swift playground?
  • iOS Get Signal Strength in Swift (Core Telephony)
  • How to directly update pixels - with CGImage and direct CGDataProvider
  •             myTrainingSessions[indexPath.row].unpinInBackgroundWithBlock{ (succ, e) -> Void in
                if succ == true {
    
                    // just remove from table view etc
                    self.myTrainingSessions[indexPath.row].deleteEventually()
                    self.myTrainingSessions.removeAtIndex(indexPath.row)
                    self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
    
                    // Shows that my object is still in datastore!
                    // object should be UNPINNED - but appers in this result....
                    var query = PFQuery(className:TrainingSession.parseClassName())
                    query.whereKey(self.userType(), equalTo: PFUser.currentUser())
                    query.orderByDescending("createdAt")
                    query.fromLocalDatastore().ignoreACLs()
                    query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
                        if error != nil { return }
                        if let result = objects as? [TrainingSession] {
                            println("local results")
                            println(result)
                        }
                    }
    
    
                }
            }
    

    I do a query after unpinning and the object is still there.

    4 Solutions Collect From Internet About “Parse Unpin Does Not Remove Object From Local Datastore”

    I’ve contacted the support team about the problem where you can’t unpin an object that has a referenced object, so I’m sharing the thread for those interested:

    https://developers.facebook.com/bugs/138298746504630/

    The support person said it was By Design, but upon my request to improve this specification, he told me he would tell the team about it.

    One of the reasons this is not working for you is because there might still be objects in your local datastore that have relation with the object you want to unpin. To do this properly, unpin all those objects first and then unpin the target object.

    I got this to work but using the follow workflow.

    When I create a new object, and am ready to save I do the following 2 steps

        newThing.saveEventually(nil)
    
        newThing.pinInBackgroundWithBlock { (res, errn) -> Void in
            // pinning is required for offline delete to work correct
            if errn != nil { return }
            //self.presentingViewController?.dismissViewControllerAnimated(true, completion: { () -> Void in
                // dismiss modal if required...
            //})
        }
    

    Within my tableview I delete like this

    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {
            self.myDataSource[indexPath.row].deleteEventually()
            self.myDataSource.removeAtIndex(indexPath.row)
            self.myDataSource.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        }
        }
    

    Fetching process, within the same tableview

    1. Local Query – from local datastore – this directly updates my tableview – called first
    2. Network Query – this does the unpinning and pinning, updates the tableview data source – once completed this calls the local Query again to update the table.

    Here is my network query

    func networkQuery() {
    
        let nquery = theQuery()
    
        nquery.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
            if let temp = objects as? [YourObjectType] where error == nil {
                PFObject.unpinAllInBackground(self.myDataSource, block: { (resultBool, error) -> Void in
                    if error != nil { return }
                    PFObject.pinAllInBackground(temp, block: { (resultBool, error) -> Void in
                        if error != nil { return }
                        self.localQuery()
                    })
                })
            }
        }
    }
    

    I have tried to break this creating new items, on and offline – deleting items on and offline – all works. Hope it helps, couldn’t find any examples online so only go it to work with trail and error…. painful process, but working like a charm now.

    After doing R & D on parse.com
    I come to know that you can unpin objects from local storage once they are stored on cloud. B’coz parse is basically used for storing data on cloud so it allow unpinning of data after storing on cloud.
    eg.

    - (void)syncDataToCloud:(id)sender {
        PFQuery *qry = [PFQuery queryWithClassName:@"Person"];
        [qry fromLocalDatastore];
        NSArray *arr = [qry findObjects];
        [PFObject saveAllInBackground:arr block:^(BOOL succeeded, NSError *error) {
            if (succeeded) {
                DisplayAlert(@"Sync successful.");
            } else {
                DisplayAlert(@"Sync unsuccessful");
            }
        }];
        [PFObject unpinAll:arr];
    }
    

    it works for me…!! 🙂