Open TouchFriend opened 3 years ago
当viewController死亡后,CTAPIBaseManager的dealloc并没有如期调用,需要等到请求完毕时,才会调用。 下面代码的闭包对CTAPIBaseManager对象进行了强引用,所以没有如期死亡。这也跟旧框架RTNetworking代码不同,现在是这样设计的吗?
CTNetworking代码: NSNumber requestId = [[CTApiProxy sharedInstance] callApiWithRequest:request success:^(CTURLResponse response) { [self successedOnCallingAPI:response]; } fail:^(CTURLResponse *response) { CTAPIManagerErrorType errorType = CTAPIManagerErrorTypeDefault; if (response.status == CTURLResponseStatusErrorCancel) { errorType = CTAPIManagerErrorTypeCanceled; } if (response.status == CTURLResponseStatusErrorTimeout) { errorType = CTAPIManagerErrorTypeTimeout; } if (response.status == CTURLResponseStatusErrorNoNetwork) { errorType = CTAPIManagerErrorTypeNoNetWork; } [self failedOnCallingAPI:response withErrorType:errorType]; }];
RTNetworking代码:
{ \ weak typeof(self) weakSelf = self; \ REQUEST_ID = [[CTApiProxy sharedInstance] call##REQUEST_METHOD##WithParams:apiParams serviceIdentifier:self.child.serviceType methodName:self.child.methodName success:^(CTURLResponse *response) { \ strong typeof(weakSelf) strongSelf = weakSelf; \ [strongSelf successedOnCallingAPI:response]; \ } fail:^(CTURLResponse *response) { \ __strong typeof(weakSelf) strongSelf = weakSelf; \ [strongSelf failedOnCallingAPI:response withErrorType:CTAPIManagerErrorTypeDefault]; \ }]; \ [self.requestIdList addObject:@(REQUEST_ID)]; \ }
当viewController死亡后,CTAPIBaseManager的dealloc并没有如期调用,需要等到请求完毕时,才会调用。 下面代码的闭包对CTAPIBaseManager对象进行了强引用,所以没有如期死亡。这也跟旧框架RTNetworking代码不同,现在是这样设计的吗?
CTNetworking代码: NSNumber requestId = [[CTApiProxy sharedInstance] callApiWithRequest:request success:^(CTURLResponse response) { [self successedOnCallingAPI:response]; } fail:^(CTURLResponse *response) { CTAPIManagerErrorType errorType = CTAPIManagerErrorTypeDefault; if (response.status == CTURLResponseStatusErrorCancel) { errorType = CTAPIManagerErrorTypeCanceled; } if (response.status == CTURLResponseStatusErrorTimeout) { errorType = CTAPIManagerErrorTypeTimeout; } if (response.status == CTURLResponseStatusErrorNoNetwork) { errorType = CTAPIManagerErrorTypeNoNetWork; } [self failedOnCallingAPI:response withErrorType:errorType]; }];
RTNetworking代码:
define AXCallAPI(REQUEST_METHOD, REQUEST_ID) \
{ \ weak typeof(self) weakSelf = self; \ REQUEST_ID = [[CTApiProxy sharedInstance] call##REQUEST_METHOD##WithParams:apiParams serviceIdentifier:self.child.serviceType methodName:self.child.methodName success:^(CTURLResponse *response) { \ strong typeof(weakSelf) strongSelf = weakSelf; \ [strongSelf successedOnCallingAPI:response]; \ } fail:^(CTURLResponse *response) { \ __strong typeof(weakSelf) strongSelf = weakSelf; \ [strongSelf failedOnCallingAPI:response withErrorType:CTAPIManagerErrorTypeDefault]; \ }]; \ [self.requestIdList addObject:@(REQUEST_ID)]; \ }