FMDB使用Queue:FMDatabaseQueue

使用 result:( void (^)( BOOL sucess))done是判断上一次block执行完成,才能执行下一个,不然不会造成死锁

在使用时,如果在queue里面的block执行过程中,又调用了 indatabase方法,则会检查 是不是同一个queue,如果是同一个queue会死锁;原因很简单:

队列里面 放了一个block,该block又在 本队列 后面放了一个 block;

从而:前一个block 里面 调用了 后一个block,必须等后一个block执行完成了,

前一个block才会 出队列;

而后一个block想要执行,则又必须先等 前一个block出队列;

因此 死锁!!!!


/**
 * @ 函数名称: saveUDFmdbContent:toTable:
 * @
函数描述:保存数据到数据库指定的表
 * @
输入参数: model ,要保存的数据,类型默认为 NSDictionary
            tableName
:遥操作的表
 * @
输出参数: nil
 * @
返回值:   nil
 */


- (
void )saveUDFmdbContent:( id )model toTable:( NSString *)tableName
{
    [
self saveUDFmdbContent :model toTable :tableName result :^( BOOL sucess) {
       
if (sucess)
        {
            [[
NSNotificationCenter defaultCenter ] postNotificationName : kChatListChangeNotification object : nil ] ;
        }
    }];
}

- (
void )saveUDFmdbContent:( id )model toTable:( NSString *)tableName result:( void (^)( BOOL sucess))done
{
   
__block BOOL is_exculate_success = NO ;
   
_tableName = tableName;
   
NSMutableString * query = [ NSMutableString stringWithFormat : @"INSERT INTO %@" ,tableName];
   
NSMutableString * keys = [ NSMutableString stringWithFormat : @" (" ];
   
NSMutableString * values = [ NSMutableString stringWithFormat : @" ( " ];
   
NSMutableArray * arguments = [ NSMutableArray arrayWithCapacity : 0 ];
   
if ([model allKeys ]) {
       
for ( NSString * key in [model allKeys ]) {
            [keys
appendFormat : @"%@," ,key];
            [values
appendString : @"?," ];
            [arguments
addObject :[model objectForKey :key]];
        }
    }
    [keys
appendString : @")" ];
    [values
appendString : @")" ];
    [query
appendFormat : @" %@ VALUES%@" ,
     [keys
stringByReplacingOccurrencesOfString : @",)" withString : @")" ],
     [values
stringByReplacingOccurrencesOfString : @",)" withString : @")" ]];
    [
_db inDatabase :^( FMDatabase *db) {
       
@try {
           
BOOL savebool = [db executeUpdate :query withArgumentsInArray :arguments];
            is_exculate_success =
YES ;
           
if (savebool)
            {
               
NSLog ( @" 保存成功 " );
               
            }
        }
       
@catch (NSException *exception) {
            [db
rollback ];
            is_exculate_success =
NO ;
        }
       
@finally {
            [db
commit ];
        }
    }];
   
if (done)
    {
        done(is_exculate_success);
    }
}
#pragma mark - 删除数据

/**
 * @
函数名称:deleteDateRecordWithField:Value:
 * @
函数描述:从默认表中删除数据
 * @
输入参数:field/value :指定删除条件field = 'value'
 * @
输出参数:nil
 * @
返回值:  BOOL
 *          
成功:YES  失败:NO
 */

//删除数据
- (
void)deleteDateRecordWithField:(NSString *)field Value:(id)value
{
    [
self deleteDateRecordFromTable:_tableName Field:field Value:value];
}

/**
 * @
函数名称:deleteDateRecordFromTable:Field:Value:
 * @
函数描述:从指定表中删除数据
 * @
输入参数:field/value :指定删除条件field = 'value'
             tableName:
制定操作的表
 * @
输出参数:nil
 * @
返回值:  BOOL
 *          
成功:YES  失败:NO
 */

