3

I am having problems debugging a recent crash. I manage to get some feedback from Firebase but I am not able to replicate the conditions. The crash happens when I press the home button and come back into the app some times.

Here is the stack trace and the code I have related to Operations.

EXC_BAD_ACCESS KERN_INVALID_ADDRESS

    Crashed: com.apple.main-thread
0  Foundation                     0x184732c94 __NSOQSchedule + 248
1  Foundation                     0x18465ab50 -[NSOperationQueue setSuspended:] + 164
2  UIKitCore                      0x18849e420 -[UIApplication _stopDeactivatingForReason:] + 1172
3  UIKitCore                      0x187c17628 -[_UISceneLifecycleMultiplexer _performBlock:withApplicationOfDeactivationReasons:fromReasons:] + 436
4  UIKitCore                      0x187c1798c -[_UISceneLifecycleMultiplexer _evalTransitionToSettings:fromSettings:forceExit:withTransitionStore:] + 756
5  UIKitCore                      0x187c17214 -[_UISceneLifecycleMultiplexer uiScene:transitionedFromState:withTransitionContext:] + 340
6  UIKitCore                      0x187c1b930 __186-[_UIWindowSceneFBSSceneTransitionContextDrivenLifecycleSettingsDiffAction _performActionsForUIScene:withUpdatedFBSScene:settingsDiff:fromSettings:transitionContext:lifecycleActionType:]_block_invoke_2 + 196
7  UIKitCore                      0x188001604 +[BSAnimationSettings(UIKit) tryAnimatingWithSettings:actions:completion:] + 856
8  UIKitCore                      0x1880fca0c _UISceneSettingsDiffActionPerformChangesWithTransitionContext + 260
9  UIKitCore                      0x187c1b66c __186-[_UIWindowSceneFBSSceneTransitionContextDrivenLifecycleSettingsDiffAction _performActionsForUIScene:withUpdatedFBSScene:settingsDiff:fromSettings:transitionContext:lifecycleActionType:]_block_invoke + 156
10 UIKitCore                      0x1880fc8f4 _UISceneSettingsDiffActionPerformActionsWithDelayForTransitionContext + 108

And in the AppDelegate class:

    let alertQueue = Foundation.OperationQueue()
    func applicationDidFinishLaunching(_ application: UIApplication) {
...
         alertQueue.isSuspended = true
         alertQueue.maxConcurrentOperationCount = 1
...
    }
        
    func applicationDidBecomeActive(_ application: UIApplication) {
             
         alertQueue.isSuspended = false
    }
            
    func applicationWillResignActive(_ application: UIApplication) {
                
         alertQueue.isSuspended = true
    }
     

Operation Class

class CustomOperation: Operation {
    
    private var _finished = false
    private var _executing = false
    override var isAsynchronous: Bool {return true}
    override var isExecuting: Bool {return _executing}
    override var isFinished: Bool {return _finished}
    
    override func start() {
        guard !isCancelled else {
            reportFinish()
            return
        }
        reportStart()
    }
    
    override func cancel() {
        self.reportFinish()
        super.cancel()
    }
    
    private func reportStart() {
        if !_executing {
            willChangeValue(for: \.isExecuting)
            _executing = true
            didChangeValue(for: \.isExecuting)
        }
    }
    
    private func reportFinish() {
        
        let willChangeExecuting = _executing
        let willChangeFinised = !_finished
        
        if willChangeExecuting {
            willChangeValue(for: \.isExecuting)
        }
        if willChangeFinised {
            willChangeValue(for: \.isFinished)
        }
        
        if willChangeExecuting {
            _executing = false
        }
        if willChangeFinised {
            _finished = true
        }
        
        if willChangeExecuting {
            didChangeValue(for: \.isExecuting)
        }
        if willChangeFinised {
            didChangeValue(for: \.isFinished)
        }
    }
}
      
PoolHallJunkie
  • 1,345
  • 14
  • 33
  • Suspend does not have any affect on operations already started. Try commenting out the two suspend statements? Still crashing? Might be the operations themselves. The crash appears to be when you set suspend true - is that correct? – David H Aug 07 '20 at 11:31
  • Yes seems to be true , as I cannot replicate it (only appears once every few days when testing on device). So it seems to be crashing when I leave the app, but I realise the crash when I go back in. The operations themselves are actually a subclass of Operation , so it might be the problem. I will update the code – PoolHallJunkie Aug 07 '20 at 11:43
  • @DavidH Added some more info. Will remove the set suspend and monitor – PoolHallJunkie Aug 07 '20 at 13:00
  • It can be, that you are trying to update UI from none main thread. Cam you post whole crash log. I do not know how big your project, but you would need to visit every completion block and check is UI changes done on main thread. – Ramis Aug 14 '20 at 07:25

0 Answers0