在View Controller之后,iOS块阻止exceptioncallback导致崩溃

我有一个singleton类与一个方法,成功和失败块作为参数,它调用另一个asynchronous执行的方法,也使用成功和失败块。 我的方法的成功块被asynchronous方法的成功块调用。 一切都很好,除非我的视图控制器在成功块返回之前被取消分配,在这种情况下,应用程序崩溃。

这种情况似乎类似于在dealloc方法中将委托设置为零。 我应该如何处理这个块?

这是我的代码看起来像:

- (void)getObjectsWithId:(NSInteger)id success:(void (^)(NSArray *objects))success failure:(void (^)(NSInteger statusCode, NSError *error))failure { NSString *path = [NSString stringWithFormat:@"/my_objects/%d/objects", id]; [self getPath:path parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { NSMutableArray *objects = [[NSMutableArray alloc] initWithCapacity:[responseObject count]]; for (NSDictionary *dict in responseObject) { Object *object = [[Object alloc] initWithDictionary:dict]; [objects addObject:object]; } success(objects); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { failure(operation.response.statusCode, error); }]; } 

您可以使用通知中心来监听视图何时解除分配,并将块设置为零,因此它不会尝试返回任何东西。

在视图出现之前张贴通知:

 [[NSNotificationCenter defaultCenter] postNotificationName:@"myNotificationName" object:broadcasterObject]; 

并注册一个事件:

 [[NSNotificationCenter defaultCenter] addObserver:listenerObject selector:@selector(receivingMethodOnListener:) name:@"myNotificationName" object:nil];