- (
void)deleteDateRecordFromTable:(NSString *)tableName Field:(NSString *)field Value:(id)value
{
    [
self deleteDateRecordFromTable:tableName Field:field Value:value result:^(BOOL sucess) {
       
if (sucess)//上一个block执行完成
        {
//            [[NSNotificationCenter defaultCenter] postNotificationName:kChatListChangeNotification object:nil] ;
        }
    }];
}
- (
void)deleteDateRecordFromTable:(NSString *)tableName Field:(NSString *)field Value:(id)value  result:(void(^)(BOOL sucess))done
{
   
__block BOOL is_exculate_success = NO;
   
_tableName = tableName;
   
NSString * query = [NSString stringWithFormat:@"DELETE FROM %@ WHERE %@ = '%@'",tableName,field,value];
   
    [
_db inDatabase:^(FMDatabase *db) {
       
@try {
           
BOOL deletebool = [db executeUpdate:query];
            is_exculate_success =
YES;
           
if (deletebool)
            {
               
NSLog(@"删除成功");
            }
           
else
            {
               
NSLog(@"删除失败");
            }
        }
       
@catch (NSException *exception) {
            [db
rollback];
            is_exculate_success =
NO;
        }
       
@finally {
            [db
commit];
        }
       
    }];
   
if (done)
    {
        done(is_exculate_success);
    }
   
}
#pragma mark - 更新数据

//更新数据,调用当前表
- (
void)updateDataRecordWithField:(NSString *)field Value:(id)value Model:(id)model
{
    [
self updateDataRecordWithTableName:_tableName field:field value:value model:model];
}

/**
 * @
函数名称:updateDataRecordWithTableName:Field:Value:model:
 * @
函数描述:向指定表中更新数据,先根据条件搜索,没有插入,有,更新
 * @
输入参数:field/value :根据条件field = 'value'查找要更新的数据
            tableName:
制定操作的表
            model:
更新内容,默认dictionary类型
 * @
输出参数:nil
 * @
返回值:  BOOL
 *          
成功:YES  失败:NO
 */


//更新特定条件的数据
- (
void)updateDataRecordWithTableName:(NSString *)tableName field:(NSString *)field value:(id)value model:(id)model
{
   
//successblock是否执行完成  exist:如果不存在更新数据,执行插入操作
    [
self updateDataRecordWithTableName:tableName field:field value:value model:model result:^(BOOL sucess, BOOL exist)
    {
       
//block执行完成
       
if (sucess)
        {
           
if (exist)
            {
                [[
NSNotificationCenter defaultCenter] postNotificationName:kChatListChangeNotification object:nil] ;
            }
           
else
            {
                [
self saveUDFmdbContent:model toTable:tableName];
            }
        }
       
    }];
}
- (
void)updateDataRecordWithTableName:(NSString *)tableName field:(NSString *)field value:(id)value model:(id)model result:(void(^)(BOOL sucess, BOOL exist))done
{
   
__block BOOL is_exculate_success = NO;
   
__block BOOL is_exist = NO;
   
if (!model)
    {
       
return;
    }
   
NSString * sql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE %@ = '%@'",tableName,field,value];
    [
_db inDatabase:^(FMDatabase *db) {
       
@try {
           
FMResultSet * rs = [db executeQuery:sql];
           
while ([rs next])
            {
                is_exist =
YES;
               
NSString * query = [NSString stringWithFormat:@"UPDATE %@ SET",tableName];
               
NSMutableString * temp = [NSMutableString stringWithCapacity:0];
               
if ([model allKeys]) {
                   
for (NSString * key in [model allKeys]) {
                        [temp
appendFormat:@" %@ = '%@',",key,[model objectForKey:key]];
                    }
                }
                [temp
appendString:@")"];
                query = [query
stringByAppendingFormat:@"%@ WHERE %@ = '%@'",[temp stringByReplacingOccurrencesOfString:@",)" withString:@""],field,value];
               
BOOL updatebool = [db executeUpdate:query];
                is_exculate_success =
YES;
               
if (updatebool)
                {
                   
NSLog(@"更新成功");
                }
               
break;
            }
            [rs
close];
           
if (!is_exist) {
                is_exculate_success =
YES;
            }
        }
       
@catch (NSException *exception) {
            [db
rollback];
            is_exculate_success =
NO;
        }
       
@finally {
            [db
commit];
        }
    }];
   
if (done)
    {
        done(is_exculate_success,is_exist);
    }
   
}
#pragma mark - 查找数据
- (NSArray *)searchDataSourceWithField:(NSString *)field Value:(id)value
{
   
return [self searchDataSourceWithTableName:_tableName Field:field Value:value Limit:0 OrderKey:nil Ascending:YES];
}

