如何覆盖 UIManagedDocument 中的 NSPersistentStoreCoordinator

Posted

技术标签:

【中文标题】如何覆盖 UIManagedDocument 中的 NSPersistentStoreCoordinator【英文标题】:How to override the NSPersistentStoreCoordinator in UIManagedDocument 【发布时间】:2014-07-04 02:56:09 【问题描述】:

我有一个通过 UIManagedObjectDocument 使用 Core Data 的应用程序。我正在尝试使用加密核心数据 (https://github.com/project-imas/encrypted-core-data) 为基础 SQLite 数据库添加加密。从 ECD 的描述中,我需要创建一种新类型的 NSPersistentSroreCoordinator。但是,我似乎无法使用 UIManagedObjectDocument 执行此操作,因为它会创建自己的内部 NSPersistenStoreCoordinator(标记为私有)。

我用这一行创建数据库:

UIManagedDocument* managedDoc = [[UIManagedDocument alloc] initWithFileURL:url];

我尝试继承 UIManagedDocument 并以这种方式创建它,但没有成功:

UIManagedDocument* managedDoc = [[EncryptedManagedDocument alloc] initWithFileURL:url];

还有我的课程实现:

@interface EncryptedManagedDocument()
@property (nonatomic,retain,readonly) NSPersistentStoreCoordinator *encryptedStoreCoordinator;
@end

@implementation EncryptedManagedDocument

@synthesize encryptedStoreCoordinator = _encryptedStoreCoordinator;

-(NSPersistentStoreCoordinator*)encryptedStoreCoordinator

    if (_encryptedStoreCoordinator)
        return _encryptedStoreCoordinator;

    _encryptedStoreCoordinator = [EncryptedStore makeStore:[self managedObjectModel]:@"SOME_PASSCODE"];
    return _encryptedStoreCoordinator;


-(NSPersistentStoreCoordinator*)persistentStoreCoordinator

    return self.encryptedStoreCoordinator;

@end

有人知道正确的方法吗?

谢谢!

【问题讨论】:

【参考方案1】:

我要说这是不可能的。 UIManagedDocument 很好地包装了所有内容,对于常见情况可以节省大量时间,但为了实现我的场景,我创建了一个类似于 UIManagedDocument 的 EncryptedManagedDocument 类,但让我可以控制创建自己的持久存储协调器。

谢谢大家。

【讨论】:

以上是关于如何覆盖 UIManagedDocument 中的 NSPersistentStoreCoordinator的主要内容,如果未能解决你的问题,请参考以下文章