Ответ 1
Здесь вам нужно будет
- Создайте локальный NSPsistentStoreCoordinator
- Добавьте существующий постоянный магазин к этому координатору и сохраните ссылку на это новое возвращенное хранилище.
- Вызовите этот удобный migratePersistStore:... предоставление хранилища из # 2, URL-адрес хранилища в каталоге документов с другим именем файла и всеми важными параметрами, включая ключ NSPersistentStoreUbiquitousContentNameKey.
Здесь код, примечания в строке.
NSURL *documentsDirectory = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
//This is the path to the new store. Note it has a different file name
NSURL *storeURL = [documentsDirectory URLByAppendingPathComponent:@"TestRemote.sqlite"];
//This is the path to the existing store
NSURL *seedStoreURL = [documentsDirectory URLByAppendingPathComponent:@"Test.sqlite"];
//You should create a new store here instead of using the one you presumably already have access to
NSPersistentStoreCoordinator *coord = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel];
NSError *seedStoreError;
NSDictionary *seedStoreOptions = @{ NSReadOnlyPersistentStoreOption: @YES };
NSPersistentStore *seedStore = [coord addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:seedStoreURL
options:seedStoreOptions
error:&seedStoreError];
NSDictionary *iCloudOptions = @{ NSPersistentStoreUbiquitousContentNameKey: @"MyiCloudStore" };
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
//This is using an operation queue because this happens synchronously
[queue addOperationWithBlock:^{
NSError *blockError;
[coord migratePersistentStore:seedStore
toURL:storeURL
options:iCloudOptions
withType:NSSQLiteStoreType
error:&blockError];
NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
[mainQueue addOperationWithBlock:^{
// This will be called when the migration is done
}];
}];
Обратите внимание, что после этой миграции вам необходимо настроить постоянное хранилище, которое вы используете с вашим MOC, с новым URL-адресом и всегда включать iCloudOptions выше с ключом NSPersistentStoreUbiquitousContentNameKey.
Это было основано на документации Apple.
После завершения вы должны увидеть новую папку в папке "Документы" в папке симулятора (~/Library/Application Support/iPhone Simulator/...) с надписью CoreDataUbiquitySupport. Вложенное в глубину ваше хранилище sqlite, синхронизированное с iCloud.
Тада!
EDIT: О, и убедитесь, что вы создали право на iCloud и включили его в свой комплект. Вы должны иметь возможность делать это в Xcode, но вы также можете обновить его на портале разработки.