Fast persistent storage for the database from UIManagedDocument

I created the database through UIManagedDocument. If I slowly add entries, everything works. If I quickly (3-4 seconds between records), the records fail when they are removed and may not be saved when the application is stopped / started.

A UIManagedDocument is created in viewDidAppear and modified into "useDocument" (code below), which checks for a non-existent database and creates it if necessary or simply opens it.

Again, I think my problem is that the main data is not immediately stored in SQLite.

-(void)setExampleDatabase:(UIManagedDocument *)exampleDatabase { if ( _exampleDatabase != exampleDatabase ) { _exampleDatabase = exampleDatabase; NSMutableDictionary *pragmaOptions = [NSMutableDictionary dictionary]; [pragmaOptions setObject:@"FULL" forKey:@"synchronous"]; [pragmaOptions setObject:@"1" forKey:@"fullfsync"]; NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: pragmaOptions, NSSQLitePragmasOption, [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES, NSInferMappingModelAutomaticallyOption, nil]; exampleDatabase.persistentStoreOptions = options; // // Does the file exist?? Is not, create it. if ( ! [[NSFileManager defaultManager] fileExistsAtPath: [self.exampleDatabase.fileURL path]] ) { [self.exampleDatabase saveToURL:self.exampleDatabase.fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) { // // OK, the creation has been done, enable the add button so records can be created self.addButton.enabled = YES; }]; // // The file exists, but it close, so open it. } else if ( self.exampleDatabase.documentState == UIDocumentStateClosed ) { [self.exampleDatabase openWithCompletionHandler:^(BOOL success) { if ( success ) { // // It now successfully opened, enable the add button, load the data from // the core data database and cause the objects to be displayed in the table view // "getCounts" is immediately below this method self.addButton.enabled = YES; [self getCounts:self.exampleDatabase.managedObjectContext]; [self.tableView reloadData]; } else { NSLog(@"Error opening a closed database"); } } ]; } else if ( self.exampleDatabase.documentState == UIDocumentStateNormal ) { // // OK, the database is opened. Enable the add button, load the data from // the core data database and cause the objects to be displayed in the table view // "getCounts" is immediately below this method self.addButton.enabled = YES; [self getCounts:self.exampleDatabase.managedObjectContext]; [self.tableView reloadData]; } else { NSLog(@"Something is wrong in useDocument - it exists in an unknown state"); exit(1); } } } - (void)viewDidLoad { [super viewDidLoad]; if ( ! self.exampleDatabase ) { NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; url = [url URLByAppendingPathComponent:@"DefaultDb"]; self.exampleDatabase = [[UIManagedDocument alloc] initWithFileURL:url]; } } 
+4
source share

Source: https://habr.com/ru/post/1446532/


All Articles