我有一个用例,在ios中将数据写入本地couchebase数据库。这里不支持写操作的并发访问。所以我想在主线程上运行CRUD操作并在辅助线程上运行一些算法后返回结果。当主线程接管控制并执行代码时,当前正在运行的线程不会等待主线程完成其操作。我怎样才能从主线程结果切换到其他线程。阻止当前线程,直到部分代码在iOS主线程上运行
例:
+(BOOL)createDocument:(NSDictionary*)data withId:(NSString*)docId {
__block CBLDocument* doc = nil;
// NSLog(@"%d count ", [[self database] documentCount]);
dispatch_async(dispatch_get_main_queue(), ^{
if(docId.length > 0) {
doc = [[self getDatabase] documentWithID:docId];
} else {
doc = [[self getDatabase] createDocument];
}
});
//I want current thread to wait till main thread completes its execution
if(doc){
return YES;
}else{
return NO;
}
}