iOS UIManagedDocument:无法打开预加载的持久存储

mac*_*beb 5 core-data preload ios uimanageddocument nspersistentstore

我正在尝试在基于UIManagedDocument的应用程序中预加载持久性存储来处理核心数据.

持久存储我尝试在应用程序B中使用,由于应用程序A"生成"并填充.在应用程序A和BI中都使用Justin Driscoll的UIManagedDocument处理程序(可在此处获得,感谢Mister Driscoll!).所有在app A中完美运行

基于此线程中解释的技术:使用UIManagedDocument在iOS 5中预加载核心数据数据库,我尝试将持久性存储放在B的应用程序包中,并在需要时将此存储复制到文档文件夹中(如果没有完成)在实例化之前的init中.

从捆绑到文档的复制都没问题(我尝试了不同的方法并通过finder和nslog检查了创建),但我无法打开"文档".应用程序不会崩溃,显示视图但表是空的(我使用与app A完全相同的代码,使用相同的fetchedResultsController).首先我认为复制持久存储是空的然后我意识到我只是无法正确打开文档/复制持久存储)=>文档状态= 5,意味着UIDocumentStateClosed和UIDocumentStateSavingError的错误(如果我正确解释它? )

(注意:我也尝试直接从bundle中实例化并打开文档,我遇到了同样的问题:doc state = 5)

所以...三天与这个文件状态= 5战斗,并没有关于修复什么的线索

我想我放入应用程序B包中的文件有问题(目前我从finder拖放到xcode,选中"为任何添加的文件夹创建文件夹引用")也许这是关于某些选项,元数据或文件权限或......

关于调查什么的任何想法?

(我不认为这是关于下面的代码但是......)这是我如何初始化(基于Justin Driscoll处理程序.只有custo是:我检查文档文件夹中是否有商店包,如果不是我创建它在捆绑中的文件)

- (id)init
{
self = [super init];
if (self) {
    self.document = nil;

    NSLog(@"--- INIT ---");

    // On vérifie si il y a un dossier "My-Default-Document-As-Database" (notre persitent store) dans le dossier "Documents"

    NSString *docDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *docFolderPath = [docDirectory stringByAppendingPathComponent:@"My-Default-Document-As-Database"];

    if (![[NSFileManager defaultManager] fileExistsAtPath:docFolderPath]) {
        NSError *error = nil;
        NSLog(@"Pas de fichier trouvé à l'adresse docFolderPath, on va donc y créer notre persistent store");

        // COPY FROM BUNDLE

        NSFileManager *fileManager = [NSFileManager defaultManager];

        NSString *DB = [docFolderPath stringByAppendingPathComponent:@"StoreContent"];

        [fileManager createDirectoryAtPath:DB withIntermediateDirectories:YES attributes:nil error:&error];

        NSLog(@"create directory error: %@",error);

        DB = [DB stringByAppendingPathComponent:@"persistentStore"];

        NSString *shippedDB = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"persistentStore"];

        NSLog(@"%d",[fileManager fileExistsAtPath:shippedDB]);

        [fileManager copyItemAtPath:shippedDB toPath:DB error:&error];

        NSLog(@"Copy error %@",error);

    }

    NSLog(@"== My-Default-Document-As-Database OK DANS DOCUMENTS ==");

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

    myDbUrl = [myDbUrl URLByAppendingPathComponent:@"My-Default-Document-As-Database/"];

    self.document = [[UIManagedDocument alloc] initWithFileURL:myDbUrl];

    NSLog(@"== initWithFileUrl ==");

            // Set our document up for automatic migrations
            NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                                     [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                                     [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
            self.document.persistentStoreOptions = options;


            // Register for notifications
            [[NSNotificationCenter defaultCenter] addObserver:self
                                                     selector:@selector(objectsDidChange:)
                                                         name:NSManagedObjectContextObjectsDidChangeNotification
                                                       object:self.document.managedObjectContext];

            [[NSNotificationCenter defaultCenter] addObserver:self
                                                     selector:@selector(contextDidSave:)
                                                         name:NSManagedObjectContextDidSaveNotification
                                                       object:self.document.managedObjectContext];
}
return self;
}
Run Code Online (Sandbox Code Playgroud)

只有我在Driscoll先生提供的performWithDocument代码上做的"修改"才能看到什么是hapening(doc状态从每次首次打开尝试的1到5然后坚持到5 ......)

- (void)performWithDocument:(OnDocumentReady)onDocumentReady
{  
NSLog(@"passage par performWithDoc");

void (^OnDocumentDidLoad)(BOOL) = ^(BOOL success) {
    NSLog(@"FilePath Apres = %@",[self.document.fileURL path]);
    NSLog(@"STATE === %d", self.document.documentState);
    onDocumentReady(self.document);
};

NSLog(@"FilePath Avant = %@",[self.document.fileURL path]);
NSLog(@"STATE === %d", self.document.documentState);

if (![[NSFileManager defaultManager] fileExistsAtPath:[self.document.fileURL path]]) {
    [self.document saveToURL:self.document.fileURL
            forSaveOperation:UIDocumentSaveForCreating
           completionHandler:OnDocumentDidLoad];
    NSLog(@"performWithDoc > fileexistAtPath nope => saveToURLForCreating");
    NSLog(@"STATE === %d", self.document.documentState);
} else if (self.document.documentState == UIDocumentStateClosed) {
    [self.document openWithCompletionHandler:OnDocumentDidLoad];
    NSLog(@"performWithDoc > UIDocStateClosed => openWithCompletionHandler");
    NSLog(@"STATE === %d", self.document.documentState);
} else if (self.document.documentState == UIDocumentStateNormal) {
    OnDocumentDidLoad(YES);
    NSLog(@"performWithDoc > docState = normal => docdidLoad(YES)");
}
NSLog(@"STATE === %d", self.document.documentState);
}
Run Code Online (Sandbox Code Playgroud)

mac*_*beb 2

感谢一位同伴,这就是答案......如果有人搜索它:

这是关于选择!

添加NSIgnorePersistentStoreVersioningOption到 init.conf 中的 pesistenStore 选项

关于以前的代码,您应该有这样的内容:

// Set our document up for automatic migrations
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
    [NSNumber numberWithBool:YES], NSIgnorePersistentStoreVersioningOption,
    [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
    [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
self.document.persistentStoreOptions = options;
Run Code Online (Sandbox Code Playgroud)