NSManagedObjectContext performBlockAndWait: doesn't execute on background thread?

后端 未结 4 780
情话喂你
情话喂你 2020-12-04 05:54

I have an NSManagedObjectContext declared like so:

- (NSManagedObjectContext *) backgroundMOC {
    if (backgroundMOC != nil) {
        return backgroundMOC;         


        
4条回答
  •  既然无缘
    2020-12-04 06:07

    The performBlockAndWait: call only makes sure that you execute the code in such a way that you don't introduce concurrency (i.e. on 2 threads performBlockAndWait: will not run at the same time, they will block each other).

    The long and the short of it is that you can't depend on which thread a MOC operation runs on, well basically ever. I've learned the hard way that if you use GCD or just straight up threads, you always have to create local MOCs for each operation and then merge them to the master MOC.

    There is a great library (MagicalRecord) that makes that process very simple.

提交回复
热议问题