iOS 13: Searchbar crashing when cancel is hit repeatedly

Viewed 843

I'm getting a crash when users click the cancel button repeatedly:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Nested internal _UISearchBar transitions not yet supported. This is an internal UIKit bug.'

Steps:

  • Launch a window with the search bar showing.
  • Make the search bar text field the first responder. I have the cancel button enabled, so it shows up.
  • Tap on the location of the cancel button a few times.
  • Watch the app crash.

Log:

*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff23b98bde __exceptionPreprocess + 350
    1   libobjc.A.dylib                     0x00007fff503b5b20 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff23b98958 +[NSException raise:format:arguments:] + 88
    3   Foundation                          0x00007fff255eb6f5 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 191
    4   UIKitCore                           0x00007fff468697ba -[_UISearchBarVisualProviderIOS prepareForTransitionToSearchLayoutState:] + 1306
    5   UIKitCore                           0x00007fff46869174 -[_UISearchBarVisualProviderIOS driveTransitionToSearchLayoutState:] + 180
    6   UIKitCore                           0x00007fff471c046a __68-[_UISearchControllerTransplantSearchBarAnimator animateTransition:]_block_invoke.98 + 107
    7   UIKitCore                           0x00007fff4762a1e6 +[UIView(UIViewAnimationWithBlocks) _setupAnimationWithDuration:delay:view:options:factory:animations:start:animationStateGenerator:completion:] + 528
    8   UIKitCore                           0x00007fff4762a795 +[UIView(UIViewAnimationWithBlocks) animateWithDuration:delay:options:animations:completion:] + 99
    9   UIKitCore                           0x00007fff471bfc15 -[_UISearchControllerTransplantSearchBarAnimator animateTransition:] + 862
    10  UIKitCore                           0x00007fff46b2c49e ___UIViewControllerTransitioningRunCustomTransition_block_invoke_2 + 54
    11  UIKitCore                           0x00007fff46c41ece +[UIInputResponderController _pinInputViewsForInputResponderController:onBehalfOfResponder:duringBlock:] + 99
    12  UIKitCore                           0x00007fff46b2c432 ___UIViewControllerTransitioningRunCustomTransition_block_invoke.645 + 180
    13  UIKitCore                           0x00007fff47628751 +[UIView(Animation) _setAlongsideAnimations:toRunByEndOfBlock:] + 175
    14  UIKitCore                           0x00007fff46b2c2b8 _UIViewControllerTransitioningRunCustomTransition + 553
    15  UIKitCore                           0x00007fff46a0fccb __56-[UIPresentationController runTransitionForCurrentState]_block_invoke.452 + 2632
    16  UIKitCore                           0x00007fff4716b85a _runAfterCACommitDeferredBlocks + 352
    17  UIKitCore                           0x00007fff4715c63c _cleanUpAfterCAFlushAndRunDeferredBlocks + 248
    18  UIKitCore                           0x00007fff4718bc6e _afterCACommitHandler + 85
    19  CoreFoundation                      0x00007fff23afaeb7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    20  CoreFoundation                      0x00007fff23af594e __CFRunLoopDoObservers + 430
    21  CoreFoundation                      0x00007fff23af5fca __CFRunLoopRun + 1514
    22  CoreFoundation                      0x00007fff23af56b6 CFRunLoopRunSpecific + 438
    23  GraphicsServices                    0x00007fff3815cbb0 GSEventRunModal + 65
    24  UIKitCore                           0x00007fff47162a67 UIApplicationMain + 1621
    25  Properly                            0x000000010d3426d2 main + 80
    26  libdyld.dylib                       0x00007fff5123bcf5 start + 1
    27  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
2 Answers

Simple solution could be disable cancel button upon tapping once. And then perform the operation under it and when process gets completed you can enable it if you want.

find the below fix for this issue.

var isSeachBarAnimationCompleted: Bool = false

func didPresentSearchController(_ searchController: UISearchController) {

 DispatchQueue.main.async(execute: 
{

            self.isSeachBarAnimationCompleted = true
            searchController.searchBar.becomeFirstResponder()
        })
}

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) 

{

   guard self.isSeachBarAnimationCompleted else 

    { 
       return 
     }
        self.isSeachBarAnimationCompleted = false
     //Do you code here...
  }

Hope this will resolve the issue.

Related