I have a general background handler that I created that solves this problem. Only the first method is publicly available, so it can be used everywhere. Please note that all parameters are required.
+(void) Run: (SEL)sel inBackground: (id)target withState: (id)state withCompletion: (void(^)(id state))completionHandler // public { [(id)self performSelectorInBackground: @selector(RunSelector:) withObject: @[target, NSStringFromSelector(sel), state, completionHandler]]; } +(void) RunSelector: (NSArray*)args { id target = [args objectAtIndex: 0]; SEL sel = NSSelectorFromString([args objectAtIndex: 1]); id state = [args objectAtIndex: 2]; void (^completionHandler)(id state) = [args objectAtIndex: 3]; [target performSelector: sel withObject: state]; [(id)self performSelectorOnMainThread: @selector(RunCompletion:) withObject: @[completionHandler, state] waitUntilDone: true]; } +(void) RunCompletion: (NSArray*)args { void (^completionHandler)(id state) = [args objectAtIndex: 0]; id state = [args objectAtIndex: 1]; completionHandler(state); }
Here is an example of how he called:
NSMutableDictionary* dic = [[NSMutableDictionary alloc] init]; __block BOOL done = false; [Utility Run: @selector(RunSomething:) inBackground: self withState: dic withCompletion:^(id state) { done = true; }];
source share