2015-10-20 23 views
1

所以我成功地使用魔法記錄與我的核心數據/ iCloud應用程序。 以下是我初始化:removeUbiquitousContentAndPersistentStoreAtURL「必須通過商店名稱」錯誤使用魔法記錄

- (NSString *) iCloudContainerID { 

    return [NSString stringWithFormat:@"iCloud.%@", [[NSBundle mainBundle] bundleIdentifier]]; 
} 

- (NSString *) localStoreName { 

    return @"test.sqlite"; 
} 

- (NSString *) iCloudContentNameKey { 

    return @"test"; 
} 

- (void) setupCoreDataWithiCloud { 

    NSString *containerID = [self iCloudContainerID]; 
    DDLogiCloud(@"Setting up Core Data with iCloud"); 
    [MagicalRecord setupCoreDataStackWithiCloudContainer:containerID 
              contentNameKey:[self iCloudContentNameKey]    // Must not contain dots 
             localStoreNamed:[self localStoreName] 
           cloudStorePathComponent:@"Documents/CloudLogs"  // Subpath within your ubiquitous container that will contain db change logs 
               completion:^{ 
                // This gets executed after all the setup steps are performed 
                // Uncomment the following lines to verify 
                NSLog(@"%@", [MagicalRecord currentStack]); 
                // NSLog(@"%i events", [Event countOfEntities]); 
               }]; 
} 

使用上面的代碼中正確設置了核心數據和iCloud中。除了一些通知處理程序外,我還可以將記錄本地添加到核心數據,並通過iCloud將更改傳播到其他設備。

在測試過程中,以編程方式重置所有內容將會很好。這是我一直在試圖基於蘋果文檔重置CoreData/iCloud的例行:https://developer.apple.com/library/ios/documentation/DataManagement/Conceptual/UsingCoreDataWithiCloudPG/UsingSQLiteStoragewithiCloud/UsingSQLiteStoragewithiCloud.html#//apple_ref/doc/uid/TP40013491-CH3-SW33

- (bool) resetCoreDataWithiCloud { 


     NSString *containerID = [self iCloudContainerID]; 
     NSURL *cloudURL = [NSPersistentStore MR_cloudURLForUbiqutiousContainer:containerID]; 
     NSString *contentNameKey = [self iCloudContentNameKey]; 
     NSDictionary *options = @{contentNameKey:NSPersistentStoreUbiquitousContentNameKey,cloudURL:NSPersistentStoreUbiquitousContentURLKey}; 
     NSURL *storeURL = [NSPersistentStore MR_urlForStoreName:[self localStoreName]]; 
//  NSURL *storeURL = [NSPersistentStore MR_urlForStoreName:[MagicalRecord defaultStoreName]; 
    // [MagicalRecord cleanUp]; 
     DDLogiCloud(@"Resetting CoreData and iCloud named %@ at %@",containerID,storeURL); 
     NSError *error; 
     bool removeResult = [NSPersistentStoreCoordinator removeUbiquitousContentAndPersistentStoreAtURL:storeURL options:options error:&error]; 
     if (removeResult == NO) { 
      DDLogError(@"Could not remove iCloud Container. Reason: %@", error.localizedFailureReason); 
     } 
     return removeResult; 
    } 

無論我怎麼努力,調用removeUbiquitous ...拋出的「錯誤在商店必須通過名稱'。

任何關於此的指導將有所幫助!

回答

0

我認爲你有一個拼寫錯誤,並且options字典中的值和resetCoreDataWithiCloud中的值被交換。

在任何情況下storeOptions應使用設置在首位的店,所以它可能是更好的選擇他們從現有的商店如果可能的話,即以同樣的:

// Get current store options 
NSPersistentStore *store = [NSPersistentStore MR_defaultPersistentStore]; 
NSURL *storeURL = store.URL; 
NSDictionary *storeOptions = store.options; 

// Destroy CoreData stack 
[MagicalRecord cleanUp]; 

// Delete the store 
NSError *error; 
BOOL isDeletedFromiCloud = [NSPersistentStoreCoordinator removeUbiquitousContentAndPersistentStoreAtURL:storeURL options:storeOptions error:&error]; 

// Handle errors 
if(isDeletedFromiCloud) { 
    NSLog(@"Deleted iCloud persistent store."); 
} 
else 
{ 
    NSLog(@"Cannot delete iCloud persistent store. Error: %@", error); 
} 

在實踐removeUbiquitousContentAndPersistentStoreAtURL可能花點時間好好從背景中調用。如果你只是需要它進行開發,那麼從主線程也可以。

相關問題