Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting an NSInternalInconsistencyException error #355

Closed
IchordeDionysos opened this issue Apr 24, 2017 · 6 comments
Closed

Getting an NSInternalInconsistencyException error #355

IchordeDionysos opened this issue Apr 24, 2017 · 6 comments
Assignees

Comments

@IchordeDionysos
Copy link

I'm getting an NSInternalInconsistencyException exception, what can it be. I don't know where to start as my app only appears in the Main method...

Exception:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'no UICollectionViewLayoutAttributes instance for -layoutAttributesForItemAtIndexPath: <NSIndexPath: 0x600000229bc0> {length = 2, path = 0 - 0}'
*** First throw call stack:
(
	0   CoreFoundation                      0x0000000101411b0b __exceptionPreprocess + 171
	1   libobjc.A.dylib                     0x000000010553f141 objc_exception_throw + 48
	2   CoreFoundation                      0x0000000101415cf2 +[NSException raise:format:arguments:] + 98
	3   Foundation                          0x00000001050d93b6 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 193
	4   UIKit                               0x000000010372b5d0 -[UICollectionViewData layoutAttributesForItemAtIndexPath:] + 636
	5   UIKit                               0x00000001036d7563 -[UICollectionView layoutAttributesForItemAtIndexPath:] + 57
	6   XLPagerTabStrip                     0x000000010461cdd6 _TFC15XLPagerTabStrip13ButtonBarView4movefT9fromIndexSi7toIndexSi18progressPercentageV12CoreGraphics7CGFloat11pagerScrollOS_11PagerScroll_T_ + 262
	7   XLPagerTabStrip                     0x0000000104614c16 _TFC15XLPagerTabStrip36ButtonBarPagerTabStripViewController15updateIndicatorfT3forCS_27PagerTabStripViewController9fromIndexSi7toIndexSi22withProgressPercentageV12CoreGraphics7CGFloat15indexWasChangedSb_T_ + 294
	8   XLPagerTabStrip                     0x0000000104619391 _TTWC15XLPagerTabStrip36ButtonBarPagerTabStripViewControllerS_34PagerTabStripIsProgressiveDelegateS_FS1_15updateIndicatorfT3forCS_27PagerTabStripViewController9fromIndexSi7toIndexSi22withProgressPercentageV12CoreGraphics7CGFloat15indexWasChangedSb_T_ + 113
	9   XLPagerTabStrip                     0x000000010462a610 _TFC15XLPagerTabStrip27PagerTabStripViewController13updateContentfT_T_ + 9968
	10  XLPagerTabStrip                     0x00000001046272f6 _TFC15XLPagerTabStrip27PagerTabStripViewController14updateIfNeededfT_T_ + 406
	11  XLPagerTabStrip                     0x0000000104625c55 _TFC15XLPagerTabStrip27PagerTabStripViewController21viewDidLayoutSubviewsfT_T_ + 101
	12  XLPagerTabStrip                     0x00000001046137a9 _TFC15XLPagerTabStrip36ButtonBarPagerTabStripViewController21viewDidLayoutSubviewsfT_T_ + 41
	13  XLPagerTabStrip                     0x0000000104613ae2 _TToFC15XLPagerTabStrip36ButtonBarPagerTabStripViewController21viewDidLayoutSubviewsfT_T_ + 34
	14  UIKit                               0x0000000102e5828b -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1396
	15  QuartzCore                          0x00000001028cb904 -[CALayer layoutSublayers] + 146
	16  QuartzCore                          0x00000001028bf526 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 370
	17  QuartzCore                          0x00000001028bf3a0 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
	18  QuartzCore                          0x000000010284ee92 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294
	19  QuartzCore                          0x000000010287b130 _ZN2CA11Transaction6commitEv + 468
	20  UIKit                               0x0000000102d8e307 _UIApplicationFlushRunLoopCATransactionIfTooLate + 167
	21  UIKit                               0x000000010358ecab __handleEventQueue + 5843
	22  CoreFoundation                      0x00000001013b7c01 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
	23  CoreFoundation                      0x000000010139d0cf __CFRunLoopDoSources0 + 527
	24  CoreFoundation                      0x000000010139c5ff __CFRunLoopRun + 911
	25  CoreFoundation                      0x000000010139c016 CFRunLoopRunSpecific + 406
	26  GraphicsServices                    0x0000000107df5a24 GSEventRunModal + 62
	27  UIKit                               0x0000000102d950d4 UIApplicationMain + 159
	28  TheSimpleClub-Nachhilfe             0x00000001003859e7 main + 55
	29  libdyld.dylib                       0x000000010646165d start + 1
	30  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

I'm using Latest XCode, Swift (updated yesterday) and XLPagerTabStrip (just installed using CocoaPods)

My Code:
Pager Tab Controller:

import UIKit
import XLPagerTabStrip

class RememberedViewController: ButtonBarPagerTabStripViewController {

    var isReload = false

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - PagerTabStripDataSource

    override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {
        var videoItems = [ContentItem]()
        videoItems.append(ContentItem(contentType: 0, image: #imageLiteral(resourceName: "ContentIcon"), title: "Video1", subject: "maths", grade: 8, progress: 0, action: ContentItem.ACTION_MORE))
        videoItems.append(ContentItem(contentType: 0, image: #imageLiteral(resourceName: "ContentIcon"), title: "Video2", subject: "biology", grade: 7, progress: 0, action: ContentItem.ACTION_MORE))
        videoItems.append(ContentItem(contentType: 0, image: #imageLiteral(resourceName: "ContentIcon"), title: "Video3", subject: "physics", grade: 13, progress: 0, action: ContentItem.ACTION_MORE))
        let videosSource = ContentItemDataSource(items: videoItems)

        var taskItems = [ContentItem]()
        taskItems.append(ContentItem(contentType: 0, image: #imageLiteral(resourceName: "ContentIcon"), title: "Task1", subject: "maths", grade: 8, progress: 0, action: ContentItem.ACTION_MORE))
        taskItems.append(ContentItem(contentType: 0, image: #imageLiteral(resourceName: "ContentIcon"), title: "Task2", subject: "biology", grade: 7, progress: 0, action: ContentItem.ACTION_MORE))
        taskItems.append(ContentItem(contentType: 0, image: #imageLiteral(resourceName: "ContentIcon"), title: "Task3", subject: "physics", grade: 13, progress: 0, action: ContentItem.ACTION_MORE))
        let tasksSource = ContentItemDataSource(items: taskItems)

        var handoutItems = [ContentItem]()
        handoutItems.append(ContentItem(contentType: 0, image: #imageLiteral(resourceName: "ContentIcon"), title: "Handout1", subject: "maths", grade: 8, progress: 0, action: ContentItem.ACTION_MORE))
        handoutItems.append(ContentItem(contentType: 0, image: #imageLiteral(resourceName: "ContentIcon"), title: "Handout2", subject: "biology", grade: 7, progress: 0, action: ContentItem.ACTION_MORE))
        handoutItems.append(ContentItem(contentType: 0, image: #imageLiteral(resourceName: "ContentIcon"), title: "Handout3", subject: "physics", grade: 13, progress: 0, action: ContentItem.ACTION_MORE))
        let handoutsSource = ContentItemDataSource(items: handoutItems)

        let videos_tab = ContentListViewController(itemInfo: "Videos", datasource: videosSource)
        let tasks_tab = ContentListViewController(itemInfo: "Tasks", datasource: tasksSource)
        let handouts_tab = ContentListViewController(itemInfo: "Handouts", datasource: handoutsSource)

        guard isReload else {
            return [videos_tab, tasks_tab, handouts_tab]
        }

        var childViewControllers = [videos_tab, tasks_tab, handouts_tab]

        for (index, _) in childViewControllers.enumerated(){
            let nElements = childViewControllers.count - index
            let n = (Int(arc4random()) % nElements) + index
            if n != index{
                swap(&childViewControllers[index], &childViewControllers[n])
            }
        }
        let nItems = 1 + (arc4random() % 8)
        return Array(childViewControllers.prefix(Int(nItems)))
    }

    override func reloadPagerTabStripView() {
        isReload = true
        if arc4random() % 2 == 0 {
            pagerBehaviour = .progressive(skipIntermediateViewControllers: arc4random() % 2 == 0, elasticIndicatorLimit: arc4random() % 2 == 0 )
        }
        else {
            pagerBehaviour = .common(skipIntermediateViewControllers: arc4random() % 2 == 0)
        }
        super.reloadPagerTabStripView()
    }
}

Tab Controller:

import UIKit
import XLPagerTabStrip

class ContentListViewController: UITableViewController, IndicatorInfoProvider {

    var itemInfo = IndicatorInfo(title: "View")
    var datasource: ContentItemDataSource

    init(itemInfo: IndicatorInfo, datasource: ContentItemDataSource) {
        self.itemInfo = itemInfo
        self.datasource = datasource
        super.init(style: .plain)
    }

    init(style: UITableViewStyle, itemInfo: IndicatorInfo, datasource: ContentItemDataSource) {
        self.itemInfo = itemInfo
        self.datasource = datasource
        super.init(style: style)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(ContentItemView.self, forCellReuseIdentifier: Config.CONTENT_ITEM_CELL)
        tableView.estimatedRowHeight = 60.0;
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.allowsSelection = false
    }

    // MARK: - IndicatorInfoProvider

    func indicatorInfo(for pagerTabStripController: PagerTabStripViewController) -> IndicatorInfo {
        return itemInfo
    }
}
@IchordeDionysos
Copy link
Author

I'm really confused why this happens ...

@m-revetria m-revetria self-assigned this Apr 27, 2017
@m-revetria
Copy link
Member

@IchordeDionysos I'm trying to figure out whats is causing the exception.

Seems like the child view controllers weren't set before the layout update.

From the stacktrace that you attached, the exception is thrown from the function ButtonBarView.moveTo(index: Int, animated: Bool, swipeDirection: SwipeDirection, pagerScroll: PagerScroll). The class ButtonBarView contains the collection of tab's buttons and it is failing to render the first item (with IndexPath = (0, 0)).

I ran your sample code and it didn't crash. So it would be something else. Can you check if you are removing the child controllers from your RememberedViewController in some way? or if you call reloadPagerTabStripView() asynchronously?

Which versions of iOS and XLPagerTabStrip are you using? Any additional information might help us to solve this problem.

Thanks

@IchordeDionysos
Copy link
Author

I didn't found the String reloadPagerTabStripView anywhere else beside the XLPagerTabStrip in my workspace. :)

I don't know how I could remove the child controllers. As I understand it I have to return them in the function viewControllers(for pagerTabStripController, ...).

xCode: Version 8.3.2 (8E2002)
Simulator: Version 10.0 (SimulatorApp-745.10)
iOS: 10.3
XLPagerTabStrip: 7.0.0 (installed using CocoaPods)

My project:
http://jmp.sh/gQdXVaZ

@m-revetria
Copy link
Member

@IchordeDionysos thanks for the sample project. I was able to reproduce the issue locally. I'll be working on this and get back to you as soon I have news

@m-revetria
Copy link
Member

@IchordeDionysos I found the issue 🎉

The issue comes from the storyboard, specifically on how the ButtonBarView was added to the view controller. Looks like first you added a UIView to the storyboard and then you changed its base class to be ButtonBarView. You should have used a UICollectionView instead, so some of its properties are set by the IB like the collection view layout, which in this case was null and it was throwing the error.

Thanks for the sample project to reproduce this issue, it was really helpful, not a chance to see the issue without it.

@IchordeDionysos
Copy link
Author

Well I changed it to be an ButtonBarView because it was told me there: https://github.com/xmartlabs/XLPagerTabStrip#connect-outlets-and-add-layout-constraints 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants