1.使用信号量
//先创建一个semaphore
dispatch_semaphore_t semaphore = dispatch_semaphore_create(1);
__block NSMutableArray *modelArr = [[NSMutableArray alloc]init];
DataBaseTool *tool = [DataBaseTool sharedInstance];
[tool inDatabase:^(FMDatabase *db) {
FMResultSet *set = [db executeQuery:@"select * from user_data where redid = ?",redid];
while ([set next]) {
UserModel *model = [[UserModel alloc]init];
model.userid = [set stringForColumnIndex:1];
model.redid = [set stringForColumnIndex:2];
model.status = [set doubleForColumnIndex:3];
[modelArr addObject:model];
}
[set close];
//发出已完成的信号
dispatch_semaphore_signal(semaphore);
}];
//等待执行,不会占用资源
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
return modeler;
2.使用dispatch_group
dispatch_group_t downloadGroup = dispatch_group_create();
//成对出现 任务开始时
dispatch_group_enter(downloadGroup);
//成对出现 任务结束时
dispatch_group_leave(downloadGroup);
dispatch_group_notify(downloadGroup, dispatch_get_main_queue(), ^{
});