2015-02-22 3 views
0

Im возникают проблемы синхронизации моего CoreData к ICloud я получаю сообщение об ошибке в консоли говоря 2015-02-22 23:09:31.229 Newstron[1711:233218] -[PFUbiquitySetupAssistant finishSetupWithRetry:](826): CoreData: Ubiquity: <PFUbiquitySetupAssistant: 0x7fbc6357e580>: Retrying after delay: 60 Error Domain=NSCocoaErrorDomain Code=134080 "The operation couldn’t be completed. (Cocoa error 134080.)" UserInfo=0x7fbc637794f0 {Reason=Didn't get a container URL back from URLForUbiquityContainerIdentifier:, giving up now. Please ensure the application is signed with the proper entitlements to read from the container., NSPersistentStoreUbiquitousContainerIdentifierKey=null}Сохранение/Syncing CoreData к ICloud в Swift

Ive Got код в AppDelegate.swift

// MARK: - Core Data stack 
func observeCloudActions(persistentStoreCoordinator psc: NSPersistentStoreCoordinator?) { 
    // iCloud notification subscriptions 
    let nc = NSNotificationCenter.defaultCenter(); 
    nc.addObserver(
     self, 
     selector: "storesWillChange:", 
     name: NSPersistentStoreCoordinatorStoresWillChangeNotification, 
     object: psc); 

    nc.addObserver(
     self, 
     selector: "storesDidChange:", 
     name: NSPersistentStoreCoordinatorStoresDidChangeNotification, 
     object: psc); 

    nc.addObserver(
     self, 
     selector: "persistentStoreDidImportUbiquitousContentChanges:", 
     name: NSPersistentStoreDidImportUbiquitousContentChangesNotification, 
     object: psc); 

    nc.addObserver(
     self, 
     selector: "mergeChanges:", 
     name: NSManagedObjectContextDidSaveNotification, 
     object: psc); 
} 

func mergeChanges(notification: NSNotification) { 
    NSLog("mergeChanges notif:\(notification)") 
    if let moc = managedObjectContext { 
     moc.performBlock { 
      moc.mergeChangesFromContextDidSaveNotification(notification) 
      self.postRefetchDatabaseNotification() 
     } 
    } 
} 

func persistentStoreDidImportUbiquitousContentChanges(notification: NSNotification) { 
    self.mergeChanges(notification); 
} 

// Subscribe to NSPersistentStoreCoordinatorStoresWillChangeNotification 
// most likely to be called if the user enables/disables iCloud 
// (either globally, or just for your app) or if the user changes 
// iCloud accounts. 
func storesWillChange(notification: NSNotification) { 
    NSLog("storesWillChange notif:\(notification)"); 
    if let moc = self.managedObjectContext { 
     moc.performBlockAndWait { 
      var error: NSError? = nil; 
      if moc.hasChanges && !moc.save(&error) { 
       NSLog("Save error: \(error)"); 
      } else { 
       // drop any managed objects 
      } 

      // Reset context anyway, as suggested by Apple Support 
      // The reason is that when storesWillChange notification occurs, Core Data is going to switch the stores. During and after that switch (happening in background), your currently fetched objects will become invalid. 

      moc.reset(); 
     } 

     // now reset your UI to be prepared for a totally different 
     // set of data (eg, popToRootViewControllerAnimated:) 
     // BUT don't load any new data yet. 
    } 
} 

// Subscribe to NSPersistentStoreCoordinatorStoresDidChangeNotification 
func storesDidChange(notification: NSNotification) { 
    // here is when you can refresh your UI and 
    // load new data from the new store 
    NSLog("storesDidChange posting notif"); 
    self.postRefetchDatabaseNotification(); 
} 

func postRefetchDatabaseNotification() { 
    dispatch_async(dispatch_get_main_queue(), {() -> Void in 
     NSNotificationCenter.defaultCenter().postNotificationName(
      "kRefetchDatabaseNotification", // Replace with your constant of the refetch name, and add observer in the proper place - e.g. RootViewController 
      object: nil); 
    }) 
} 

lazy var applicationDocumentsDirectory: NSURL = { 
    // The directory the application uses to store the Core Data store file. This code uses a directory named "hyouuu.pendo" in the application's documents Application Support directory. 
    let urls = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask) 
    return urls[urls.count-1] as! NSURL 
    }() 

