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