使用FMDB:如何同时访问主线程和后台线程中的sqlite

时间:2016-01-19 10:44:25

标签: ios multithreading sqlite fmdb

在后台线程中使用FMDatabaseQueue

插入批量数据(超过10,000条记录)
FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:[self dbPath]];

queue inTransaction:^(FMDatabase *db) {

    for (NSDictionary *propertyValueDictionary in customers) {
        NSString *customerId = [propertyValueDictionary objectForKey:@"CustomerId"];
        NSString *property = [propertyValueDictionary objectForKey:@"Property"];
        NSString *value =  [propertyValueDictionary objectForKey:@"Value"];
        BOOL deleted = [[propertyValueDictionary objectForKey:@"Deleted"] boolValue];
        NSString *revision = [propertyValueDictionary objectForKey:@"Revision"];

        NSString *sql = @"INSERT OR REPLACE INTO Customer (CustomerId, Property, Value) VALUES (?, ?, ?);";
        [db executeUpdate:sql, customerId, property, value];

    }
}];

同时,我想在主线程中读/写sqlite而不会延迟并阻止UI。

任何解决方案?提前谢谢。

0 个答案:

没有答案