0

I'm seeing a mysterious crash from Crashlytics that I can't determine the cause of. Any possible guidance would be appreciated.

In my HomeViewController class in the viewWillAppear method, I'm calling the following method:

func showSplash() {
        view.bringSubview(toFront: splashView)
        self.navigationController?.isNavigationBarHidden = true
        UIApplication.shared.setStatusBarHidden(true, with: UIStatusBarAnimation.none)
        splashVC = self.storyboard!.instantiateViewController(withIdentifier: "SplashViewController") as? SplashViewController
        if splashVC != nil && splashView != nil {
            splashVC!.delegate = self
            splashVC!.view.frame = splashView.bounds
            addChildViewController(splashVC!) // CRASHING ON THIS LINE
            splashView.addSubview(splashVC!.view)
            splashVC!.didMove(toParentViewController: self)
        }
}

Crashlytics is reporting that for a small number of users, it's crashing on the addChildViewController() line in the above method, and it's crashing multiple times for this small subset of users. I'm unable to reproduce. Here's the log from Crashlytics:

Crashed: Thread
0  TSMNJ                         0x10009e5b4 HomeViewController.showSplash() -> () (HomeViewController.swift:173)
1  TSMNJ                         0x1000a7c34 specialized HomeViewController.viewWillAppear(Bool) -> () (HomeViewController.swift:125)
2  TSMNJ                         0x10009d838 @objc HomeViewController.viewWillAppear(Bool) -> () (HomeViewController.swift)
3  UIKit                          0x19351a754 -[UIViewController _setViewAppearState:isAnimating:] + 624
4  UIKit                          0x19351a4cc -[UIViewController __viewWillAppear:] + 156
5  UIKit                          0x1935bb740 -[UINavigationController _startTransition:fromViewController:toViewController:] + 752
6  UIKit                          0x1935baf28 -[UINavigationController _startDeferredTransitionIfNeeded:] + 856
7  UIKit                          0x1935baadc -[UINavigationController __viewWillLayoutSubviews] + 64
8  UIKit                          0x1935baa40 -[UILayoutContainerView layoutSubviews] + 188
9  UIKit                          0x1934ffa80 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1196
10 QuartzCore                     0x1909ad9d8 -[CALayer layoutSublayers] + 148
11 QuartzCore                     0x1909a24cc CA::Layer::layout_if_needed(CA::Transaction*) + 292
12 QuartzCore                     0x1909a238c CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 32
13 QuartzCore                     0x19091f3e0 CA::Context::commit_transaction(CA::Transaction*) + 252
14 QuartzCore                     0x190946a68 CA::Transaction::commit() + 512
15 QuartzCore                     0x190947488 CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 120
16 CoreFoundation                 0x18d5fa0c0 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 32
17 CoreFoundation                 0x18d5f7cf0 __CFRunLoopDoObservers + 372
18 CoreFoundation                 0x18d5262d8 CFRunLoopRunSpecific + 476
19 UIKit                          0x19356d7b0 -[UIApplication _run] + 608
20 UIKit                          0x193568534 UIApplicationMain + 208
21 TSMNJ                         0x1000750e8 main (AppDelegate.swift:20)
22 libdispatch.dylib              0x18c5095b8 (Missing)
codeman
  • 8,868
  • 13
  • 50
  • 79
  • tl;dr: Don't use `if splashVC != nil && splashView != nil { }`, use an `if let` or `guard`, which ensures that your variables are non-nil in that scope. Remove the extra `!` marks elsewhere and make your code safer. – JAL Sep 01 '17 at 14:00
  • Yeah but would doing it how I have it now actually cause a nil crash? – codeman Sep 01 '17 at 14:08
  • Yes, due to threading issues it could be possible that on the line where you check for nil `splashVC` could be non nil, but could be set to nil by the time you enter the scope of the if. – JAL Sep 01 '17 at 14:11
  • Interesting, I didn't realize that was possible. Thank you. – codeman Sep 01 '17 at 14:16
  • You can find some related discussion here: https://stackoverflow.com/a/45329642/2415822 – JAL Sep 01 '17 at 14:25
  • If you move your comments to an answer, I'll accept it as the answer. Issue seems to be resolved now based on looking at Crashlytics. – codeman Sep 05 '17 at 15:35

0 Answers0