4

我通过 UIManagedDocument 创建了一个数据库。如果我慢慢添加记录,一切正常。如果我快速(记录之间的 3-4 秒),记录在获取时会出现乱序,并且可能无法在应用程序停止/启动时继续存在。

UIManagedDocument 在 vi​​ewDidAppear 中创建并在“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