Parse.com syncing network with Local Datastore - pinning & unpinning issue

China☆狼群 提交于 2019-12-24 15:00:04

问题


I have a Parse backend setup where I have three classes:

  1. User
  2. Place - with restaurant information
  3. SavedPlace - an object modelling the User and Place relationship with pointers to both User and SavedPlace.

What I'm trying to do is to be unable to sync my network and local datastore but only where there are changes i.e. only for SavedPlace objects that are different in the network and local (using updateAt). However, I'm running into issues with pinning and unpinning and I've looked everywhere including the following post below but I cannot seem to resolve it. Parse Local Datastore + Network Sync

See my code for this function where I want to fetch only the updated SavedPlace objects, unpin the old ones in Local Datastore, and re-pin into the Datastore.

The issue seems to be when I re-pin the updated SavedPlace objects retrieved from the network - it seems to delete the Place objects in the Local Datastore. As you can see in the image below, there are both SavedPlace and Place objects pinned and re-pinning the SavedPlace objects delete all the Place objects except for the SavedPlace object I repinned.

SQLite of Local Datastore

Any way round this? Am I using pinAllInBackground correctly?

Appreciate your help on this.

func fetchUpdatedSavedPlacesRemotelyAndPinLocally() {

    if let user = PFUser.currentUser(),
        let lastUpdateLocalDatastore = self.userDefaults.objectForKey("lastUpdateLocalDatastore") {

        // Fetch the places from Parse where lastUpdateDate in Parse is newer than the lastUpdateLocalDatastore
        print("Current lastupdateLocalDatastore: \(lastUpdateLocalDatastore)")
        let savedPlaceQueryParse = PFQuery(className: "SavedPlace")
        savedPlaceQueryParse.whereKey("user", equalTo: user)
        savedPlaceQueryParse.includeKey("place")
        savedPlaceQueryParse.includeKey("objectId")
        savedPlaceQueryParse.whereKey("updatedAt", greaterThanOrEqualTo: lastUpdateLocalDatastore)
        savedPlaceQueryParse.findObjectsInBackgroundWithBlock {
            (updatedSavedPlacesNetwork: [PFObject]?, error: NSError?) -> Void in
            if let updatedSavedPlacesNetwork = updatedSavedPlacesNetwork {
                if updatedSavedPlacesNetwork != [] {
                    print("Success - retrieved \(updatedSavedPlacesNetwork.count) updated places from Parse")

                    // Create an array of objectIds of the updated saved places to match against in the Local datastore
                    var updatedSavedPlaceObjectId = [String]()
                    for updatedSavedPlaceNetwork in updatedSavedPlacesNetwork {
                        updatedSavedPlaceObjectId.append(updatedSavedPlaceNetwork.objectId!)
                    }

                    // Fetch these updated saved places from the Local Datastore
                    let savedPlaceQuery = PFQuery(className: "SavedPlace")
                    savedPlaceQuery.fromLocalDatastore()
                    savedPlaceQuery.whereKey("user", equalTo: user)
                    savedPlaceQuery.includeKey("place")
                    savedPlaceQuery.includeKey("objectId")
                    savedPlaceQuery.whereKey("objectId", containedIn: updatedSavedPlaceObjectId)
                    savedPlaceQuery.findObjectsInBackgroundWithBlock {
                        (updatedSavedPlacesLocal: [PFObject]?, error: NSError?) -> Void in
                        if error == nil {
                            if let updatedSavedPlacesLocal = updatedSavedPlacesLocal {

                                // Unpin the updated saved places from the Local Datastore
                                PFObject.unpinAllInBackground(updatedSavedPlacesLocal) { (success: Bool, error: NSError?) -> Void in
                                    if (success) {
                                        print("Success - \(updatedSavedPlacesLocal.count) updated saved places unpinned from Local Datastore")

                                        // Pin the updated saved places from Parse to the Local Datastore and update the lastUpdateLocalDatastore
                                        PFObject.pinAllInBackground(updatedSavedPlacesNetwork) { (success: Bool, error: NSError?) -> Void in
                                            if (success) {
                                                print("Success - \(updatedSavedPlacesNetwork.count) updated saved places pinned to Local Datastore")
                                                self.userDefaults.setObject(NSDate(), forKey: "lastUpdateLocalDatastore")
                                                print("New lastUpdateLocalDatastore: \(self.userDefaults.objectForKey("lastUpdateLocalDatastore"))")
                                            }
                                            else {
                                                print("Fail - updated saved places not pinned and returned with error: \(error!.description)")
                                            }
                                        }
                                    }
                                    else {
                                        print("Fail - updated saved places not unpinned and returned with error: \(error!.description)")
                                    }
                                }
                            }
                        }
                        else {
                            print("Fail - updated saved places not fetched in Local Database and returned with error: \(error!.description)")
                        }
                    }
                }
                else {
                    print("No updates")
                }
            }
            else {
                print("Fail - load from Parse failed with error: \(error!.description)")
            }
        }
    }
}

来源:https://stackoverflow.com/questions/35577377/parse-com-syncing-network-with-local-datastore-pinning-unpinning-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!