-
Notifications
You must be signed in to change notification settings - Fork 0
多线程下的使用
Karim Chang edited this page Dec 1, 2018
·
3 revisions
FFDB
多线程下的封装也是基于FMDB下的FMDatabaseQueue
实现的。
对应在FFDB
中,封装成FFDBSafeOperation
和FFDBTransaction
,两个类的接口都是接近一样的,所以下面的讲解都是根据FFDBTransaction
。
在多线程下做任何数据库调用各自的方法都需要放到主线程中执行
❌这是错误的做法:
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSMutableArray *array = [NSMutableArray array];
for (int i = 0; i<100; i++) {
ClassRoom *room = [[ClassRoom alloc]init];
[array addObject:room];
[FFDBTransaction insertObjectList:array isRollBack:YES completion:nil];
}
});
✅这是正确的做法:
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSMutableArray *array = [NSMutableArray array];
for (int i = 0; i<100; i++) {
ClassRoom *room = [[ClassRoom alloc]init];
[array addObject:room];
[[NSOperationQueue mainQueue]addOperationWithBlock:^{
[FFDBTransaction insertObjectList:array isRollBack:YES completion:nil];
}];
}
});
- 插入: 目前提供的方法:
+ (void)insertObjectList:(NSArray <__kindof FFDataBaseModel *>*)objectList
isRollBack:(BOOL)isRollBack
completion:(UpdateResult)block;
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSMutableArray *array = [NSMutableArray array];
for (int i = 0; i<100; i++) {
ClassRoom *room = [[ClassRoom alloc]init];
[array addObject:room];
[[NSOperationQueue mainQueue]addOperationWithBlock:^{
[FFDBTransaction insertObjectList:array isRollBack:YES completion:nil];
}];
}
});
- 更新: 目前提供的方法:
+ (void)updateObjectList:(NSArray<__kindof FFDataBaseModel *> *)objectList
isRollBack:(BOOL)isRollBack
completion:(UpdateResult)block;
+ (void)updateFromClass:(Class)dbClass
set:(NSArray <NSString *>*)setColumns
where:(NSString *)whereFormat
values:(NSArray <id>*)values
isRollBack:(BOOL)isRollBack
completion:(UpdateResult)block;
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSMutableArray *array = [FFDBTransaction selectObjectWithFFDBClass:[ClassRoom class]];
for (int i = 0; i<array.count; i++) {
ClassRoom *room = array[i];
room.name = "I'm update";
[array addObject:room];
[[NSOperationQueue mainQueue]addOperationWithBlock:^{
[FFDBTransaction updateObjectList:array isRollBack:YES completion:nil];
}];
}
});
- 删除: 目前提供的方法:
+ (void)deleteObjectList:(NSArray<__kindof FFDataBaseModel *> *)objectList
isRollBack:(BOOL)isRollBack
completion:(UpdateResult)block;
+ (void)deleteFromClass:(Class)dbClass
where:(NSString *)whereFormat
values:(NSArray <id>*)values
isRollBack:(BOOL)isRollBack
completion:(UpdateResult)block;
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSMutableArray *array = [FFDBTransaction selectObjectWithFFDBClass:[ClassRoom class]];
for (int i = 0; i<array.count; i++) {
ClassRoom *room = array[i];
room.name = "I'm update";
[array addObject:room];
[[NSOperationQueue mainQueue]addOperationWithBlock:^{
[FFDBTransaction deleteObjectList:array isRollBack:YES completion:nil];
}];
}
});
- 查询
+ (void)selectAllObjectFromClass:(Class)dbClass
completion:(QueryResult)block;
+ (void)selectFromClass:(Class)dbClass
columns:(NSArray <NSString *>*)columns
where:(NSString *)whereFormat
values:(NSArray <id>*)values
toClass:(Class)toClass
completion:(QueryResult)block;
NSMutableArray *array = [FFDBTransaction selectAllObjectFromClass:[ClassRoom class] completion:nil];