Creating Background Thread for Core Data writing

前端 未结 2 1508
忘掉有多难
忘掉有多难 2021-01-03 07:57

I\'m trying to recreate the three tier core data system that is described in this cocoanetics article ( http://www.cocoanetics.com/2012/07/multi-context-coredata/). The prob

2条回答
  •  难免孤独
    2021-01-03 09:00

    Managing threads is very basic in iOS

    To have something run on background, you do like this:

    - (void)someMethod {
        // method is called on main thread normally
    
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            .... // here things are run in background
        });
    }
    

    To go back to main thread anywhere, do this:

    - (void)someOtherMethod {
        // method is called on background thread
    
        dispatch_async(dispatch_get_main_queue(), ^{
            ... // here things are on main thread again
        });
    }
    

提交回复
热议问题