- (
NSArray *)searchDataSourceWithTableName:(NSString *)tableName Field:(NSString *)field Value:(id)value
{
   
return [self searchDataSourceWithTableName:tableName Field:field Value:value Limit:0 OrderKey:nil Ascending:YES];
}

- (
NSArray *)searchDataSourceWithLimit:(NSInteger)limit
{
   
return [self searchDataSourceWithTableName:_tableName Field:nil Value:nil Limit:limit OrderKey:nil Ascending:YES];
}

- (
NSArray *)searchDataSourceWithTableName:(NSString *)tableName Limit:(NSInteger)limit
{
   
return [self searchDataSourceWithTableName:_tableName Field:nil Value:nil Limit:limit OrderKey:nil Ascending:YES];
}

- (
NSArray *)searchDataSourceWithField:(NSString *)field Value:(id)value Limit:(NSInteger)limit  OrderKey:(NSString *)orderKey Ascending:(BOOL)isascend
{
   
return [self searchDataSourceWithTableName:_tableName Field:field Value:value Limit:limit OrderKey:orderKey Ascending:isascend];
}


/**
 * @
函数名称:searchDataSourceWithTableName:Field:Value:Limit:OrderKey:Ascending:
 * @
函数描述:从指定表中查找符合条件的数据
 * @
输入参数:field/value :根据条件field = 'value'查找数据
            tableName:
制定操作的表
             limit
:要返回的数量,不指定默认为0,返回所有
             orderKey/isascend
:指定排序的键值和排序方式,默认升序
 * @
输出参数:nil
 * @
返回值:  NSArray:查找到的符合条件的数组
 */


- (
NSArray *)searchDataSourceWithTableName:(NSString *)tableName Field:(NSString *)field Value:(id)value Limit:(NSInteger)limit OrderKey:(NSString *)orderKey Ascending:(BOOL)isascend
{
   
_tableName = tableName;
   
NSString * query = [NSString stringWithFormat:@"SELECT * FROM %@",tableName];
   
   
if (field && value)
    {
        query = [query
stringByAppendingFormat:@" WHERE %@ = '%@'",field,value];
    }
   
if (orderKey)
    {
       
if (isascend)
        {
            query = [query
stringByAppendingFormat:@" ORDER BY %@",orderKey];
        }
       
else
        {
            query = [query
stringByAppendingFormat:@" ORDER BY %@ DESC",orderKey];
        }
    }
   
   
if (limit > 0)
    {
        query = [query
stringByAppendingFormat:@" limit %zd",limit];
    }
   
NSMutableArray * array = [NSMutableArray arrayWithCapacity:0];
    [
_db inDatabase:^(FMDatabase *db) {
       
FMResultSet * rs = [db executeQuery:query];

       
while ([rs next])
        {
           
if ([self.delegate respondsToSelector:@selector(receiveSearchResult:)]) {
               
id model = [self.delegate receiveSearchResult:rs];
                [array
addObject:model];
            }
        }
        [rs
close];
       
    }];
   
return array;
}

你可能感兴趣的:(OC)