5

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

Devapploper
  • 6,062
  • 3
  • 20
  • 41
Andrea Bozza
  • 1,374
  • 2
  • 12
  • 31

2 Answers2

10

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.

Yoichi Tagaya
  • 4,547
  • 2
  • 27
  • 38
0

I notice the same problem in xcode 7 . For example

  let delay = 0.2 * Double(NSEC_PER_SEC)
  let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))

            dispatch_after(time, dispatch_get_main_queue(), {



            })

works only if i also set optimimizazion level None[-OO]

Andrea Bozza
  • 1,374
  • 2
  • 12
  • 31