Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift presentViewController completion block working only in debug not called in release

I have a strange behaviour with a completion block in Debug and Release. For example:

        sourceViewController.presentViewController(ccVC, animated: true, completion: { () -> Void in
            NSUserDefaults.standardUserDefaults().setBool(true, forKey: kChromeCastInstructionsShown)
            NSUserDefaults.standardUserDefaults().synchronize()
            println("save bolean")
        })

In debug: println("save bolean") print string In relase: println("save bolean") print nothing

Any idea about this behaviour? Someone experiment a clear solution?

Kind Andrea

like image 885
Andrea Bozza Avatar asked Mar 16 '15 08:03

Andrea Bozza


1 Answers

It looks a Swift compiler bug (at least version 1.1) discussed here: https://github.com/ReactiveCocoa/ReactiveCocoa/issues/1632

In a release build, closures are not invoked sometimes especially when they are sequenced like:

array.map({ $0 * 2 }).map({ $0 * 3 }).map({ $0 * 4 })...

The problem appears in Swift only, not in Objective-C. It can be workarounded by setting Swift Compiler Optimization Level to None [-Onone] if your app does not require the better performance by the optimization.

Screenshot

Or another workaround is naming the closure as a function:

func completionHandler() {
    NSUserDefaults.standardUserDefaults().setBool(true, forKey: kChromeCastInstructionsShown)
    NSUserDefaults.standardUserDefaults().synchronize()
    println("save bolean")
}

sourceViewController.presentViewController(ccVC, animated: true, completion: completionHandler)

I take the former workaround for my project because I want to keep my code simple and don't need the performance enhancement by the optimization.

like image 162
Yoichi Tagaya Avatar answered Oct 10 '22 08:10

Yoichi Tagaya