CoreData Package Series one----Nsmanagedobjectcontext multithreading

Source: Internet
Author: User

The CoreData principle is to read the entity Class model file into memory and then create the corresponding database table based on the model file. Let the entity class and database table map, type Java inside the Hibernate ORM framework. Let's discuss the next nsmanagedobjectcontext.

When creating Nsmanagedobjectcontext, you can specify three modes:

//or without parameters, this is the default Nsconfinementconcurrencytype // bind to a background thread Nsprivatequeueconcurrencytype // bind to a main thread Nsmainqueueconcurrencytype

In other words, the context can be used in multi-threaded situations. If you use the context only on a single thread to save data, you will be slow to handle large amounts of data.

Therefore, it is best to use the context on different sites.

//you can encapsulate the following code into a nsmanagedobjectcontext category .StaticNsmanagedobjectcontext *_foregroudcontext;StaticNsmanagedobjectcontext *_backgroudcontext;+ (Nsmanagedobjectcontext *) Foregroudcontext {Staticdispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{        //1. Create a single-instance main thread context_foregoundcontext =[[Nsmanagedobjectcontext alloc] initwithconcurrencytype:nsmainqueueconcurrencytype]; //2. Default setting a version of the automatic migration of storage        IDPersistentstorecoordinator = [Nspersistentstorecoordinator coordinatoruseautomigration];//using a category-encapsulated[_foregoundcontext Setpersistentstorecoordinator:persistentstorecoordinator];    }; return_foregroudcontext;}+ (Nsmanagedobjectcontext *) Backgroudcontext {Staticdispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{        //1. Create a new background thread context        IDContext =[[Nsmanagedobjectcontext alloc] initwithconcurrencytype:nsconfinementconcurrencytype]; //2. Set as background thread context[self setbackgroudcontext:context]; //3. Set the _foregroudcontext as the parantcontext of the child thread contextContext.parantcontext =[self foregroundcontext];    }; return_backgroudcontext;}//can create a new background thread context as _backgroudcontext+ (void) Setbackgroudcontext: (Nsmanagedobjectcontext *) Context {if(Context = =_backgoundcontext)return; _backgoundcontext=Nil; _backgoundcontext=context;}//get the Unified portal for the context+(instancetype) managedobjectcontext {if([Nsthread Ismainthread]) {return[self foregroundcontext]; } Else {        return[self backgroundcontext]; }}

Summarize:


1. Nsmanagedobjectcontext can set other Nsmanagedobjectcontext as parent context


2. Child context can access all objects under the parent level


3. And when the content of the child nsmanagedobjectcontext changes, if the Save method is executed, it will automatically merge into the parent Nsmanagedobjectcontext


4. This time the parent must also save again, if the parent has no parent, then it will be written directly to the Nspersistentstorecoordinator, if there is a further layer of the parent bubble "prevent unnecessary calls"

CoreData Package Series one----Nsmanagedobjectcontext multithreading

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.