Kodeco Forums

Video Tutorial: Introducing Concurrency Part 4: Dependencies

Learn how to use the results from one operation in another and specify that the second operation shouldn't be executed until the first has completed.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/4034-introducing-concurrency/lessons/5

Hi Sam!

I couldn’t get any images in filteredImages array. Here is the code.

class TiltShiftOperation : NSOperation {
  var inputImage: UIImage?
  var outputImage: UIImage?
      
  override func main() {
    // TODO: Update the input image location to check for input from dependencies

    if let dependencyData = dependencies
      .filter({$0 is ImageFilterDataProvider })
      .first as? ImageFilterDataProvider
      where inputImage == .None{
      inputImage = dependencyData.filteredImage
    }
        
    guard let inputImage = inputImage else { return }

    let mask = topAndBottomGradient(inputImage.size)
    outputImage = inputImage.applyBlurWithRadius(4, maskImage: mask)
  }
}

protocol ImageFilterDataProvider {
  // TODO: Fill this is
  var filteredImage: UIImage? {get}
}

extension TiltShiftOperation: ImageFilterDataProvider{
  var filteredImage: UIImage? {return outputImage}
}


for compressedFile in compressedFilePaths {
  guard let inputURL = compressedFile else { continue }

  // TODO: Update the operation graph to add filtering
  let loadingOperation = DataLoadOperation(url: inputURL)
  let decompressionOp = ImageDecompressionOperation()
  let filteringOp = TiltShiftOperation()
  filteringOp.completionBlock = {
    guard let output = filteringOp.outputImage else { return }
    appendQueue.addOperationWithBlock {
      filteredImages.append(output)
    }
  }

  loadingOperation |> decompressionOp |> filteringOp
  queue.addOperations([loadingOperation, decompressionOp, filteringOp], waitUntilFinished: false)
}

filteredImages

What could be the problem?

Try adding the following list before you try and look at the content of filteredImages:

//: Need to wait for the queue to finish before checking the results
queue.waitUntilAllOperationsAreFinished()

At the moment the code is inspecting the contents of the output array before it has had a chance to be populated.

sam

Hi Sam, will these advanced videos be updated for Swift3. All the swift compiler errors take away from a good learning experience. Interesting topic and hard too so would be nice work with code that compiles.

Hi @ronaldoh1

This series is currently being updated, with new material, better explanations and everything fixed for Swift 3 & Xcode 8. I’m not sure of the release schedule, but it hopefully won’t be that long.

sam

cool cool. Thank you - looking forward to. It’s a hard topic but interesting. Hope it’s sometime soon.

1 Like