iOS5 NSManagedObjectContext Concurrency types and how are they used?

后端 未结 3 1591
野的像风
野的像风 2021-02-02 00:49

Literature seems a bit sparse at the moment about the new NSManagedObjectContext concurrency types. Aside from the WWDC 2011 vids and some other info I picked up along the way,

3条回答
  •  庸人自扰
    2021-02-02 01:25

    The queue concurrency types help you to manage mutlithreaded core data:

    For both types, the actions only happen on the correct queue when you do them using one of the performBlock methods. i.e.

    [context performBlock:^{
        dataObject.title = @"Title";
        [context save:nil]; // Do actual error handling here
    }];
    

    The private queue concurrency type does all it's work in a background thread. Great for processing or disk io.

    The main queue type just does all it's actions on a UIThread. That's neccesary for when you need to do things like bind a NSFetchedResultsController up to it, or any other ui related tasks that need to be interwoven with processing that context's objects.

    The real fun comes when you combine them. Imagine having a parent context that does all io on a background thread that is a private queue context, and then you do all your ui work against a child context of the main queue type. Thats essentially what UIManagedDocument does. It lets you keep you UI queue free from the busywork that has to be done to manage data.

提交回复
热议问题