Search code examples
iosnsoperationnsoperationqueue

NSOperation pass back data


I have been searching but can only find the delegate pattern idea to pass back data from a NSOperation. I have a NSOperation that downloads data upon completion of that NSOperation I would like it to pass back to the class that put it in a NSoperationQueue the data it downloaded. There could be upmost of 100 of these NSOPerations in my queue, all retrieving unique data. Any ideas would be greatly appreciated.


Solution

  • Another idea would be to use blocks. You can initialize your NSOperation object with a block

    typedef void(^CompletionBlock)(NSData *data);
    
    - (id) initWithCompletionBlock: (CompletionBlock) block;
    

    and call it after the operation is finished and data has been downloaded.

    dispatch_async(dispatch_get_main_queue(), ^{
        if(block){
            block(fetchedData);
        }
    });