我正在使用FMDatabaseQueue并将其设置为单身
static FMDatabaseQueue* sharedDatabase;
+ (FMDatabaseQueue *)sharedDatabase
{
if (sharedDatabase == nil) sharedDatabase = [FMDatabaseQueue databaseQueueWithPath:DATABASE_PATH];
return sharedDatabase;
}
当我像这样调用inDatabase方法时,它会导致死锁......
[[UserData sharedDatabase] inDatabase:^(FMDatabase *db) {
FMResultSet * rs = [db executeQuery:@"SELECT id FROM table1"];
if([rs next]) {
[[NSNotificationCenter defaultCenter] postNotificationName:kNotification1 object:aObject userInfo:@{@"key":@1}];
}else{
[[NSNotificationCenter defaultCenter] postNotificationName:kNotification1 object:aObject userInfo:@{@"key":@0}];
}
[rs close];
}];
另一个控制器观察通知
- (void)update:(NSNotification *)notification
{
NSNumber *key = [notification.userInfo objectForKey:@"key"];
if ([key isEqual:@1]){
[[UserData sharedDatabase] inDatabase:^(FMDatabase *db) {
[db executeUpdate:@"UPDATE table1 SET number = ?",1];
}];
}
}
任何人都有想法解决这个问题......?
答案 0 :(得分:1)
执行查询后发送通知怎么样?
__block BOOL result;
[[UserData sharedDatabase] inDatabase:^(FMDatabase *db) {
FMResultSet * rs = [db executeQuery:@"SELECT id FROM table1"];
result = ([rs next]) ? YES : NO;
[rs close];
}];
[[NSNotificationCenter defaultCenter] postNotificationName:kNotification1 object:aObject userInfo:@{@"key":@result}];
答案 1 :(得分:0)
我不知道你究竟该做什么,但是你应该知道dataBase只能同时访问。因为FMResultSet是一个Singleton,它只能访问数据库。
所以我认为你改变了这样:
[[UserData sharedDatabase] inDatabase:^(FMDatabase *db)
{
int resultCount = 0
FMResultSet * rs = [db executeQuery:@"SELECT id FROM table1"];
if([rs next])
{
resultCount++;
}
[rs close];
[[NSNotificationCenter defaultCenter] postNotificationName:kNotification1 object:aObject userInfo:@{@"key":resultCount}];
}];
-(void)update:(NSNotification *)notification
{
NSNumber *key = [notification.userInfo objectForKey:@"key"];
if ([key isEqual:@1])
{
[[UserData sharedDatabase] inDatabase:^(FMDatabase *db)
{
[db executeUpdate:@"UPDATE table1 SET number = ?",1];// ?i think the sql is some thing wrong
}];
}
}