I have a method that does an asynchronous network call, and then passes the success or failure results back via blocks:
- (void) loginWithSuccess:(void (^)(id responseObject))success failure:(void (^)(NSError* error))failure { ... if(error) { failure(error); } else { success(responseObject); } } I noticed that if I call this method and pass in nil as my blocks, my application will crash with EXEC_BAD_ACCESS:
[manager loginWithWithSuccess:nil failure:nil]; But, if I pass in empty blocks, it runs fine:
[manager loginWithWithSuccess:^(id responseObject){} failure:^(NSError *error){}]; I assume this is because at runtime you can't pass parameters to nil? So, when defining my methods that take blocks, should I always check that the blocks are not nil before calling them?
nilBlock, becausedoThatThingYouDoWithCompletion:aBlock error:nilshould be the way you say "I don't want to hear about errors".NULL == nil. Neither can be called as a function.dispatch_block_t crash = NULL; crash();