This question is similar to ios NSError types but the solution described there didn't work and I believe it isn't quite what I need.
I have a method that takes performs an asynchronous call and then invokes a completion block. When I try to pass the NSError ** to the completion block, I get this error:
Sending 'NSError *const __strong *' to parameter of type 'NSError *__autoreleasing *' changes retain/release properties of pointer
The code is as follows:
+(void) agentWithGUID:(NSString *) guid completion:(void (^)(AKAgentProfile * agentProfile, NSError ** error)) completionBlock
{
dispatch_queue_t requestQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(requestQueue, ^{
NSString * parameterizedUrl = [AKAgentProfileEndPoint stringByAppendingString:guid];
NSURL *url = [NSURL URLWithString:parameterizedUrl];
NSData *data = [NSData dataWithContentsOfURL:url];
NSError * error = nil;
AKAgentProfile * agentProfile = [[[AKAgentFactory alloc] init] agentProfileWithData:data error:&error];
dispatch_async(dispatch_get_main_queue(), ^{
completionBlock(agentProfile,&error);
});
});
}
Your completion block arguments are total nonsense.
You have a variable NSError* err on the call stack.
You then try to pass the address of err to a completion block that will be called in the main thread. By the time that completion block is called, your function has long returned, and &err is rubbish. If the completion block tried to store anything there, it would store an NSError* where some long time ago your err variable was on the stack, most likely overwriting some valuable data of a completely unrelated method.
This just doesn't work with callback blocks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With