【问题标题】:Ensembles: MagicalRecord and iCloud合奏:MagicalRecord 和 iCloud
【发布时间】:2016-05-18 14:17:17
【问题描述】:

我正在尝试将 iCloud 集成到我的旧项目中,但同时尝试使用 MagicalRecord 更新数据库。当我不使用自定义 .sqlite 文件名时,iCloud 可以工作,但不能使用自定义文件名。这里有一些代码可以更好地解释我的意思。

// Load model. Don't use the standard 'merged model' of Magical Record, because that would include
// the Ensembles model. Don't want to merge models.
NSManagedObjectModel *model = [NSManagedObjectModel MR_newManagedObjectModelNamed:@"Polyglott.momd"];
[NSManagedObjectModel MR_setDefaultManagedObjectModel:model];

// Setup Core Data Stack
[MagicalRecord setShouldAutoCreateManagedObjectModel:NO];

// This is the custom file name
[MagicalRecord setupCoreDataStackWithAutoMigratingSqliteStoreNamed:@"PolyglottModel"];
// Here is the default one
// [MagicalRecord setupAutoMigratingCoreDataStack];

// Setup Ensemble

// Here, I'm not sure which one is the right way
NSURL *url = [NSPersistentStore MR_urlForStoreName:@"PolyglottModel.sqlite"];
// NSURL *url = [NSPersistentStore MR_urlForStoreName:[MagicalRecord defaultStoreName]];

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Polyglott" withExtension:@"momd"];
self.cloudFileSystem = [[CDEICloudFileSystem alloc] initWithUbiquityContainerIdentifier:@"iCloud.de.thm.polyglott"];
self.ensemble = [[CDEPersistentStoreEnsemble alloc] initWithEnsembleIdentifier:@"PolyglottMR"
                                                            persistentStoreURL:url
                                                         managedObjectModelURL:modelURL
                                                               cloudFileSystem:self.cloudFileSystem];
self.ensemble.delegate = self;

// Listen for local saves, and trigger merges
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(localSaveOccurred:) name:CDEMonitoredManagedObjectContextDidSaveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cloudDataDidDownload:) name:CDEICloudFileSystemDidDownloadFilesNotification object:nil];

[self syncWithCompletion:nil];

leechPersistentStoreWithCompletion 和 mergeWithCompletion 都会给我任何错误,但是当我请求数据库时,没有数据。

有什么想法或例子吗?非常感谢您。

【问题讨论】:

    标签: objective-c icloud magicalrecord ensembles


    【解决方案1】:

    我假设您已经查看了 Ensembles 中的 MagicalRecord 示例代码。

    我不确定到底出了什么问题,但我猜你没有将正确的名称传递给MR_urlForStoreName:。您可能应该放弃扩展名或类似的东西。通过设置断点并打印 URL 以查看它是否正确,应该很容易看到。

    【讨论】:

    • 你说得对,我是按照示例代码来的。我尝试了两个名称,有和没有.sqlite。实际上,检查我在没有扩展名的情况下看到的路径。无论如何,它也不起作用。最后,我决定将存储从自定义文件复制到默认文件,以进行更新,然后从那里使用默认文件。这是一个有点棘手但快速的解决方案。谢谢!
    猜你喜欢
    • 2012-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-03
    • 2017-10-25
    • 1970-01-01
    • 1970-01-01
    • 2015-06-13
    相关资源
    最近更新 更多