【发布时间】:2017-05-11 15:27:01
【问题描述】:
TL;DR:克隆并自己检查泄漏https://github.com/JakubMazur/SO41343532/
我有一堂课可以处理我所有的网络。它叫做ResponseOrganizer,里面有一个类方法:
+ (void)getSth:(void (^)(NSURLSessionDataTask *operation, NSArray *locales, id plainObject))success failure:(void (^)(NSURLSessionDataTask *operation, NSError *error))failure {
Connection *connection = [Connection new];
connection.urlString = @"http://sample-file.bazadanni.com/download/txt/json/sample.json";
connection.requestMethodType = GET;
[connection fireWithSuccess:^(NSURLSessionDataTask *operation, NSArray *returnArray, id originalResponse) {
success(operation, returnArray, originalResponse);
} failure:^(NSURLSessionDataTask *operation, NSError *error) {
failure(operation, error);
}];
}
Connection 是一个我的内部连接对象:
这里是实现:
#import "Connection.h"
@interface Connection()
@property (weak,nonatomic) AFHTTPSessionManager *manager;
@end
@implementation Connection
#pragma mark - Connection groundwork
-(void)fireWithSuccess:(void (^)(NSURLSessionDataTask *operation, NSArray* returnArray, id originalResponse))success failure:(void (^)(NSURLSessionDataTask *operation, NSError *error))failure {
self.manager = [AFHTTPSessionManager manager];
[self.manager urlString:self.urlString withMethod:self.requestMethodType parameters:self.paramaters success:^(NSURLSessionDataTask *operation, id responseObject) {
success(operation,@[responseObject],nil);
} failure:^(NSURLSessionDataTask *operation, NSError *error) {
failure(operation,error);
}];
}
@end
我在AFNetworking 中有一个调用正确方法的类别。为了简化它看起来像这样:
-(void)urlString:(NSString*)urlString withMethod:(RequestMethodType)method parameters:(NSDictionary*)parameters success:(void (^)(NSURLSessionDataTask *operation, id responseObject))success failure:(void (^)(NSURLSessionDataTask *operation, NSError *error))failure {
switch (method) {
case GET: {
[self getWithURLString:urlString parameters:parameters success:^(NSURLSessionDataTask *operation, id responseObject) {
success(operation,responseObject);
} failure:^(NSURLSessionDataTask *operation, NSError *error) {
failure(operation,error);
}];
break;
}
}
当我想在我的 ViewController 中发出请求时,我会这样:
[ResponseOrginizer getSth:^(NSURLSessionDataTask *operation, NSArray *locales, id plainObject) {
} failure:^(NSURLSessionDataTask *operation, NSError *error) {
}];
当我在仪器中运行它时,我总是得到:
在这里并不重要,它会落在成功/失败块上,它总是会导致泄漏。我从中提取所有内容并尽可能简单地将其放在 github 上。 Github 链接: https://github.com/JakubMazur/SO41343532/
【问题讨论】:
标签: ios objective-c memory-leaks afnetworking nsurlsession