首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AFNetworking存储器

AFNetworking存储器
EN

Stack Overflow用户
提问于 2013-10-10 11:01:55
回答 1查看 532关注 0票数 1

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

代码语言:javascript
复制
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的原因。

如有任何建议,将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-10 11:05:17

从我头上-我看到你不是在用ARC。

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

你要在某个地方发布这个行动吗?

代码语言:javascript
复制
 [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
                if (networkQueue.operationCount == 0)
                {
                    if(hasDownloadError || isCancellingSync) {
                        return ;
                    }

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

                }

在这里,您在completionBlock中使用completionBlock,而块保留networkQueue,然后将操作添加到networkQueue中,后者保留操作,这导致两个操作都不释放。尝试创建networkQueue的一个弱变量,并使用它来打破循环。

如果这些不工作,运行仪器,并记录下哪些对象留在内存中,以及它们的引用计数何时更改。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19294006

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档