4

UIManagedDocument を使用してデータベースを作成しました。ゆっくりとレコードを追加すると、すべてが機能します。急速に (レコード間で 3 ~ 4 秒) の場合、フェッチ時にレコードが乱れ、アプリの停止/開始中に存続できない可能性があります。

UIManagedDocument は viewDidAppear で作成され、存在しないデータベースをチェックして必要に応じて作成するか、単に開く「useDocument」(以下に示すコード) で変更されます。

繰り返しますが、私の問題は、コア データがすぐに 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's close, so open it.
        } else if ( self.exampleDatabase.documentState == UIDocumentStateClosed ) {
            [self.exampleDatabase openWithCompletionHandler:^(BOOL success) {
                if ( success ) {
                    //
                    // It's 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

0 に答える 0