AFNetworking Memory

时间:2013-10-10 11:01:56

标签: afnetworking

使用AFNetworking从服务器下载文件。这是代码:

self.networkQueue = [[[NSOperationQueue alloc] init] autorelease];
[networkQueue setMaxConcurrentOperationCount:3];

for(NSDictionary* fileDictionary in self.syncArray) {
    @autoreleasepool {

        if([[fileDictionary allKeys] containsObject:@"downloadZipURL"]) {
            NSString* downloadPath = [fileDictionary objectForKey:@"downloadZipURL"];
            downloadPath = [downloadPath stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
            NSURLRequest *requestURL = [NSURLRequest requestWithURL:[NSURL URLWithString:downloadPath]];

            NSString* localDestPath = [NSString stringWithFormat:@"%@/%@", [FileUtil userDocumentsDirectory], [downloadPath lastPathComponent]];
            NSString* localTempPath = [NSString stringWithFormat:@"%@.tmp", localDestPath];
            [(NSMutableDictionary*)fileDictionary setObject:localDestPath forKey:@"downloadDestination"];

            AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:requestURL];
            operation.outputStream = [NSOutputStream outputStreamToFileAtPath:localDestPath append:NO];
            operation.userInfo = fileDictionary;

            [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
                if (networkQueue.operationCount == 0)
                {
                    if(hasDownloadError || isCancellingSync) {
                        return ;
                    }

                    [self performSelectorInBackground:@selector(processAllFiles) withObject:nil];

                }

            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                NSLog(@"Error: %@", error);
            }];

            //            [operation setDownloadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
            //                NSLog(@"Sent %lld of %lld bytes, %@", totalBytesWritten, totalBytesExpectedToWrite, localDestPath);
            //                float progress = (float)totalBytesWritten/(float)totalBytesExpectedToWrite;
            //                [(NSMutableDictionary*)operation.userInfo setObject:[NSString stringWithFormat:@"Downloading %.0f%%", progress*100] forKey:@"downloadStatus"];
            //                [(NSMutableDictionary*)operation.userInfo setObject:[NSNumber numberWithFloat:progress] forKey:@"downloadProgress"];
            //                [syncViewController onPermitUpdated];
            //            }];

            [networkQueue addOperation:operation];
        }
    }
}

我的问题是,一旦运行此代码,内存就会慢慢被吃掉并且永远不会被回馈。现在,这些可以是大文件,这就是我使用outputStream的原因。

任何建议都将不胜感激。

1 个答案:

答案 0 :(得分:-1)

脱离我的头顶 - 我发现你没有使用ARC。

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:requestURL]

您是否在某个地方发布此操作?

 [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
                if (networkQueue.operationCount == 0)
                {
                    if(hasDownloadError || isCancellingSync) {
                        return ;
                    }

                    [self performSelectorInBackground:@selector(processAllFiles) withObject:nil];

                }

在这里,你在completionBlock中使用networkQueue并且该块保留了networkQueue,然后你将操作添加到networkQueue,后者保留了操作,这导致它们都没有解除分配。尝试制作networkQueue的弱变量并使用它来打破循环。

如果这些不能正常工作 - 运行仪器并记下存储器中剩余的物体以及它们的引用计数何时发生变化。