Parse.com将网络与本地数据存储同步 - 固定&解决问题

时间:2016-02-23 12:17:07

标签: ios swift networking parse-platform local-database

我有一个Parse后端设置,我有三个类:

  1. 用户
  2. 地点 - 附餐厅信息
  3. SavedPlace - 使用User和SavedPlace指针建模User和Place关系的对象。
  4. 我要做的是无法同步我的网络和本地数据存储区,但仅限于有变化的地方,即仅针对网络和本地不同的SavedPlace对象(使用updateAt)。但是,我遇到了固定和取消固定的问题,我到处都看,包括下面的帖子,但我似乎无法解决它。 Parse Local Datastore + Network Sync

    请参阅此函数的代码,其中我只想获取更新的SavedPlace对象,取消固定本地数据存储区中的旧对象,然后重新固定到数据存储区。

    问题似乎是当我重新固定从网络检索到的更新的SavedPlace对象时 - 它似乎删除了本地数据存储中的Place对象。如下图所示,SavedPlace和Place对象都固定并重新固定SavedPlace对象,删除除了我重新编写的SavedPlace对象之外的所有Place对象。

    SQLite of Local Datastore

    任何方式围绕这个?我正确使用pinAllInBackground吗?

    感谢你的帮助。

    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)")
                }
            }
        }
    }
    

0 个答案:

没有答案