In my ios app, I use FMDB to connect to the sqlite3 db. In the app, db will be used in multithread.As a result, I create a singleton in this way:
+ (id)instance
{
static DBManager 开发者_如何学Python*dbManager = nil;
if (dbManager == nil) {
dbManager = [[DBManager alloc]init];
}
[dbManager initialDBmanager];
return dbManager;
}
however Error: FMDataBase is currently in use , sometimes occures. then I update the instance:
+ (id)instance
{
static DBManager *dbManager = nil;
if (dbManager == nil) {
dbManager = [[DBManager alloc]init];
[dbManager initialDBmanager];
}
while([dbManager.db inUse])//In my opinion this promises the db is free
{
}
return dbManager;
}
The Error still exists. So My question is how to correctly create a singleton and why the code can't avoid the error. thanks!
I use @synchronized, and it temporarily fixs the problem.
I will keep eyes on it~
精彩评论