I'm getting this message in 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
How do I find the location of this file (iCloudStore.sqlite)? I've tried ~/Library/Containers and ~/Library/Mobile Documents.
Thanks
you can find it at
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;
iCloudStoreURL is what you want.
You create this store from a core data with iCloud demo code. Right?
you have passed this store URL in [coordinator addPersistentStore]
functions.
there are two location and one name you should notice when using iCloud with Core Data:
1.STORE URL
the iCloud store real file, it store all the data in local folder. Every device have a store file. It will automatic transfer data from iCloud.
When adding a iCloud store, you pass this store url to [coordinator addPersistentStore]
function.
Then the store file will locate at that URL.
It should in an local folder, like a subdirectory in documentsDirectory
for example:
[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask].lastObject URLByAppendingComponent:@"iCloudStore"];
or some other directory. My choice is
[[[NSFileManager defaultManager] URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask].lastObject URLByAppendingComponent:@"iCloudStore"];
2.iCloud CoreData Content URL
This URL is in a iCloud container (or code named "ubiquity container").
It is only used for recording the change for the core data content.
Think it in a cloud. [NSFileManager URLForUbiquityContainer:nil]
is the location of that cloud.
iCloud CoreData Content URL
is used for all the iCloud core data databases transfer logs on this ubiquity container. (Different core data databases on different apps maybe in the same ubiquity container and store the logs all in this contentURL
.)
You pass this URL when you set the options for adding iCloud Store:
NSDictionary *cloudOptions =
@{ NSMigratePersistentStoresAutomaticallyOption:@(YES),
NSInferMappingModelAutomaticallyOption :@(YES),
NSPersistentStoreUbiquitousContentNameKey :self.iCloudCoreDataContentName,
NSPersistentStoreUbiquitousContentURLKey :self.iCloudCoreDataContentURL}
this URL should be a subdirectory of the iCloud container, like [[NSFileManager URLForUbiquityContainer:nil] URLByAppendingComponent:@"CoreDataLogs"]
.
this property is optional in options. If you omit it, the iCloud CoreData Content URL will be [NSFileManager URLForUbiquityContainer:nil]
.
3.iCloud CoreData Content Name
Specify an unique name for a core data database.
It is required for every iCloud store. Different iCloud stores should have different names.
You pass this name when you set the options for adding iCloud Store:
NSDictionary *cloudOptions =
@{ NSMigratePersistentStoresAutomaticallyOption:@(YES),
NSInferMappingModelAutomaticallyOption :@(YES),
NSPersistentStoreUbiquitousContentNameKey :self.iCloudCoreDataContentName,
NSPersistentStoreUbiquitousContentURLKey :self.iCloudCoreDataContentURL}