Afnetworking如何释放下载的数据内存?

时间:2015-03-23 11:27:46

标签: ios memory-management afnetworking-2

我正在下载带有帖子请求的40 MB文件。在下载请求期间,它也分配40 MB。但在那之后,下载编译后的内存不会自行解除分配。这是我的示例代码,如何释放内存?

+(AFHTTPRequestOperation*)PostDownloadRequest:(NSDictionary*)postDic saveName:(Product*)product Url:(NSString*)Url success:(void (^)(id responseObject)) success failure:(void (^)(AFHTTPRequestOperation *operation)) failure
{
    if(Downloadings==nil)
    {
        Downloadings=[[NSMutableArray alloc]init];
    }
    __block AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager.requestSerializer setValue:@"Application/Json" forHTTPHeaderField:@"Content-Type"];
    [manager.requestSerializer setCachePolicy: NSURLRequestReloadIgnoringLocalCacheData];
    if([IdentityUser UserId]!=nil)
        [manager.requestSerializer setValue:[IdentityUser Token] forHTTPHeaderField:@"Authorization"];
    if (product.productFormatType == 1) {
        [manager.requestSerializer setValue:@"Application/pdf" forHTTPHeaderField:@"Accept"];
    }
    else{
        [manager.requestSerializer setValue:@"Application/zip" forHTTPHeaderField:@"Accept"];
    }
    [Downloadings addObject:product.iosInAppId];

    __block AFHTTPRequestOperation *requestOperation =[manager POST:Url
                                                         parameters:postDic
                                                            success:^(AFHTTPRequestOperation *operation, id responseObject) {
                                                                dispatch_queue_t taskQ = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
                                                                 dispatch_async(taskQ, ^{
                                                                    FileProcess *pr=[[FileProcess alloc]init];
                                                                    NSLog(@"%d", (int)product.productFormatType);
                                                                    if (product.productFormatType == 1) {
                                                                        [pr SavePdfToFile:product data:responseObject];
                                                                    }
                                                                    else{
                                                                        [pr SaveEpubToFile:product data:responseObject];
                                                                    }
                                                                });
                                                                  success(nil);
                                                                responseObject=nil;
                                                                requestOperation=nil;
                                                            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                                                failure(operation);
                                                            }];

    return requestOperation;
}

1 个答案:

答案 0 :(得分:0)

将您的代码放在

@autoreleasepool {
    ...do your work here...
  }

试试这个:

@autoreleasepool 
{
    if (product.productFormatType == 1) 
         [pr SavePdfToFile:product data:responseObject];
    else
         [pr SaveEpubToFile:product data:responseObject];
}