Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I was having the same problem.</p> <p>Are you trying to open the document inside viewDidLoad?</p> <p>Try moving the code to another method. It solved the problem for me.</p> <p>in ViewController.h</p> <pre><code>@property (nonatomic,strong) NSManagedObjectContext *managedObjectContext; @property (nonatomic,strong) UIManagedDocument *document; </code></pre> <p>in ViewController.m</p> <pre><code>@synthesize managedObjectContext = _managedObjectContext; @synthesize document = _document; - (void)viewDidLoad { [super viewDidLoad]; // Do not try to open the document here // Call another method instead :D if (!_managedObjectContext) { [self createContext]; } } - (void)createContext { NSFileManager *fileManager = [NSFileManager defaultManager]; NSURL *url = [[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; url = [url URLByAppendingPathComponent:@"Database"]; self.document = [[UIManagedDocument alloc] initWithFileURL:url]; // FILE DOES NOT EXIST - Let's create a new one if (![[NSFileManager defaultManager] fileExistsAtPath:[url path]]) { [self.document saveToURL:url forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) { if (success) { self.managedObjectContext = self.document.managedObjectContext; } else { NSLog(@"ERROR: Cannot create new document"); } }]; // FILE IS CLOSED - Let's open it } else if (self.document.documentState == UIDocumentStateClosed) { [self.document openWithCompletionHandler:^(BOOL success) { if (success) { self.managedObjectContext = self.document.managedObjectContext; } else { NSLog(@"File is closed and it wont open!"); } }]; // FILE EXISTS AND IS OPENED - Yay! } else { self.managedObjectContext = self.document.managedObjectContext; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload