Why is DownloadEarthquakesOperation a GroupOperation

I've been porting the sample code from the Advanced NSOperation session to Objective C/iOS 8.

In doing that, I noticed that the DownloadEarthquakesOepraiton is a GroupOperation, but only seems to have one operation in it. Why is that? What purpose does it solve to have a GroupOperation with a single Operation? @DaveDelong

Accepted Answer

DownloadEarthquakesOperation is a group operation because of "self".


When creating the NSURLSessionTask, I needed to capture "self" inside the completion block in order to handle the results. However, if this were a URLSessionTaskOperation, I wouldn't be able to reference self before the call to super.init(task:), but I would have to do it before calling super because super requires the NSURLSessionTask. So there's this stalemate where I have to configure the task before calling super, but I can't configure the task before calling super because I need self.


The quickest way to work around this was to not subclass URLSessionTaskOperation, but instead put it inside a group, where I have explicit control over the timing of the creation of the operation.

Why is DownloadEarthquakesOperation a GroupOperation
 
 
Q