RxSwift icon indicating copy to clipboard operation
RxSwift copied to clipboard

Assertion failure in -[UICollectionViewData numberOfItemsInSection:]

Open YanisSOTO opened this issue 9 years ago • 13 comments

Hello there, i've some crash since the last release of Rx. Error : Assertion failure in -[UICollectionViewData numberOfItemsInSection:]

Platform/Environment : iOS

This appears when i set a layout to my collectionView.

Check the code bellow :

http://stackoverflow.com/questions/41460870/assertion-failure-in-uicollectionview-since-last-release-of-rxswift-datasources

As you can see, before the code working but now it's like there is something to do more with the section but what ? This kind of code is used in other file and it's working. I really do not understand. In 3.0.0 it's working, in 3.1.1 not. If someone have an idea what's need to be update a big thank's !

YanisSOTO avatar Jan 04 '17 10:01 YanisSOTO

Hi @YanisSOTO ,

It's little hard to figure out what's happening there, but I think that you shouldn't just assume there is a section:

//Crash when he try to access to numberOfItems(inSection...)
    let ySize = CGFloat(Float(collectionView.numberOfItems(inSection: 0)) + 1.5) * (self.itemSize.height - offset);

You should first ask for number of sections.

kzaher avatar Jan 04 '17 22:01 kzaher

Hi @kzaher

