Swift : NSOperationQueue does not execute dependent operations
| Originator: | Inforeqd51 | ||
| Number: | rdar://17498071 | Date Originated: | 29-june-2014 |
| Status: | Open | Resolved: | no |
| Product: | Developer Tools | Product Version: | xCode Version 6.0 (6A216f), iOS 8.0 |
| Classification: | NSOperationQueue Swift | Reproducible: | Always |
Summary:
add two operations to the NSOperationQueue where the second operation is dependent on the first in Swift. Each of those operations dispatch their main method to the background queue. Even after the first operation completes, the next one does not execute.
whereas this happens in objective-c
Steps to Reproduce:
1. create a concurrent subclass of NSOperation where the main method is dispatched to the background queue
2. create two instances of the same and add a dependency of second to the first
3. add both to the operation queue
Expected Results:
after the first finishes, the next one should start
Actual Results:
the first executes and finishes, but the next one does not start. comment out the dispatch to background and it works.. pls note even without commenting it works in Objective c
Version:
xCode Version 6.0 (6A216f)
code sample
import Foundation
class MyConcurrentOperation:NSOperation{
var myExecuting = false
var myFinished = false
var message : String? = nil
init(){
myExecuting = false
myFinished = false
}
override var executing: Bool {
get{
return myExecuting
}
}
override var finished: Bool {
get{
return myFinished
}
}
override var asynchronous: Bool {
get {
return true
}
}
func finishOperation(){
self.willChangeValueForKey("executing")
self.willChangeValueForKey("finished")
myFinished = true
myExecuting = false
self.didChangeValueForKey("finished")
self.didChangeValueForKey("executing")
}
override func start() {
if self.cancelled{
self.finishOperation()
return;
}
self.willChangeValueForKey("executing")
myExecuting = true
self.didChangeValueForKey("executing")
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), {
self.main()
})
}
override func main() {
autoreleasepool({
if let message = self.message {
println("this is the main function with message:\(self.message!)")
}
})
self.finishOperation()
}
}
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var myoperation = MyConcurrentOperation()
myoperation.message = "this is first operation"
var mynextoperation = MyConcurrentOperation()
mynextoperation.message = "this is second operation"
mynextoperation.addDependency(myoperation)
let operationQ = NSOperationQueue();
operationQ.addOperation(myoperation)
operationQ.addOperation(mynextoperation)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!