如果libsqlite不是线程安全的那样的代码
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
__block NSArray *__albumsCollection = albumCollections;
dispatch_apply(count, queue, ^(size_t i)
{
MPMediaItem *albumObj = [[__albumsCollection objectAtIndex:i] representativeItem];
///// making some sqlite queries
});
会引发BAD_EXEC。
那么如何使这段代码线程安全?
我的解决方案是使用主队列
dispatch_apply(count, dispatch_get_main_queue(), ^(size_t i)
{
/// my sqllite queries
});
但我对此并不满意。如何让它更好?
答案 0 :(得分:4)
而不是使用dispatch_get_main_queue()
获取主队列,您可能希望在非主线程上创建单独的private dispatch queue,如下所示:
dispatch_queue_t queue = dispatch_queue_create("com.example.MyQueue", DISPATCH_QUEUE_SERIAL); // or NULL as last parameter if prior to OS X 10.7/iOS 5.0
dispatch_apply(count, queue, ^(size_t i) {
/// your SQLite queries
});
或者你可以使用来自Gus Mueller(@ccgus)精彩FMDB SQLite包装器框架的FMDatabaseQueue
(这就是我要做的):
FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:aPath];
[queue inDatabase:^(FMDatabase *db) {
// Your SQLite queries:
[db executeQuery:@"...", ...];
...
}];
...它会将您的查询块发送到串行调度队列,同步包装它的执行。
还不确定吗?
[queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
// Your SQLite queries:
[db executeQuery:@"...", ...];
...
}];
现在怎么样?
答案 1 :(得分:0)
dispatch_queue_t q=dispatch_queue_create("name", NULL);
dispatch_sync(q, ^{
//your code is here
});
dispatch_async(dispatch_get_main_queue(), ^(void) {
//as soon as above block is completed, this block executed and you will be notified that
//work is completed
});