About thread safety issues with CoreData and SQLite multi-threaded access

Source: Internet
Author: User
Tags sqlite

Http://www.jianshu.com/p/95db3fc4deb3

About thread safety issues with CoreData and SQLite multi-threaded access

Database read operations are typically accessed in multiple threads. When reading the data, we want to ensure that its current state can not be modified, that is, read the lock, otherwise there will be data error confusion.
There are two commonly used data persistence methods in iOS: CoreData and SQLite, both of which need to be thread-safe to explain thread-safe access to SQLite in Fmdb.

One: Fmdb thread safety: (take picture as an example)

1. There is no thread-safe way to perform:

//************** database Save Picture ******************//fmdatabase *database = [Fmdatabase databasewithpath:[self getDatabasePath    ]];    open databases [Database open]; NSString *sql = @ "CreateTableIfNotExistsTest (IdInteger PrimaryKey AutoIncrement,NameText,imageBLOB); "; CREATE TABLE [Database Executeupdate:sql]; Convert the UIImage object to NSData nsdata *data = uiimagepngrepresentation ([UIImage imagenamed:@ "User_browse"]); Write Data sql = @ "insert into Test (name,image) values (?,?) "; [Database executeupdate:sql,@ "Zhang San ", data]; Read Show sql = @ "SELECT * from Test;"; Fmresultset *resultset = [Database Executequery:sql]; while (Resultset.next) {//[resultset dataforcolumn:@ "image"]; NSData *imagedata = [ResultSet dataforcolumnindex:2]; Uiimageview *imageview = [[Uiimageview alloc] Initwithframe:cgrectmake (0, 0,  300)]; Imageview.image = [UIImage imagewithdata:imagedata]; [Self.view Addsubview:imageview]; }

2, using thread queue

//************** Database Thread safety ***********//fmdatabasequeue *queue = [[Fmdatabasequeue alloc] initwithpath:[self GetDatabaseP    ATH]]; [Queue indatabase:^ (Fmdatabase *db) {//thread-safe __block nsstring *sql = @ "CreateTableIfNotExistsTest (IdInteger PrimaryKey AutoIncrement,NameText,imageBLOB); "; CREATE TABLE [Database Executeupdate:sql]; }]; Insert data [queue indatabase:^ (Fmdatabase *db) {//write Data sql = @ "insert into test (name,image) values (?,?) ", data];}]; Read [Queue indatabase:^ (Fmdatabase *db) {//read show sql = @ "SELECT * from test;"; Fmresultset *resultset = [Database Executequery:sql]; while (Resultset.next) {//[resultset dataforcolumn:@ "image"]; NSData *imagedata = [ResultSet dataforcolumnindex:2]; Uiimageview *imageview = [[Uiimageview alloc] Initwithframe:cgrectmake (0, 0, 300, 300)]; Imageview.image = [UIImage imagewithdata:imagedata]; [Self.view Addsubview:imageview]; } }];

Analyze the implementation of Fmdb under thread safety:
When you create a database using Fmdbdatabasequeue, a thread queue is created with GCD:

。。。 _queue = dispatch_queue_create([[NSString stringWithFormat:@"fmdb.%@", self] UTF8String], NULL);        dispatch_queue_set_specific(_queue, kDispatchQueueSpecificKey, (__bridge void *)self, NULL); _openFlags = openFlags;。。。

The method is then called at read Time [queue inDatabase:^(FMDatabase *db) , and the current database is locked in the block

dispatch_sync(_queue, ^() {        FMDatabase *db = [self database];        block(db);    ……}

We can see that this is actually a lock-up of the entire database to ensure thread safety.

Second, CoreData thread safety

1. No thread-safe coredata data reads:

The creation of the Nsmanagedobjectcontext object:_managedObjectContext = [[NSManagedObjectContext alloc] init];

Insert Data operation: (Appdetailmodal as data model)

Context for the returned _managedobjectcontext

AppDetailModal *newapp = [NSEntityDescription insertNewObjectForEntityForName:TableName inManagedObjectContext:context];

Other query, update, delete operations
Get entity

    NSEntityDescription *entity = [NSEntityDescription entityForName:TableName inManagedObjectContext:context];

2. Thread-Safe CoreData operation:

Create a parallel Nsmanagedobjectcontext object first

NSManagedObjectContext* context=[[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];

Then use the two methods when performing a read operation:

    • -(void) Performblock: (void (^) (void)) block

    • -(void) performblockandwait: (void (^) (void)) block

[context performBlock:^{        //要执行的读取操作 }];

Written with Stackedit.

About thread safety issues with CoreData and SQLite multi-threaded access

Related Article

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.