Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9183797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:53:31+00:00 2026-06-17T18:53:31+00:00

I’m trying to pre load a persistent store in an app based on UIManagedDocument

  • 0

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);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T18:53:33+00:00Added an answer on June 17, 2026 at 6:53 pm

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I am confused How to use looping for Json response Array in another Array.
I have a small JavaScript validation script that validates inputs based on Regex. I
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.