2015-07-15 12 views
5

In meiner App für iOS habe ich meine Datenbank für die Synchronisierung mit iCloud eingestellt. Wenn ich einen neuen Datensatz einlege, werden die Daten korrekt aktualisiert und ich kann es in meinem Container-Ordner ansehen. Jetzt möchte ich einen Schalter, um Benutzer aktivieren oder deaktivieren Sie die Core-Daten synchronisieren Prozess mit iCloud während App läuft, aber ich weiß nicht, wie dies zu tun.iOS ICloud Core Data aus der App

Ich bin neu mit iOS und ICloud. Brauche Hilfe bitte. Danke im Voraus.

Dies ist mein Code in AppDelegate:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator 
{ 
    if (_persistentStoreCoordinator != nil) { 
     return _persistentStoreCoordinator; 
    } 

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"myStore.sqlite"];  
    NSURL *cloudRootURL=[fileManager URLForUbiquityContainerIdentifier:nil]; 

    NSString *pathToCloudFile = [[cloudRootURL path]stringByAppendingPathComponent:@"Documents"]; 
    pathToCloudFile = [pathToCloudFile stringByAppendingPathComponent:@"myCloudLogs"]; 

    NSURL *cloudURL = [NSURL fileURLWithPath:pathToCloudFile]; 

    NSString *cloudStoreTitle = @"myStoreCloud"; 
    NSDictionary *options = @{NSMigratePersistentStoresAutomaticallyOption : @YES, 
          NSInferMappingModelAutomaticallyOption : @YES, 
          NSPersistentStoreUbiquitousContentURLKey: cloudURL, 
          NSPersistentStoreUbiquitousContentNameKey: cloudStoreTitle}; 

    NSError *error = nil; 
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; 


    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&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. 

     //Typical reasons for an error here include: 
     // The persistent store is not accessible; 
     // The schema for the persistent store is incompatible with current managed object model. 
     //Check the error message to determine what the actual problem was. 


     //If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory. 

     //If you encounter schema incompatibility errors during development, you can reduce their frequency by: 
     // Simply deleting the existing store: 
     //[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil] 

     // Performing automatic lightweight migration by passing the following dictionary as the options parameter: 
     //@{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES} 

     //Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details. 


     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     abort(); 
    } 

    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter]; 

    [notificationCenter addObserver:self 
         selector:@selector(processStoresWillChange:) 
          name:NSPersistentStoreCoordinatorStoresWillChangeNotification 
         object:_persistentStoreCoordinator]; 

    [notificationCenter addObserver:self 
         selector:@selector(processStoresDidChange:) 
          name:NSPersistentStoreCoordinatorStoresDidChangeNotification 
         object:_persistentStoreCoordinator]; 

    [notificationCenter addObserver:self 
         selector:@selector(processContentChanges:) 
          name:NSPersistentStoreDidImportUbiquitousContentChangesNotification 
         object:_persistentStoreCoordinator]; 


    return _persistentStoreCoordinator; 
} 
+0

Ich glaube nicht, dass Sie es tun können. Eigentlich verstehe ich, dass ich einen persistenten Speicher habe, der entweder iCloud-fähig ist oder nicht (d. H. Du definierst ihn, wenn du installierst, dass die storeOptions-Werte null sind oder einen Wert enthalten). Daher würde ich vorschlagen, dass Sie alle Änderungen speichern und dann den Kontext des verwalteten Objekts zurücksetzen und den persistenten Speicher entfernen und einen neuen persistenten Speicher mit storeOption = nil einrichten müssen (der dann nicht iCloud aktiviert ist). Wenn Sie möchten, kann ich Ihnen einen Beispielcode geben. – Red

Antwort

0

nicht Allgegenwart Behälter verwenden. Sehen Sie sich die Apple-Dokumentation zum Ausschließen der Datei von der iCloud-Synchronisierung an. https://developer.apple.com/library/ios/qa/qa1719/_index.html Wenn Sie den bool-Wert auf NO setzen. Die Datei wird wieder in den zu sichernden Dateien enthalten sein. Erstellen Sie die Kerndaten (Datenbank) in Dokumenten, um sie automatisch zu synchronisieren. Mit diesem Code können Sie die Synchronisierung programmgesteuert jederzeit auf Knopfdruck deaktivieren. Aber sei gewarnt, dass die App möglicherweise abgelehnt wird, wenn die Datenbankgröße zu groß wird, da nur "Benutzerdaten" mit icloud synchronisiert werden sollen.

Verwandte Themen