Fmdb Multi-threaded "is currently on use" or "database is locked" issue

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.