TabView icon indicating copy to clipboard operation
TabView copied to clipboard

Adding a tab via `activateTab` when `viewControllers` is empty causes crash.

Open iosdevzone opened this issue 6 years ago • 5 comments

If the view controllers array is empty and activateTab is called to add a tab an assertion failure will cause the program to crash

2018-04-02 15:18:46.925062-0700 TabView Sample[331:36129] 
*** Assertion failure in -[TabView.TabViewTabCollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:animator:],
 /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3698.52.10/UICollectionView.m:6031
2018-04-02 15:18:46.926864-0700 TabView Sample[331:36129] 
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
reason: 'Invalid update: invalid number of items in section 0.  
The number of items contained in an existing section after the update (1) must be equal to 
the number of items contained in that section before the update (1), plus or minus the number 
of items inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the 
number of items moved into or out of that section (0 moved in, 0 moved out).'

This behavior can be seen in the Sample project by making the following edit to ViewController.swift

        // Set the tabs in this tab view
//        self.viewControllers = [
//            Tab(title: "White", color: .white),
//            Tab(title: "Black", color: .black),
//            Tab(title: "Red", color: .red),
//            Tab(title: "Green", color: .green),
//            Tab(title: "Blue", color: .blue),
//            Tab(title: "A really long title", color: .blue)
//        ]
		
		self.activateTab(Tab(title: "Red", color: .red))

I haven't been able to quite figure out what is going on. If I do a PR will be forthcoming.

iosdevzone avatar Apr 02 '18 22:04 iosdevzone

A little more information on this.

As long as the viewControllers array is assigned before the first call to activateTab the problem does not occur. So it must be some side-effect of the set block on viewControllers.

    // No crash
    self.viewControllers = [ ]		
    self.activateTab(Tab(title: "Red", color: .red))

iosdevzone avatar Apr 02 '18 22:04 iosdevzone

The above commit (46bf391) appears to solve the problem for me.

iosdevzone avatar Apr 02 '18 23:04 iosdevzone

Thanks for looking into this, @iosdevzone. I'll take a look into this as well. If you have a working solution, feel free to open a PR.

ian-mcdowell avatar Apr 03 '18 18:04 ian-mcdowell

I've opened PR #2, which fixes this issue locally for me. Can you confirm?

ian-mcdowell avatar Apr 03 '18 19:04 ian-mcdowell

Thanks for the speedy response. Yes, #2 solves the problem and probably does less unnecessary work than my solution.

Oh and also, thanks for this great framework. It's saved me a ton of time!

iosdevzone avatar Apr 04 '18 23:04 iosdevzone