Invoke model method with block that will run on the main thread

前端 未结 4 2042
野性不改
野性不改 2020-12-13 16:36

one of the central tenets of the architecture of my latest app is that I\'m going to call methods on the app\'s model which will be async and accept failure and success scen

4条回答
  •  心在旅途
    2020-12-13 17:20

    NSObject has a method:

    - (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait
    

    Create a method that takes a NSDictionary parameter in a convenient class that will always be around (like your app delegate, or a singleton object), package up the block and its parameters into a NSDictionary or NSArray, and call

    [target performSelectorOnMainThread:@selector(doItSelector) withObject:blockAndParameters waitUntilDone:waitOrNot];
    

提交回复
热议问题