The Nsmanagedobjectcontext in CoreData is insecure in multi-threading, and if you want to have multi-threaded access to CoreData, the best approach is to have a thread a nsmanagedobjectcontext,
, each Nsmanagedobjectcontext object instance can use the same Nspersistentstorecoordinator instance, which provides secure sequential access to persistent storage. This is because Nsmanagedobjectcontext will be locked before using Nspersistentstorecoordinator.
ios5.0 provides the Initwithconcurrentcytype method for Nsmanagedobjectcontext, where one of the nsprivatequeueconcurrencytype automatically creates a new thread to store Nsmanagedobje Ctcontext and it will also automatically create nspersistentstorecoordinator,appdelegate as in the previous chapter, ios5.0 can be implemented with GCD before
[Plain]View PlainCopyprint?
- -(Ibaction) Addintodatasource: (ID) Sender {
- user* user= (user *) [nsentitydescription insertnewobjectforentityforname:@ "User" Inmanagedobjectcontext: Self.myAppDelegate.managedObjectContext];
- [User Setname:_nametext.text];
- [User Setage:[nsnumber Numberwithinteger:[_agetext.text IntegerValue]];
- [User Setsex:_sextext.text];
- //
- address* address= (Address *) [nsentitydescription insertnewobjectforentityforname:@ "Address" InManagedObjectContext:self.myAppDelegate.managedObjectContext];
- [Address Setidentify:[nsnumber Numberwithinteger:[_identytext.text IntegerValue]];
- [Address sethomelocation:@ "Xianning"];
- nserror* error;
- BOOL Issavesuccess=[_myappdelegate.managedobjectcontext save:&error];
- if (!issavesuccess) {
- NSLog (@ "error:%@", Error);
- }else{
- NSLog (@ "Save successful!");
- // }
- nsmanagedobjectcontext* Context=[[nsmanagedobjectcontext Alloc] Initwithconcurrencytype: Nsprivatequeueconcurrencytype];
- Context.parentcontext=_myappdelegate.managedobjectcontext;
- [Context performblock:^{
- Background thread
- [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (mocdidsavenotification:) Name: Nsmanagedobjectcontextdidsavenotification Object:nil];
- user* user= (user *) [nsentitydescription insertnewobjectforentityforname:@ "User" Inmanagedobjectcontext: Self.myAppDelegate.managedObjectContext];
- [User Setname:_nametext.text];
- [User Setage:[nsnumber Numberwithinteger:[_agetext.text IntegerValue]];
- [User Setsex:_sextext.text];
- nserror* error;
- if (![ Context Save:&error]) {
- NSLog (@ "error is%@", error);
- }
- Main thread
- [_myappdelegate.managedobjectcontext performblock:^{
- nserror* error;
- if (![ _myappdelegate.managedobjectcontext Save:&error]) {
- NSLog (@ "error is%@", error);
- // }
- //
- // }];
- }];
- Dispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_default, 0), ^{
- //
- Dispatch_sync (Dispatch_get_main_queue (), ^{
- //
- // });
- // });
- }
Notification Center
[Plain]View PlainCopyprint?
- -(void) Mocdidsavenotification: (nsnotification *) notification
- {
- Nsmanagedobjectcontext* Savecontext=[notification Object];
- if (_myappdelegate.managedobjectcontext==savecontext) {
- Return
- }
- if (_myappdelegate.managedobjectcontext.persistentstorecoordinator!=savecontext.persistentstorecoordinator) {
- Return
- }
- Dispatch_sync (Dispatch_get_main_queue (), ^{
- [_myappdelegate.managedobjectcontext mergechangesfromcontextdidsavenotification:notification];
- });
- }
In fact, you can not notice that the following content is not allowed to comment, but also note the Notification Center on the line
Ext.: http://blog.csdn.net/chen505358119/article/details/9344389
CoreData Multithreading Security