我不确定我是否在这里造成了泄漏,是否可以通过执行选择器将分配的 NSError 返回给调用方法?可以创建 NSMutableArray 并将其存储在我为回调获得的同一个对象中吗?然后将其传递给代表?代码运行良好,但因为我是新来的 arc,所以我害怕做错事。
(我正在使用执行选择器,因为我的选择器是动态的。仅用于我静态编写的示例)。
AFHTTPRequestOperation *operation = [self.client HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation *operation, id responseObject) {
//-----------------Callback--------------------
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
SEL callback = @selector(getOperationCallback:);
NSError *error = [self performSelector:callback withObject:operation];
//------------------Delegate Call---------------
if(operation.delegate)
[operation.delegate onFinish:operation.requestIdentifier error:error
data:operation.parsedObject];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//------------------Delegate Call---------------
if(operation.delegate)
[operation.delegate onFinish:operation.requestIdentifier error:error data:nil];
}];
- (NSError *)getOperationCallback:(AFHTTPRequestOperation *)operation{
NSArray *rawJson = (NSArray *)operation.jsonObject;
NSError *error;
NSMutableArray *array = [[NSMutableArray alloc] init];
for(id json in rawJson){
MyObject *object = [[MyObject alloc] initWithJson:json];
if(object){
[array addObject:object];
}else{
error = [NSError errorWithDomain:@"myErrors" code:1000 userInfo:nil];
break;
}
}
operation.parsedObject = array;
return error;
}