简体   繁体   中英

FMDB with multithread problem

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 *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~

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM