使用AFNetworking请求进行分配

时间:2013-08-21 16:43:23

标签: ios afnetworking instruments allocation cfnetworking

我正在使用AFNetworking发送请求,但是当请求完成其工作时,我正在仪器中获取这些内存分配,并且它们永远保留在那里......如果我尝试启动另一个请求,应用程序会多次收到内存警告,因此它被迫关闭。

Graph    Category   Live Bytes  # Living    # Transitory    Overall Bytes   # Overall   # Allocations (Net / Overall)
1   CFString (store)    4,86 MB 29  3595    30,78 MB    3624    <XRRatioObject: 0x7ffd1c5dc3f0>  %0.00, %0.17
1   Malloc 3,91 MB  3,91 MB 1   1   7,83 MB 2   <XRRatioObject: 0x7ffd1c5dc3f0>  %0.00, %0.00

每个人的内部分配:

第一

#   Address Category    Timestamp   Live    Size    Responsible Library Responsible Caller
0   0x4d28000   CFString (store)    01:12.165.450   •   4923392 CFNetwork   _ZL19_cacheKeyForRequestPK13_CFURLRequest
1   0x36b8000   CFString (store)    00:54.705.461   •   172032  CFNetwork   _ZL19_cacheKeyForRequestPK13_CFURLRequest
2   0x1e3f5a00  CFString (store)    01:12.326.108   •   1024    CFNetwork   _ZL19_cacheKeyForRequestPK13_CFURLRequest

第二

#   Address Category    Timestamp   Live    Size    Responsible Library Responsible Caller
0   0x6903000   Malloc 3,91 MB  01:01.583.198   •   4104192 Foundation  -[NSConcreteMutableData initWithLength:]

修改1

以下是我们用于创建请求的代码:

// Do a json request
+ (void) doJsonRequestWithPath
: (NSString *) path
withMethod: (NSString *) method
withParameters: (id) parameters
withHeaders: (NSDictionary *) headers
onSuccess: (void(^)(NSURLRequest *, NSHTTPURLResponse *, id))success
onFailure: (void(^)(NSURLRequest *, NSHTTPURLResponse *, NSError *, id))failure
{    
    // Register json request operation
    [httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
    // Json parameter encoding
    [httpClient setParameterEncoding:AFJSONParameterEncoding];

    NSMutableURLRequest *request;


    if ([parameters isKindOfClass:[NSDictionary class]]) {
        // Create a request with the http client + path and params
        request = [httpClient requestWithMethod:method path:path parameters:parameters];
    }
    else {
        request = [httpClient requestWithMethod:method path:path parameters:nil];
        [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        //[request setValue:@"100-continue" forHTTPHeaderField:@"Expect"];
        [request setHTTPBody:[parameters dataUsingEncoding:NSUTF8StringEncoding]];
    }

    [request setCachePolicy:NSURLRequestReloadIgnoringCacheData];
    [request setTimeoutInterval:20];

    // Add headers to the request if there is any
    if (headers != nil)
        [self addHeadersToRequest:request fromDictionary:headers];

    // Create a json request operation
    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:success failure:failure];



    // Starts the operation
    [operation start];
}

修改2

我们有一个递归调用

- (void)sendFingerprintAtIndex:(NSInteger) index withGuid:(NSString *) guid
{
    SendActivationRequest *sendActivationRequest = [[SendActivationRequest alloc]
                                                    initWithGuid: guid
                                                    andWithTotalImages:4
                                                    andWithImageIndex:index
                                                    andImageType:2 //digital
                                                    andWithImage:image];

[self.activationDao sendActivationRequest:sendActivationRequest withCompletionBlock:^(NSString *hash, NSArray *errors) {        
        if (hash) {

                .
                .
                .
            }
            else {
                // recursive call
                [self sendFingerprintAtIndex:newIndex withGuid:guid];
            }
        }
        else
        {
            self.completionBlockController(NO, [errors objectAtIndex:0]);
        }
    }];
}

在道中

- (void) sendActivationRequest:(SendActivationRequest *) sendActivationRequest withCompletionBlock:(void (^)(NSString *, NSArray *))completionBlock
{
    // Path
    NSString *path = @"EnviarAtivacao";

    id params = [sendActivationRequest getParams];
    NSDictionary *headers = [sendActivationRequest getHeader];

        // Do a json post request
    [self doJsonPostRequestWithPath:path
                     withParameters:params
                        withHeaders:headers
                          onSuccess:^(NSURLRequest *request, NSHTTPURLResponse *response, id json) {
                              .
                              .
                              .                  

                          } onFailure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *err, id json) {
                              .
                              .
                              .


                          }];

}

谢谢!

2 个答案:

答案 0 :(得分:0)

您在完成块中保留了周期。你不应该在任何街区中引用“自我”。尝试做这样的事情

__weak MyClass *wSelf = self;
MyCompletionBlock completionBlock = ^{
    __strong MyClass *sSelf = wSelf;
   [sSelf doThings];
}

答案 1 :(得分:0)

这些是来自应用的共享NSURLCache实例的分配。

  

如果我尝试启动另一个请求,应用程序会多次收到内存警告,因此它被强制关闭。

发出内存警告时,

NSURLCache应自动丢弃其内存缓存。我不确定你的意思是“它被迫关闭”,但这可能是你在视图控制器中释放内存警告时释放未使用内存的问题。