使用魔法记录removeUbiquitousContentAndPersistentStoreAtURL“必须传入商店名称”错误

时间:2015-10-20 14:53:55

标签: ios core-data icloud magicalrecord

所以我在我的Core Data / iCloud应用程序中成功使用了Magical Record。 这是我如何初始化它:

- (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]);
                                              }];
}

使用上面的代码正确设置Core Data和iCloud。除了一些通知处理程序,我还可以在本地向核心数据添加记录,并通过iCloud将更改传播到其他设备。

在测试期间,以编程方式重置所有内容会很不错。这是我一直试图根据Apples文档重置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;
    }

无论我尝试什么,删除Ubiquitous的调用都会抛出“必须传入商店名称”的错误。

任何有关这方面的指导都会有所帮助!

1 个答案:

答案 0 :(得分:0)

我认为你有一个拼写错误,vertical_slider.appendChild( range_pitch ); document.body.appendChild( vertical_slider ); 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); } 可能需要一段时间,因此最好从后台调用。如果您只需要它进行开发,那么它也可以从主线程中完成。