0

I have to used NSOperations to download images (and save to disk), but memory didn't released! After some googling i found hook, that my operations in the retention cycle. I tried to fix my code, but problem is still here.

Can anyone find the error in my code?

There is my code:

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];
Nostromo
  • 21
  • 2
  • 7
  • 1
    OK, question 1: Where is `queue` stored? Don't you need to keep a reference to it in something like an instance variable? – trojanfoe Jan 28 '16 at 15:40
  • @trojanfoe this is a single method. Why i need to keep reference on queue? I don't need it after complection. – Nostromo Jan 28 '16 at 15:45
  • So what stops the queue from being destroyed? Does the system keep a reference somewhere? – trojanfoe Jan 28 '16 at 15:46
  • This cycle performs well, I'm got my images on the disk. Problem is in memory. Also ARC. – Nostromo Jan 28 '16 at 15:48
  • @trojanfoe - is probably right that the queue ought to be retained (see here: http://stackoverflow.com/questions/2500933/is-it-dangerous-to-set-off-an-autoreleased-nsoperationqueue) But the OP says the problem is garbage due to a retain cycle. I don't see any self reference -- direct or indirect -- in what's posted. Can the OP provide evidence that there's really a problem? – danh Jan 28 '16 at 16:26

0 Answers0