基于FMDatabase的一层很薄的封装,主要目的是消除反复open close数据库的冗余代码,并以单例的形式暴露
#import "YLSDatabaseHelper.h" #import "YLSGlobalUtils.h" @implementation YLSDatabaseHelper { FMDatabase* db; } -(id) init { self = [super init]; if(self){ NSString *dbFilePath = [YLSGlobalUtils getDatabaseFilePath]; db = [[FMDatabase alloc] initWithPath:dbFilePath]; } return self; } +(YLSDatabaseHelper*) sharedInstance { static dispatch_once_t pred = 0; __strong static id _sharedObject = nil; dispatch_once(&pred,^{ _sharedObject = [[self alloc] init]; }); return _sharedObject; } -(void) doOperation:(void(^)(FMDatabase*))block { [db open]; block(db); [db close]; } @end