SQLite データベースの 2 つのテーブルに挿入する必要があるデータがたくさんあります。この作品を背景にしたい。私のフローは基本的に次のようになります: (疑似コード)
while (files in database are not parsed) {
if (fileType == type1) {
parseType1;
showProgress;
}
else {
parseType2;
showProgress;
}
}
私は最新バージョンの FMDB を入手し、次のようにデータをエンキューできると考えました。
FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:[self databasePath]];
BOOL oldshouldcachestatements = _db.shouldCacheStatements;
[_db setShouldCacheStatements:YES];
[_db beginTransaction];
NSString *insertQuery = [[NSString alloc] initWithFormat:@"INSERT INTO %@ values(null, ?, ?, ?, ?);", tableName];
[tableName release];
__block BOOL success;
// Parse each line by tabs
for (NSString *line in lines) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSArray *fields = [line componentsSeparatedByString:@"\t"];
// Need to check since some of the .txt files have an empty newline at the EOF.
if ([fields count] == NUM_FIELDS) {
NSNumber *start = [NSNumber numberWithInteger:[[fields objectAtIndex:1] integerValue]];
NSNumber *end = [NSNumber numberWithInteger:[[fields objectAtIndex:2] integerValue]];
NSNumber *length = [NSNumber numberWithInteger:([end integerValue] - [start integerValue])];
NSArray *argArray = [[NSArray alloc] initWithObjects:ID, start, end, length, nil];
[queue inDatabase:^(FMDatabase *db) {
success = [_db executeUpdate:insertQuery withArgumentsInArray:argArray];
}];
[argArray release];
}
[pool drain];
}
私の parseType メソッドは両方とも次のようになります。単独で実行すると、FMDatabase が現在使用されているというエラーが表示されます。同じメソッドと別々のメソッドの両方をキューに入れる必要があるからですか? 私も次のようなことを試しました:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0) ^{
parseType1;
});
dispatch_async(dispatch_get_main_queue, ^{
update UI;
});
しかし、データベースが現在使用されているという同じ問題が発生します。FMDatabaseQueue を正しく使用していますか? それとも、別のことをする必要がありますか? ここで GCD の代わりに NSOperationQueue を使用すると、より良いでしょうか? ありがとう!