CoreData Migration Local Store -> iCloud Store

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • CoreData Migration Local Store -> iCloud Store

    Hey :),

    ich steh vor folgendem Problem.
    Ich habe schon erfolgreich mein iCloud MagicalRecord Store erstellt und der funktioniert auch super, wenn man diesen von 0 befüllt :)

    Aber da ich jetzt schon viele Daten habe möchte ich diese natürlich auch mit in die iCloud nehmen.
    Aber wie genau findet die Migration statt.

    Ich habe dazu nur folgenden Thread gefunden der mir aber nicht wirklich weiterhilft.
    MagicalRecord Lightweight Migration UND iCloud

    Quellcode

    1. if ([[NSFileManager defaultManager] ubiquityIdentityToken]) {
    2. [self iCloudCoreDataSetup];
    3. }
    4. else {
    5. NSLog(@"iCloud not enabled");
    6. [MagicalRecord setupAutoMigratingCoreDataStack];
    7. }
    Hier schaue ich ob iCloud verfügbar ist. Davor hab ich CoreData immer mit dem else Zweig initialisiert.

    Hier ist mein Code für die iCloud Installation

    Quellcode

    1. - (void)iCloudCoreDataSetup {
    2. // containerID should contain the same string as your iCloud entitlements
    3. NSString *containerID = [NSString stringWithFormat:@"iCloud.%@", [[NSBundle mainBundle] bundleIdentifier]];
    4. [MagicalRecord setupCoreDataStackWithiCloudContainer:containerID
    5. contentNameKey:@"App" // Must not contain dots
    6. localStoreNamed:@"App.sqlite"
    7. cloudStorePathComponent:@"Documents/CloudLogs" // Subpath within your ubiquitous container that will contain db change logs
    8. completion:^{
    9. // This gets executed after all the setup steps are performed
    10. // Uncomment the following lines to verify
    11. NSLog(@"%@", [MagicalRecord currentStack]);
    12. // NSLog(@"%i events", [Event countOfEntities]);
    13. }];
    14. // NOTE: MagicalRecord's setup is asynchronous, so at this point the default persistent store is still probably NIL!
    15. // Uncomment the following line if you want to check it.
    16. NSLog(@"%@", [MagicalRecord currentStack]);
    17. // The persistent store COORDINATOR is however fully setup and can be accessed
    18. // Uncomment the following line if you want to check it.
    19. NSLog(@"Store coordinator at this point %@", [NSPersistentStoreCoordinator MR_defaultStoreCoordinator]);
    20. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
    21. // Finally, store change notifications must be observed. Without these, your app will NOT function properly!
    22. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
    23. // This notification is issued only once when
    24. // 1) you run your app on a particular device for the first time
    25. // 2) you disable/enable iCloud document storage on a particular device
    26. // usually a couple of seconds after the respective event.
    27. // The notification must be handled on the MAIN thread and synchronously
    28. // (because as soon as it finishes, the persistent store is removed by OS).
    29. // Refer to Apple's documentation for further details
    30. [[NSNotificationCenter defaultCenter] addObserverForName:NSPersistentStoreCoordinatorStoresWillChangeNotification
    31. object:[NSPersistentStoreCoordinator MR_defaultStoreCoordinator]
    32. // queue:nil // Run on the posting (i.e. background) thread
    33. queue:[NSOperationQueue mainQueue] // Run on the main thread
    34. usingBlock:^(NSNotification *note) {
    35. // For debugging only
    36. NSLog(@"%s notificationBlockWillChange:%@, isMainThread = %i", __PRETTY_FUNCTION__, note, [NSThread isMainThread]);
    37. // Disable user interface with setEnabled: or an overlay
    38. // NOTE: Probably not crucial since the store switch is almost instantaneous.
    39. // I only hint it here by changing the tint color to red.
    40. self.window.tintColor = [UIColor redColor];
    41. // Save changes to current MOC and reset it
    42. if ([[NSManagedObjectContext MR_defaultContext] hasChanges]) {
    43. [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
    44. }
    45. [[NSManagedObjectContext MR_defaultContext] reset];
    46. // TODO: Drop any managed object references here
    47. }];
    48. // This notification is issued couple of times every time your app starts
    49. // The notification must be handled on the BACKGROUND thread and asynchronously to prevent deadlock
    50. // Refer to Apple's documentation for further details
    51. [[NSNotificationCenter defaultCenter] addObserverForName:NSPersistentStoreCoordinatorStoresDidChangeNotification
    52. object:[NSPersistentStoreCoordinator MR_defaultStoreCoordinator]
    53. queue:nil // Run on the posting (i.e. background) thread
    54. usingBlock:^(NSNotification *note) {
    55. // For debugging only
    56. NSLog(@"%s notificationBlockDidChange:%@, isMainThread = %i", __PRETTY_FUNCTION__, note, [NSThread isMainThread]);
    57. // This block of code must be executed asynchronously on the main thread!
    58. dispatch_async(dispatch_get_main_queue(), ^{
    59. // Recommended by Apple
    60. [[NSManagedObjectContext MR_defaultContext] reset];
    61. // Notify UI that the data has changes
    62. // NOTE: I am using the same notification that MagicalRecord sends after merging changes
    63. [[NSNotificationCenter defaultCenter] postNotificationName:kMagicalRecordPSCDidCompleteiCloudSetupNotification object:nil];
    64. // Re-enable user interface with setEnabled: or removing the overlay
    65. // NOTE: Probably not crucial since the store switch is almost instantaneous.
    66. // I only hint it here by changing the tint color back to default.
    67. self.window.tintColor = nil;
    68. });
    69. }];
    70. }
    Alles anzeigen
    Aber wie kann ich jetzt die alten Daten vom local Store in die iCloud nehmen? Dazu finde ich leider auch nichts im Netz mit Hilfe von Magical Record :/

    Vielen Dank für eure Hilfe.

    Lg Dennis