NSBlockOperation保留循环

时间:2016-01-28 15:19:47

标签: ios objective-c cycle nsoperation nsblockoperation

我必须使用NSOperations下载图像(并保存到磁盘),但内存没有发布!经过一些谷歌搜索我发现钩子,我的操作在保留周期。我试图修复我的代码,但问题仍然存在。

任何人都可以在我的代码中找到错误吗?

有我的代码:

NSOperationQueue *queue = [[NSOperationQueue alloc] init];
queue.maxConcurrentOperationCount = 10;

__block NSBlockOperation *completionOperation = [NSBlockOperation blockOperationWithBlock:^{
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        complection(YES); // this is a method's callback actually
    }];
}];

for (NSURL* url in imagesURLs) {
    __weak NSBlockOperation *downdloadOperation = [NSBlockOperation blockOperationWithBlock:^{
        NSData *imageData = [NSData dataWithContentsOfURL:url];
        if (imageData) {
            [[RuzaImageDiskHandler instance] saveImage:imageData forUrlKey:url];
        }
    }];

    [completionOperation addDependency:downdloadOperation];
}

[queue addOperations:completionOperation.dependencies waitUntilFinished:NO];
[queue addOperation:completionOperation];

0 个答案:

没有答案