CoreData Multithreading Security

Source: Internet
Author: User

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?
  1. -(Ibaction) Addintodatasource: (ID) Sender {
  2. user* user= (user *) [nsentitydescription insertnewobjectforentityforname:@ "User" Inmanagedobjectcontext: Self.myAppDelegate.managedObjectContext];
  3. [User Setname:_nametext.text];
  4. [User Setage:[nsnumber Numberwithinteger:[_agetext.text IntegerValue]];
  5. [User Setsex:_sextext.text];
  6. //
  7. address* address= (Address *) [nsentitydescription insertnewobjectforentityforname:@ "Address" InManagedObjectContext:self.myAppDelegate.managedObjectContext];
  8. [Address Setidentify:[nsnumber Numberwithinteger:[_identytext.text IntegerValue]];
  9. [Address sethomelocation:@ "Xianning"];
  10. nserror* error;
  11. BOOL Issavesuccess=[_myappdelegate.managedobjectcontext save:&error];
  12. if (!issavesuccess) {
  13. NSLog (@ "error:%@", Error);
  14. }else{
  15. NSLog (@ "Save successful!");
  16. //    }
  17. nsmanagedobjectcontext* Context=[[nsmanagedobjectcontext Alloc] Initwithconcurrencytype: Nsprivatequeueconcurrencytype];
  18. Context.parentcontext=_myappdelegate.managedobjectcontext;
  19. [Context performblock:^{
  20. Background thread
  21. [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (mocdidsavenotification:) Name: Nsmanagedobjectcontextdidsavenotification Object:nil];
  22. user* user= (user *) [nsentitydescription insertnewobjectforentityforname:@ "User" Inmanagedobjectcontext: Self.myAppDelegate.managedObjectContext];
  23. [User Setname:_nametext.text];
  24. [User Setage:[nsnumber Numberwithinteger:[_agetext.text IntegerValue]];
  25. [User Setsex:_sextext.text];
  26. nserror* error;
  27. if (![ Context Save:&error]) {
  28. NSLog (@ "error is%@", error);
  29. }
  30. Main thread
  31. [_myappdelegate.managedobjectcontext performblock:^{
  32. nserror* error;
  33. if (![ _myappdelegate.managedobjectcontext Save:&error]) {
  34. NSLog (@ "error is%@", error);
  35. //            }
  36. //
  37. //        }];
  38. }];
  39. Dispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_default, 0), ^{
  40. //
  41. Dispatch_sync (Dispatch_get_main_queue (), ^{
  42. //
  43. //        });
  44. //    });
  45. }

Notification Center

[Plain]View PlainCopyprint?
  1. -(void) Mocdidsavenotification: (nsnotification *) notification
  2. {
  3. Nsmanagedobjectcontext* Savecontext=[notification Object];
  4. if (_myappdelegate.managedobjectcontext==savecontext) {
  5. Return
  6. }
  7. if (_myappdelegate.managedobjectcontext.persistentstorecoordinator!=savecontext.persistentstorecoordinator) {
  8. Return
  9. }
  10. Dispatch_sync (Dispatch_get_main_queue (), ^{
  11. [_myappdelegate.managedobjectcontext mergechangesfromcontextdidsavenotification:notification];
  12. });
  13. }


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

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.