Problem One: The scenario where "is currently on use" appears is this, multithreaded operations database, each thread uses the Fmdatabase instance (note that Fmdatabasequeue is not used).
Question two: "Database is locked" the scene is such, multi-threaded operation of the databases, each thread each created the Fmdatabasequeue instance operation database, or a thread to create fmdatabasequeue instance to operate, The other thread creates a fmdatabase instance to manipulate.
Workaround: Fmdb Multi-threaded operations database, must use Fmdatabasequeue, must create only one instance, that is, multiple threads operate the database is the same Fmdatabasequeue instance.
Cause: Because the first fmdatabase is not thread-safe, if the two threads operate the database at the same time, will be "is currently on use"; Fmdatabasequeue is actually a dispatch queue (g-c-d), the operation of the database must be sequential execution, The operation of the two database cannot be executed concurrently, if two threads each create an instance of Fmdatabasequeue, the same database operation is triggered simultaneously when the thread executes simultaneously, causing "database is locked", So must be a fmdatabasequeue instance, under multiple threads operating simultaneously, in fact, in the same queue in a single operation, no simultaneous operation.
PS: Here's how I fix it.
1 #pragmaMark-Configure Database single Chat2+ (Fmdatabasequeue *) Getshareddatabasequeue3 {4 StaticFmdatabasequeue *my_fmdatabasequeue=Nil;5 6 if(!my_fmdatabasequeue) {7NSString *path = [kdocuments stringbyappendingpathcomponent:@"Db_cmbcc.sqlite"];8My_fmdatabasequeue =[Fmdatabasequeue Databasequeuewithpath:path];9 }Ten returnMy_fmdatabasequeue; One}
-(void) Savesinglechatmessage: (Messageentitymodel *) messageentity{ fmdatabasequeue *queue = [Storemanagerhelper Getshareddatabasequeue]; [Queue indatabase:^ (Fmdatabase *db) { //Open Database if ([db Open]) { //Database Build table, insert statement } else { NSLog (@ "failed to open database! "); } }];}
Fmdb Multi-threaded "is currently on use" or "database is locked" issue