Home > database >  Can two same closures on the main thread, from the same function call, crash an iOS program written
Can two same closures on the main thread, from the same function call, crash an iOS program written

Time:10-01

My users experienced crashes when I sent them an update on TestFlight. After examining the eight crash reports they submitted, I've noticed a commonality - there are two of the same closures sitting on top of thread 0. Could this have caused the crash? What do you think is the cause, if not?

Please see image for crash report of thread 0. All other threads generally look the same in the report.

Note - when the users opened their app subsequent times after the initial opening, they did not experience further crashes.

Thank you in advance for your thoughts.

Update from comments, 9/29/22 -

Here's the closure code as requested by Damien and Tadreik:

When the app is opened, this line runs during initialization, which sets up the variables the connection view controller needs. Thus the empty closure.

if !twilioIDs.isEmpty { 
    ProfileModelManager.shared.getUsersForConnectionView(withTwilioIDs: twilioIDs) { _ in }
}

And the code below is invoked when the connection view is tapped on from the menu tab the second time:

if !twilioIDs.isEmpty {
    ProfileModelManager.shared.getUsersForConnectionView(withTwilioIDs: twilioIDs) { result in
        guard let success = result else { return }
            
        if success {
            self.handleSuccessOfGettingConnectionCards()
        }
        else {
            self.handleFailureOfGettingConnectionCards()
        }
    } 
}

Here is the code for handleSuccessOfGettingConnectionCards -

refreshControl.endRefreshing()
hideNoConnectionsLabel()
createChatViewControllersForChannels()
connectionsTableView.alpha = 1
errorConnectingLabel.alpha = 0
connectionsTableView.reloadData()
showActivitySpinner(false)
navigationController?.navigationBar.isHidden = true

Here is the code for handleFailureOfGettingConnectionCards -

showErrorConnectingMessage()
refreshControl.endRefreshing()
connectionsTableView.alpha = 0
hideNoConnectionsLabel()
showActivitySpinner(false)

Thanks in advance again for any intuition or experience you may share.

The crash log for thread 0

CodePudding user response:

The code inside the closure ProfileModelManager.shared.getUsersForConnectionView(withTwilioIDs: twilioIDs) { result in captures a reference to self, your problem might be that at the time of execution, self is pointing to a null reference (has been deallocated), which can cause a crash. Try to set a weak reference to self like that and see if the crash still occurs :

if !twilioIDs.isEmpty {
    ProfileModelManager.shared.getUsersForConnectionView(withTwilioIDs: twilioIDs) { [weak self] result in
        guard let success = result else { return }
            
        if success {
            self?.handleSuccessOfGettingConnectionCards()
        }
        else {
            self?.handleFailureOfGettingConnectionCards()
        }
    } 
}

If you want you could also handle the nil case of the weak self as any other optional.

  • Related