i've found the problem. I set my layout before my binding. I call my setsubview (where the layout is declared) in the viewDidLoad and my binding (cell bind, dataSources bind ...) in the viewWillAppear. I fix it by declaring the binding before but i really don't like it. If i ask for number of section it will crash too :(

I found nothing in the different patchnote why this is not working anymore (cause it was working before the 3.0.1). Thank's for your answer.

YanisSOTO avatar Jan 05 '17 08:01 YanisSOTO

I had a datasource-bound tableview start crashing after updating to 3.1.0 as well, but switching it to a simpler .rx.items binding resolved the issue. I think there may be something to this, though perhaps the previous behavior was undesirable.

tomburns avatar Jan 05 '17 15:01 tomburns

Hi guys,

I would be happy to give this a look, but I would need more specific information and repro cases.

kzaher avatar Jan 06 '17 13:01 kzaher

Not sure if this is related but as it might be here is my problem and analysis of the issue.

Setup:

  • One TableView on each UICollectionViewCell
  • A Variable<[object]> of the cell's view model is bound to the TableView with array.asDriver().drive(tableView.rx.items(cellIdentifier: identifier))
  • Whenever the cell is prepared for reuse the DisposeBag is recreated to remove all bindings.
  • Once reused the new array is bound to the tableview

Issue: When the collection view changes sometimes a crash occurs on RxTableViewReactiveArrayDataSource.swift:92 when force unwrapping the itemsModel. This happens on reuse when iOS is requesting a visible cell during a UI update triggered by a bounds change.

Analysis: On reuse a new RxTableViewReactiveArrayDataSource will be created. When iOS requests the cell a new RxTableViewReactiveArrayDataSource is active that does not yet have an itemModels or is not of the same "size" as the old data source. UITableView has not received a call to reloadData() and therefore hasn't reevaluated the datasource.

While my issue is related to the reusing in a collection view cell, I suspect that updating the binding or disposing the current binding and continue using the tableview results in the same issue.

Possible fix: I'm not too familiar with the inner workings of rxcocoa, but I assume that in case of disposing the DataSource/breaking the binding the UITableView needs to be told about it. I haven't found any code that does this, but I might have just missed it. If it is indeed missing we should fix this.

WolfgangD avatar Jan 11 '17 16:01 WolfgangD

@WolfgangD Do you dispose disposeBag which associated with cell lifecycle or view controller lifecycle?

sergdort avatar Jan 11 '17 19:01 sergdort

@sergdort I dispose it independent of cell or view controller lifecycle. The reason being that CollectionView cells are reused. I dispose the disposeBag in func prepareForReuse() of my cell class. The UITableView of the cell being reused still continues to exist, it is just not shown on screen. Once the cell is reused, I set a new binding and iOS will show the cell's tableview again. Somewhere shortly after that the crash happens.

WolfgangD avatar Jan 11 '17 19:01 WolfgangD

Hi @WolfgangD ,

can you maybe create a small repro and publish it somewhere?

kzaher avatar Jan 11 '17 20:01 kzaher

I just got confused with

I'm not too familiar with the inner workings of rxcocoa, but I assume that in case of disposing the DataSource/breaking the binding the UITableView needs to be told about it. I haven't found any code that does this, but I might have just missed it. If it is indeed missing we should fix this.

What do you mean by

I assume that in case of disposing the DataSource

When do you dispose dataSource ?

sergdort avatar Jan 11 '17 20:01 sergdort

@kzaher unfortunately it is not easy extractable and the issue is quite hard to reproduce with this setup.

I might be able to reproduce it in a small sample project, but this will take a bit as I don't have much time at hand right now.

@sergdort

I guess the question I need to ask is. What happens if the binding is broken by disposing the DisposeBag? I assumed the old RxTableViewReactiveArrayDataSource will be disposed in this case. If this is the case the UITableView needs to be told about the changed data source.

WolfgangD avatar Jan 11 '17 21:01 WolfgangD

Hello, i've made this project to reproduce my bug with the same logic in each file. Hope it will be clear for you. When you clone pod are in 3.0.0, every thing is working. Switch to the last release(RxSwift) and the crash will appear. In the viewController there is my solution to resolve it in 3.0.1 but i really want to found an other.

project

Thank's for your time

YanisSOTO avatar Jan 12 '17 11:01 YanisSOTO

Hi @YanisSOTO ,

I've tried the example. As far as I can tell, there isn't any issue with the library, you were assuming there is always at least one section, which is not a valid assumption.

If you add

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        
        let offset: CGFloat = self.itemSize.height / 150 * 50;
        
        let newRect = CGRect(x: rect.origin.x, y: rect.origin.y + CGFloat(Int(rect.origin.y / 150)) * offset, width: rect.width, height: rect.height + CGFloat(Int(rect.height / 150)) * offset)

        if (self.collectionView?.numberOfSections ?? 0) < 1 { // <--- this is needed
            return []
        }
        
override var collectionViewContentSize: CGSize
    {
        guard let collectionView = self.collectionView else {return CGSize(width: 0,height: 0);}
        
        let offset: CGFloat = self.itemSize.height / 150 * 50;

        if collectionView.numberOfSections < 1 { // <--- this is needed
            return .zero
        }

everything will work fine.

iOS will still complain

2017-01-14 13:17:56.899 rxBug[54551:624920] The relevant UICollectionViewFlowLayout instance is <rxBug.SRCollectionViewStackLayout: 0x7f8a4400e510>, and it is attached to <UICollectionView: 0x7f8a43813600; frame = (20 20; 728 1004); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x60800004f2a0>; layer = <CALayer: 0x600000035760>; contentOffset: {208.5, 1738}; contentSize: {936, 2739}> collection view layout: <rxBug.SRCollectionViewStackLayout: 0x7f8a4400e510>.
2017-01-14 13:17:56.900 rxBug[54551:624920] Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.
2017-01-14 13:17:57.004 rxBug[54551:624920] The behavior of the UICollectionViewFlowLayout is not defined because:
2017-01-14 13:17:57.004 rxBug[54551:624920] the item width must be less than the width of the UICollectionView minus the section insets left and right values, minus the content insets left and right values.

... but this isn't related with this library. You are probably some invalid calculations in the code.

kzaher avatar Jan 14 '17 12:01 kzaher

Hey @kzaher, just want to let you know that I've made a sample project to reproduce the situation I explained above. However I wasn't able to get it to reproduce. Good thing is that I was able to disproved my analysis of the issue and I now think that it might not be an issue in RxSwift. I will try a few more things and open a new issue if I find anything related to RxSwift.

Thanks for RxSwift and keep up the good work.

WolfgangD avatar Jan 16 '17 13:01 WolfgangD