Xcode - "提供的普遍名称已被使用" - 如何找到文件夹

Dav*_*nte 1 xcode icloud

我在Xcode中收到此消息:

    The provided ubiquity name is already in use., 
NSURL=file://localhost/var/mobile/Applications/6C748748-9689-4F40-B8D7-
CDE8CA280FF8/Documents/SharedCoreDataStores/138F8194-DCC7-4D66-859B-
B2C35BDF2984/iCloudStore.sqlite
Run Code Online (Sandbox Code Playgroud)

如何找到此文件的位置(iCloudStore.sqlite)?我试过〜/ Library/Containers和〜/ Library/Mobile Documents.

谢谢

fro*_*cjn 6

你可以找到它

NSURL *DocumentsURL = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask].lastObject;

NSURL *tempURL = DocumentsURL;
tempURL = [tempURL URLByAppendingPathComponent:@"sharedCoreDataStores"];
tempURL = [tempURL URLByAppendingPathComponent:@"138F8194-DCC7-4D66-859B-B2C35BDF2984"];
tempURL = [tempURL URLByAppendingPathComponent:@"iCloudStore.sqlite"];
NSURL *iCloudStoreURL = tempURL;
Run Code Online (Sandbox Code Playgroud)

iCloudStoreURL就是你想要的.您可以使用iCloud演示代码从核心数据创建此商店.对?

您已在[coordinator addPersistentStore]函数中传递此商店URL .


将iCloud与Core Data一起使用时,您应该注意两个位置和一个名称:

1. 商店网址

iCloud存储真实文件,它将所有数据存储在本地文件夹中.每个设备都有一个商店文件.它将自动从iCloud传输数据.

添加iCloud存储时,您将此存储URL传递给[coordinator addPersistentStore]函数.

然后商店文件将定位在该URL.

它应该在本地文件夹中,如documentsDirectory中的子目录

for example: 
[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask].lastObject URLByAppendingComponent:@"iCloudStore"];
Run Code Online (Sandbox Code Playgroud)

或其他一些目录.我的选择是

[[[NSFileManager defaultManager] URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask].lastObject URLByAppendingComponent:@"iCloudStore"];
Run Code Online (Sandbox Code Playgroud)

2. iCloud CoreData内容URL

此URL位于iCloud容器(或名为"ubiquity容器")的代码中.它仅用于记录核心数据内容的更改.

想想它在云端.[NSFileManager URLForUbiquityContainer:nil]是云的位置.

iCloud CoreData Content URL用于所有iCloud核心数据数据库传输此普遍容器上的日志.(不同应用程序上的不同核心数据数据库可能位于相同的普遍容器中,并将日志存储在此中contentURL.)

在设置添加iCloud Store的选项时传递此URL:

 NSDictionary *cloudOptions =
 @{  NSMigratePersistentStoresAutomaticallyOption:@(YES),
 NSInferMappingModelAutomaticallyOption      :@(YES),
 NSPersistentStoreUbiquitousContentNameKey   :self.iCloudCoreDataContentName,
 NSPersistentStoreUbiquitousContentURLKey    :self.iCloudCoreDataContentURL}
Run Code Online (Sandbox Code Playgroud)

此URL应该是iCloud容器的子目录,例如[[NSFileManager URLForUbiquityContainer:nil] URLByAppendingComponent:@"CoreDataLogs"].

此属性在选项中是可选的.如果省略它,iCloud CoreData内容URL将是[NSFileManager URLForUbiquityContainer:nil].

3. iCloud CoreData内容名称

指定核心数据数据库的唯一名称.每个iCloud商店都需要它.不同的iCloud商店应该有不同的名称.

在设置添加iCloud Store的选项时传递此名称:

 NSDictionary *cloudOptions =
 @{  NSMigratePersistentStoresAutomaticallyOption:@(YES),
 NSInferMappingModelAutomaticallyOption      :@(YES),
 NSPersistentStoreUbiquitousContentNameKey   :self.iCloudCoreDataContentName,
 NSPersistentStoreUbiquitousContentURLKey    :self.iCloudCoreDataContentURL}
Run Code Online (Sandbox Code Playgroud)