我继承了一个使用 ASIHttpRequest 进行所有网络通信的项目。我不清楚我们使用的是哪个特定版本。我只能说,从 .h 文件中,特定文件的最早创建日期是 1010 年 8 月 17 日(ASIDataDecompressor)。
我们正在使用完成和失败块。出于某种原因,通常会触发故障块,这只有在服务器无法响应时才会真正发生。我们的日志看起来很正常,并且我们没有收到任何通知(Airbrake)在错误发生时服务器出现问题,所以现在我假设我们的服务器很好并且它是应用程序罪魁祸首。
我决定通过 Instruments (Leaks) 运行该应用程序,并惊讶地发现当我强制请求失败时,会立即创建约 27 个泄漏。我不知道如何很好地绕过 Instruments,所以现在我不确定如何处理这些信息。
我想我会发布我的代码,看看是否有任何明显的地方。
在viewDidLoad中,执行了这段代码
[[MyAPI sharedAPI] getAllHighlights:pageNumber:perPage onSuccess:^(NSString *receivedString,NSString *responseCode) {
[self getResults:receivedString];
if(![responseCode isEqualToString:@"Success"]) {
[self hideProgressView];
appDelegate.isDiscover_RefreshTime=YES;
[[MyAPI sharedAPI] showAlert:responseCode];
} else {
NSString *strLogEvent=@"Discover_Highlights_Loaded Page_";
strLogEvent=[strLogEvent stringByAppendingFormat:@"%i",intPageNumber];
[FlurryAnalytics logEvent:strLogEvent timed:YES];
}
} onFail:^(ASIFormDataRequest *request) {
NSDictionary *parameters = [[MyAPI sharedAPI] prepareFailedRequestData:request file:@"Discover" method:_cmd];
[FlurryAnalytics logEvent:@"Unable_to_Connect_to_Server" withParameters:parameters timed:true];
[self hideProgressView];
appDelegate.isDiscover_RefreshTime=YES;
[[AfarAPI sharedAPI] showAlert:@"Unable to Connect to Server."];
[tblHighlightsGrid reloadData];
[tblListHighlights reloadData];
}];
这些 typedef 已在 API Singleton 的顶部定义:
typedef void (^ASIBasicBlockWrapper)(NSString *responseString,NSString *responseCode);
typedef void (^ASIBasicBlockWrapperFail)(ASIFormDataRequest *request);
MyAPISingleton#getAllHighlights...
- (void)getAllHighlights:(NSString *)pageNumber:(NSString *)perPage onSuccess:(ASIBasicBlockWrapper)cb1 onFail:(ASIBasicBlockWrapperFail)cb2{
NSString *access_token= [[NSUserDefaults standardUserDefaults] objectForKey:@"access_token"];
NSString *url = [baseURL stringByAppendingFormat:AFAR_GET_ALL_HIGHLIGHTS_ENDPOINT, pageNumber,perPage];
if (access_token) { url = [url stringByAppendingFormat:ACCESS_TOKEN, access_token]; }
__block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:url]];
[request setRequestMethod:@"GET"];
[request setDelegate:self];
[self executeAsynchronousRequest:request onSuccess:cb1 onFail:cb2];
}
最后,MyAPI#executeAsynchronousRequest:
- (void) executeAsynchronousRequest:(ASIFormDataRequest *)request onSuccess:(ASIBasicBlockWrapper)cb1 onFail:(ASIBasicBlockWrapperFail)cb2
{
[request setCompletionBlock:^{
int statusCode = [request responseStatusCode];
NSString *statusMessage = [self statusErrorMessage:statusCode];
cb1([request responseString],statusMessage);
}];
[request setFailedBlock:^{
cb2(request);
}];
[request startAsynchronous];
}
关于为什么会产生 27 个泄漏,有什么突出的吗?