ioscore-datapreloaduimanageddocumentnspersistentstore

iOS UIManagedDocument : can't open pre loaded persistent store


I'm trying to pre load a persistent store in an app based on UIManagedDocument to deal with core data.

Persistent store I try to use in app B, is "generated" and populated thanks to app A. In both app A and B I use the UIManagedDocument handler from Justin Driscoll (available here, thanks Mister Driscoll !). All works perfectly in app A.

Based on the technique explained in this thread : Pre-load core data database in iOS 5 with UIManagedDocument, I try to put the persistent store in the app bundle of B, and to copy this store in the documents folder if needed (if not done before) in init just before instantiating.

Copy from bundle to documents is all ok (I tried different method and checked the creation thanks to finder and nslog), but I just can't open the "document". App won't crash, views are displayed but tables are empty (I use exactly same code as app A, with same fetchedResultsController). First I thought that copied persistent store was empty then I realized that I just can't open correctly the document/copied persistent store) => Document state = 5, meaning an error of UIDocumentStateClosed and UIDocumentStateSavingError (if I correctly interpret it ???)

(Note: I've also tried to instantiate and open the document directly from the bundle and I've got the same problem : doc state = 5)

So... Three days fighting with this document state = 5 and no clue about what to fix

I imagine that there's something wrong with the file I put in the bundle of app B (currently I drag and drop from finder to xcode with "Create folder references for any added folders" selected) Maybe it's about some options, or metadata, or file permissions or...

Any idea about what to investigate ?

(I don't think it's about the following code but well...) Here's how I init (based on Justin Driscoll handler. Only custo is : I check if there's a store package in the documents folder, if not I create it based on the file in the bundle)

- (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;
}

Only "modifications" I made on the performWithDocument code provided by Mr Driscoll are some nslog to see what's hapening (doc state go from 1 to 5 on every first open try and then stick to 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);
}

Solution

  • Thanks to a compadre, here's the answer ... if somebody search for it :

    It was about options !!

    Add NSIgnorePersistentStoreVersioningOption to the pesistenStore options in the init.

    Regarding previous code, you should have something like this :

    // 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;