lazy var managedObjectModel: NSManagedObjectModel = { 
    // The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model. 
    let modelURL = NSBundle.mainBundle().URLForResource("MyAppData", withExtension: "momd")! 
    NSLog("modelURL:\(modelURL)") 
    return NSManagedObjectModel(contentsOfURL: modelURL)! 
    }() 

lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = { 
    // The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail. 
    // Create the coordinator and store 
    var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel) 
    let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("MyAppData.sqlite") 
    NSLog("storeURL:\(url)") 
    var error: NSError? = nil 
    var failureReason = "There was an error creating or loading the application's saved data." 
    if coordinator!.addPersistentStoreWithType(
     NSSQLiteStoreType, 
     configuration: nil, 
     URL: url, 
     options: [NSPersistentStoreUbiquitousContentNameKey : "MyAppName"], 
     error: &error) == nil 
    { 
     coordinator = nil 
     // Report any error we got. 
     let dict = NSMutableDictionary() 
     dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data" 
     dict[NSLocalizedFailureReasonErrorKey] = failureReason 
     dict[NSUnderlyingErrorKey] = error 
     //error = NSError(domain: "Pendo_Error_Domain", code: 9999, userInfo: dict as! [NSObject : AnyObject]) 
     // Replace this with code to handle the error appropriately. 
     // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
     NSLog("AddPersistentStore error \(error), \(error!.userInfo)") 
    } 

    self.observeCloudActions(persistentStoreCoordinator: coordinator) 

    return coordinator 
    }() 

lazy var managedObjectContext: NSManagedObjectContext? = { 
    // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail. 
    let coordinator = self.persistentStoreCoordinator 
    if coordinator == nil { 
     return nil 
    } 
    var managedObjectContext = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType) 
    managedObjectContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy 
    managedObjectContext.persistentStoreCoordinator = coordinator 
    return managedObjectContext 
    }() 

// MARK: - Core Data Saving support 

func saveContext() { 
    if let moc = self.managedObjectContext { 
     var error: NSError? = nil 
     if moc.hasChanges && !moc.save(&error) { 
      // Replace this implementation with code to handle the error appropriately. 
      // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
      NSLog("Unresolved error \(error), \(error!.userInfo)") 
     } 
    } 
} 

Я не Я не знаю, в чем проблема: Code Signing выглядит нормально, но я все время получаю эту ошибку, которую я искал в Apple Developer Forms, нет ответов, смотрел на переполнение и Google, равно никаких ответов на мою проблему. не так много документации по этой ошибке, которую я получаю. Так есть лучший способ реализовать CoreData с интеграцией iCloud или есть исправление для этой проблемы. Код в AppDelegate.swift - это единственное место, где я внедряю код iCloud.

+0

Вы нашли решение на этом? Я тестирую синхронизацию при отключении iCloud в настройках и вижу ту же ошибку, когда отключу свое приложение в настройках iCloud Drive. Это имеет смысл, потому что приложение уже синхронизировано с облаком, поэтому есть локальная копия данных, поэтому я подозреваю, что ошибка вызвана отсутствием разрешений на системном уровне для повторной синхронизации с контейнером. Я пытаюсь выяснить, нужно ли мне что-то делать с ошибкой или просто игнорировать ее. – Matt

ответ

8

У меня тоже была эта проблема.
Исправлено, перейдя на цель/возможности и включив iCloud и щелкнув по флажку для документов iCloud. Невозможно опубликовать снимок экрана, так как моя репутация слишком низкая.

+0

У меня сейчас работает, просто нужно синхронизировать каждый раз, когда я добавляю, меняю или удаляю. и единственный раз, когда он синхронизируется, когда приложение закрывается. –

+0

Получил то же сообщение, включил опцию в ответе (в Xcode 6.2.4), и он начал подключаться. – vagrant

Смежные вопросы