FMDB threading issues when working with large data

Source: Internet
Author: User
Tags dota

The first rule to insert a database: The next action after the last operation is executed if the data in the database operation is relatively large, the main thread will be stuck (running particularly slow) solution:to add a child thread to a database Dispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_default, 0), ^{
for (int i = 0; i <; i++) {
[[Fmdbmanager sharedintance] inserttablenamewithtablename:@ "Dota" name:@ "Lasy" gender:@ "F" age:@ "a" Image: Uiimagepngrepresentation ([UIImage imagenamed:@ "2.jpg"]) myid:@ "5"];               }            });            However, when a child thread is added, it can cause the program to Crash the solution if it is asynchronous: The block queue operation code is as follows:#注意: If Nsoperationqueue is local, each execution will have a different block queue in the execution of the Nsoperationqueue object that is added as a propertyself. Queue = [[nsoperationqueue alloc] init];  nsblockoperation *block1 = [nsblockoperation blockoperationwithblock:^{ for (int 0 < 1000 [[fmdbmanager sharedintance] Inserttablenamewithtablename:@ "Dota"Name:@ "Lasy"Gender:@ "Woman"Age:@ "12"Image:Uiimagepngrepresentationuiimage Imagenamed:@ "2.jpg" ]) myid@ "2" ];&NBSP;         }   }];nsblockoperation *block2 = [nsblockoperation blockoperationwithblock: ^{ for (int 0 < 1000             [[FmdbmanagerSharedintance]Inserttablenamewithtablename:@ "Dota"Name:@ "Sony" Gender:@ "Male"Age:@ "22"Image:Uiimagepngrepresentation([UIImageImagenamed: @ "3.jpg" ]) myid@ "1" ";         }    }];    [selfqueue Addoperation[self. Queue addoperation: Block2];  It feels like this can really solve the problem of multiple sub-threads in asynchronous operation, but in fact it will still be Crash at run time .even with FMDB third-partyThe Nsoperation object that adds the property is initialized in Viewdidload and sets his maximum number of concurrent executions code as follows:[self. Queue Setmaxconcurrentoperationcount:1];  so the problem is solved.

FMDB threading issues when working with large data

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.