[client enqueueBatchOfHTTPRequestOperations:@[profileOperation,commFriendsOperation] progressBlock:^(NSUInteger finished, NSUInteger total){} completionBlock:^(NSArray *completedRequests){
//Code here
}]
下面的代码位于 AFNetworking 批处理请求成功块中。仪器表明,正如它所评论的那样,代码爆炸存在内存泄漏。但是,我无法弄清楚它是如何泄漏的以及如何修复它,有人知道吗?
提前致谢。
AFHTTPRequestOperation *commFriendsRequest = [completedRequests objectAtIndex:1];
if(!commFriendsRequest.error&&commFriendsRequest.responseData){
//Leak 46.2%
NSDictionary *commDic = [NSJSONSerialization JSONObjectWithData:commFriendsRequest.responseData options:NSJSONReadingMutableContainers error:NULL];
int status1 = [[commDic objectForKey:@"status"]intValue];
if(status1 == kSuccessCode){
NSArray *commFriendsArray = [commDic objectForKey:@"data"];
if(commFriendsArray&&commFriendsArray.count){
//Leak 30.8%
NSMutableArray *userItems = [NSMutableArray arrayWithCapacity:commFriendsArray.count];
for(NSDictionary *dic in commFriendsArray){
//Leak 23.1%
userItem *item = [[userItem alloc]initFromServer:dic];
[userItems addObject:item];
}
if(profile){//Both Success
profile.commonFriends = userItems;
successBlock(profile);//block call back
}else{
if(status0==kSuccessCode){
successBlock(userItems);//block call back
}else{
NSError *error = [NSError errorWithDomain:kErrorDomain code:status0 userInfo:nil];
failedBlock(error);//block call back
}
}
}else if(status0==kSuccessCode){//CommFriends 0 has profile
successBlock(profile);//block call back
}
}else{
if(status0==kSuccessCode){//CommFriends failed has profile
successBlock(profile);//block call back
}else{//Both failed
NSError *error = [NSError errorWithDomain:kErrorDomain code:status1 userInfo:nil];
failedBlock(error);//block call back
}
}
}else{
failedBlock(commFriendsRequest.error);
}
//[[userItem alloc] initFromServer:dict] code
-(id)initFromServer:(NSDictionary *)dict{
if ((self = [super init])) {
NSString *_userID = [dict objectForKey:@"userID"];
NSString *_userName = [dict objectForKey:@"userName"];
genderType _userGender = [[dict objectForKey:@"userGender"] integerValue];
NSString *_userHeadImageURL = [dict objectForKey:@"userHeadImageURL"];
NSString *_source = [dict objectForKey:@"source"];
int _likeCount = [[dict objectForKey:@"likedCount"] intValue];
int _sameFriends = [[dict objectForKey:@"sameFriends"] intValue];
NSString *_homePage = [dict objectForKey:@"homePage"];
NSString *_createTime = [dict objectForKey:@"createTime"];
if([dict objectForKey:@"dis"])
_distance = [[dict objectForKey:@"dis"]floatValue];
else
_distance = 0;
if ([dict objectForKey:@"uid"]) {
self.uid = [[dict objectForKey:@"uid"] intValue];
}
else{
self.uid = -1;
}
self.userID = _userID;
self.userName = _userName;
self.userGender = _userGender;
self.userHeadImageURL = _userHeadImageURL;
self.source = _source;
self.likeCount = _likeCount;
self.sameFriends = _sameFriends;
if (_homePage && ![_homePage isKindOfClass:[NSNull class]]) {
self.homePage = _homePage;
}
if([dict objectForKey:@"hasJoin"]){
self.hasJoined = [[dict objectForKey:@"hasJoin"]boolValue];
}
if (_createTime && ![_createTime isKindOfClass:[NSNull class]]) {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss z"];
self.createTime = [dateFormatter dateFromString:_createTime];
}
NSString *c = [dict objectForKey:@"comment"];
if (c && ![c isKindOfClass:[NSNull class]]) {
self.comment = c;
}
}
return self;
}