【问题标题】:removeUbiquitousContentAndPersistentStoreAtURL "Must pass in a store name" error using Magical Record使用 Magical Record 的 removeUbiquitousContentAndPersistentStoreAtURL “Must pass in a store name”错误
【发布时间】:2015-10-20 14:53:55
【问题描述】:

所以我成功地将 Magical Record 与我的 Core Data/iCloud 应用程序一起使用。 这是我初始化它的方式:

- (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 传播到其他设备。

在测试期间,最好以编程方式重置所有内容。这是我一直在尝试根据 Apple Docs 重置 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... 的调用都会引发“必须传入商店名称”的错误。

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

【问题讨论】:

    标签: ios core-data icloud magicalrecord


    【解决方案1】:

    我认为你有一个错字,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 可能需要一段时间,所以最好从后台调用。如果您只需要它进行开发,那么从主线程也可以。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-17
      • 2012-07-09
      • 2017-08-05
      • 2022-06-24
      • 1970-01-01
      • 2014-04-20
      • 2012-12-15
      • 2013-04-24
      相关资源
      最近更新 更多