user5633758
user5633758

Reputation:

how adddependency method works in NSOperationQueue

i am testing NSoperationqueue

 queue = NSOperationQueue()
    let operation1 = NSBlockOperation(block: {
        let img1 = NSData(contentsOfURL: self.img1)
        NSOperationQueue.mainQueue().addOperationWithBlock({
            self.imgs1.image = UIImage(data: img1!)

        })
    })

    operation1.completionBlock = {
        print("Operation 1 completed")
    }
    queue.addOperation(operation1)




    let operation2 = NSBlockOperation(block: {
        let img2 = NSData(contentsOfURL: self.img2)
        NSOperationQueue.mainQueue().addOperationWithBlock({
            self.imgs2.image = UIImage(data: img2!)

        })
    })



    operation2.completionBlock = {
        print("Operation 2 completed")
    }
    queue.addOperation(operation2)

    let operation3 = NSBlockOperation(block: {
        let img3 = NSData(contentsOfURL: self.img3)
        NSOperationQueue.mainQueue().addOperationWithBlock({
            self.imgs3.image = UIImage(data: img3!)

        })
    })

    operation3.completionBlock = {
        print("Operation 3 completed")
    }
    queue.addOperation(operation3)

 operation3.addDependency(operation1)

}

the result is

Operation 3 completed

Operation 1 completed

Operation 2 completed

can you please explain addDependency works

it should work like operation 1 should complete first ?

how to execute one after other ?

how to create dependency ?

Upvotes: 6

Views: 3020

Answers (4)

vivekDas
vivekDas

Reputation: 1288

  1. Hi first try creating all the operations, and then add the dependency and then add them to the queue, like given below.

    create operation1 then operation2 then operation3

    After that do - operation3.addDependency(operation1)

    then add the operations to the queue, queue.addOperation(operation1) queue.addOperation(operation2) queue.addOperation(operation3)

Upvotes: 1

Chandan
Chandan

Reputation: 757

  queue = NSOperationQueue()
    let operation1 = NSBlockOperation(block: {
        let img1 = NSData(contentsOfURL: self.img1)
        NSOperationQueue.mainQueue().addOperationWithBlock({
            self.imgs1.image = UIImage(data: img1!)

        })
    })

    operation1.completionBlock = {
        print("Operation 1 completed")
    }
    queue.addOperation(operation1)




    let operation2 = NSBlockOperation(block: {
        let img2 = NSData(contentsOfURL: self.img2)
        NSOperationQueue.mainQueue().addOperationWithBlock({
            self.imgs2.image = UIImage(data: img2!)

        })
    })



    operation2.completionBlock = {
        print("Operation 2 completed")
    }
    queue.addOperation(operation2)

    let operation3 = NSBlockOperation(block: {
        let img3 = NSData(contentsOfURL: self.img3)
        NSOperationQueue.mainQueue().addOperationWithBlock({
            self.imgs3.image = UIImage(data: img3!)

        })
    })

    operation3.completionBlock = {
        print("Operation 3 completed")
    }
    queue.addOperation(operation3)

    operation2.addDependency(operation1)
    operation3.addDependency(operation2)
  }
  • Operation 2 is dependent on operation 1 (means operation 2 will start once after operation 1 completed)

  • Operation 3 will start executing after operation 2 complete.

Note: If any other other operation is there then let op4 can be start before 1,2,3

Output:


Operation 1 completed

Operation 2 completed

Operation 3 completed

Upvotes: 0

Dipen Panchasara
Dipen Panchasara

Reputation: 13600

NSOperationQueue dependency only works when you are using NSOperationQueue.mainQueue().

Consider you have to perform 2 NSOperation, one is to download image and second apply filter. In this case applying filter is dependent on download operation. Following code show dependency operation.

// *** create image download operation ***
let imageOperation: NSOperation = ...

// *** create filter operation ***
let filterOperation: NSOperation = ...

// *** Add dependency of filter operation on image download operation ***
filterOperation.addDependency(imageOperation)

// *** Init NSOperationQueue mainQueue ***
let operationQueue = NSOperationQueue.mainQueue()

// *** Add Operations to queue ***
operationQueue.addOperations([imageOperation, filterOperation], waitUntilFinished: false)

Referenced from NSHipter blog

Upvotes: -1

Farhad Faramarzi
Farhad Faramarzi

Reputation: 465

you can use it :

operation3.addDependency(operation2)
operation2.addDependency(operation1)

operation3 will start when operation2 be completed and operation2 will start when operation1 be completed .

notice : operation1 should be completed until operation2 will start

queue = NSOperationQueue()
    let operation1 = NSBlockOperation(block: {
        let img1 = NSData(contentsOfURL: self.img1)
        NSOperationQueue.mainQueue().addOperationWithBlock({
            self.imgs1.image = UIImage(data: img1!)

        })
    })

    operation1.completionBlock = {
        print("Operation 1 completed")
    }


    let operation2 = NSBlockOperation(block: {
        let img2 = NSData(contentsOfURL: self.img2)
        NSOperationQueue.mainQueue().addOperationWithBlock({
            self.imgs2.image = UIImage(data: img2!)

        })
    })



    operation2.completionBlock = {
        print("Operation 2 completed")
    }

    let operation3 = NSBlockOperation(block: {
        let img3 = NSData(contentsOfURL: self.img3)
        NSOperationQueue.mainQueue().addOperationWithBlock({
            self.imgs3.image = UIImage(data: img3!)

        })
    })

    operation3.completionBlock = {
        print("Operation 3 completed")
    }

    operation3.addDependency(operation2)
    operation2.addDependency(operation1)

    queue.addOperation(operation1)
    queue.addOperation(operation2)
    queue.addOperation(operation3)


}

Upvotes: 7

Related Questions