核心数据iCloud Sync

时间:2015-07-12 04:23:40

标签: ios core-data icloud

我遇到核心数据icloud同步问题。我已经在线观看了WWDC 2013视频和其他一些教程,同步正在为一些输入工作。如果我慢慢添加数据并让icloud先同步,它就可以正常工作。但是当我尝试在一个设备中连续添加大量数据时,只有部分数据记录会同步到其他设备。但是Strangley在给了很长一段时间(可能是3,4小时)之后,数据会再次在设备上同步。我的设置如下,

class CoreDataController : NSObject {
static let sharedInstance = CoreDataController()



override init(){
    super.init()

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "persistentStoreWillChange:", name: NSPersistentStoreCoordinatorStoresWillChangeNotification, object: self.managedObjectContext!.persistentStoreCoordinator)


    NSNotificationCenter.defaultCenter().addObserver(self, selector: "persistentStoreDidChange:", name: NSPersistentStoreCoordinatorStoresDidChangeNotification, object: nil)


    NSNotificationCenter.defaultCenter().addObserver(self, selector: "recieveICloudChanges:", name: NSPersistentStoreDidImportUbiquitousContentChangesNotification, object: self.managedObjectContext!.persistentStoreCoordinator)

}

//MARK: - Notifications Selectors

func persistentStoreWillChange (notification:NSNotification) {
    NSLog("Receive PersistentStoreWillChange Notification")

    self.managedObjectContext!.performBlock { () -> Void in
        if self.managedObjectContext!.hasChanges {
            var error:NSError? = nil
            self.managedObjectContext!.save(&error)
            if error != nil {
                println("Save error: \(error)")
            }else{
                // drop any manged object refrences
                self.managedObjectContext!.reset()
            }

        }
    }
}

func persistentStoreDidChange(notification:NSNotification)  {

    NSLog("Receive PersistentStoreDidChange Notification")
//I post notification here to let the app refresh data from other classes 
   NSNotificationCenter.defaultCenter().postNotificationName("receiveNewData", object: nil)


}


func recieveICloudChanges (notification:NSNotification){

    NSLog("Receive ICloudChanges Notification")
    self.managedObjectContext!.performBlock { () -> Void in
        self.managedObjectContext!.mergeChangesFromContextDidSaveNotification(notification)

//I post notification here to let the app refresh data from other classes 
     NSNotificationCenter.defaultCenter().postNotificationName("receiveNewData", object: nil)
    }
}

//MARK: - NSManagedObjectContext Initializer 

lazy var managedObjectContext: NSManagedObjectContext? = {


    var managedObjectContext = NSManagedObjectContext()


    let modelURL = NSBundle.mainBundle().URLForResource("cloudynote", withExtension: "momd")!
    let objectmodel = NSManagedObjectModel(contentsOfURL: modelURL)!

    managedObjectContext = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.MainQueueConcurrencyType)
    managedObjectContext.persistentStoreCoordinator = NSPersistentStoreCoordinator(managedObjectModel: objectmodel)

    let documentDirectory = NSFileManager.defaultManager().URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask).last as! NSURL

    let storeURL = documentDirectory.URLByAppendingPathComponent("CoreData.sqlite")

    let storeOptions = [NSPersistentStoreUbiquitousContentNameKey:"CloudyNoteStore"]

    var error: NSError? = nil
    var failureReason = "There was an error creating or loading the application's saved data."

    if managedObjectContext.persistentStoreCoordinator?.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: storeURL, options: storeOptions, error: &error) == nil{
        // Report any error we got.
        var dict = [String: AnyObject]()
        dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
        dict[NSLocalizedFailureReasonErrorKey] = failureReason
        dict[NSUnderlyingErrorKey] = error
        error = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)

        NSLog("Unresolved error \(error), \(error!.userInfo)")
        abort()
    }


    return managedObjectContext
    }()

}

0 个答案:

没有答案