源自:http://stackoverflow.com/questions/25331261/fmdatabasequeue-error-database-is-locked
context.h
@interface context : NSObject
{
FMDatabaseQueue *_queue;
}
+ (context *)sharedInstance;
@property(strong, nonatomic, readwrite) FMDatabaseQueue *queue;
@end
context.m
#import "context.h"
@implementation context
@synthesize queue = _queue;
+ (context *)sharedInstance {
static dispatch_once_t onceToken;
static context *instance = nil;
dispatch_once(&onceToken, ^{
instance = [[context alloc] init];
});
return instance;
}
- (id)init {
self = [super init];
if (self) {
_queue = [FMDatabaseQueue databaseQueueWithPath:YOUR_SQLITE_FILE_PATH];
}
return self;
}
@end
How to use it
context *appContext = [context sharedInstance];
[appContext.queue inDatabase:^(FMDatabase *db) {
FMResultSet *results = [db executeQuery:@"SELECT * FROM something"];
if([results next]) {
NSLog(@"results dump = %@", [results resultDictionary]);
}
[results close];}];