A __weak
reference is set to nil
if the object it points to is deallocated.
So if your Request
object has already been deallocated when the completion block is
called, weakSelf
is nil
. In that case weakSelf.successBlock
evaluates to a NULL pointer, and that is causing the crash.
The following pattern avoids this problem:
[_operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
Request *strongSelf = weakSelf;
if (strongSelf) {
strongSelf.successBlock(operation.response, responseObject);
}
} ...
strongSelf
will be nil
if the Request
object has already been deallocated.
Otherwise the strong reference ensures that the object is not deallocated
while the block is executing.
On the other hand, if you want the Request
object to exist until the completion block
is called, then you should not use a weak reference.