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

Binding an ObservableCollection to a Collection View #20

Closed
ramirez opened this issue Dec 13, 2015 · 13 comments
Closed

Binding an ObservableCollection to a Collection View #20

ramirez opened this issue Dec 13, 2015 · 13 comments

Comments

@ramirez
Copy link

ramirez commented Dec 13, 2015

Hi,

is there any more extensive example of binding an observableCollection of a collectionView? I'm porting an app I had in SwiftBond to ReactiveKit. I'm trying using the way explained here in the documentation and also using some inspiration from the SwiftBond demo app but I can't seem to be able to figure it out. I either get:

malloc: *** error for object 0x7fa13512a718: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug

from the ReactiveUIKit extension, or

*** 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 (29) must be equal to the number of items contained in that section before the update (29), plus or minus the number of items inserted or deleted from that section (29 inserted, 0 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).'

on the same code. Any ideas or hints?

Thx!

LEo

@srdanrasic
Copy link
Contributor

Hi @ramirez, hmm, that sounds bad. Could you post the code related to ObservableCollection and a Collection View and how are using it?

@ramirez
Copy link
Author

ramirez commented Dec 13, 2015

Hi,

my problem seems to be that I keep changing the contents of the collection I'm using, based on an observed value. I have a ViewModel that has:

var localPosts:ObservableCollection<[Post]> = ObservableCollection<[Post]>([])

and I observe a variable that triggers a repopulation of the collection (One suspect that I have is that I put that in Init. Maybe I should do that binding later):

init () {
selectedUser.observe { event in
            Account.listPosts()
                .observeNext {
                    value in
                    self.localPosts.removeAll()
                    self.localPosts.insertContentsOf(value, at: 0)
            }
        }
}

In my view I bind the observable collection to my collectionView:

func bindView() {
        viewModel.localPosts.bindTo(collectionView) { (indexPath, dataSource, tableView) -> UICollectionViewCell in
            let cell = self.collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! PostCollectionCellView
            let post = dataSource[indexPath.row]
            let cellViewModel = PostCollectionCellViewModel(post: post)
            cell.viewModel = cellViewModel
            post.updateFromBackend()
            return cell
            }.disposeIn(rBag)
    }

That's mostly it. The rest of the code is just layout.

The malloc error is reported in a line of
public class RKCollectionViewDataSource<C: ObservableCollectionType where C.Collection.Index == Int>: NSObject, UICollectionViewDataSource

in particular the init method:

public init(collection: C, collectionView: UICollectionView, animated: Bool = true, proxyDataSource: RKCollectionViewProxyDataSource?, createCell: (NSIndexPath, C.Collection, UICollectionView) -> UICollectionViewCell)

when calling: `uSelf.collectionView.performBatchUpdates``

Could it be that I'm modifying the collection before doing the binding to the collectionView a problem?
Cheers,

LEo

@srdanrasic
Copy link
Contributor

Hmm, we might have a bug in the binding...

What happens if you replace

self.localPosts.removeAll()
self.localPosts.insertContentsOf(value, at: 0)

with

self.localPosts.replace(value)

That basically does the same thing, just in one line.

I'll try to reproduce modifying the collection before doing the binding - but that should also work.

@ramirez
Copy link
Author

ramirez commented Dec 13, 2015

I tried but I get the same. The only way I found to make it work is by doing the binding before ever touching the collection.

@srdanrasic
Copy link
Contributor

Hi @ramirez, I've traced the issue to table/collection view binding. Just released ReactiveUIKit 1.0.9 with the issue fixed. Please confirm if it works. Thanks!

@ramirez
Copy link
Author

ramirez commented Dec 13, 2015

Works. Thx!

LEo

@retsohuang
Copy link

I use ReactiveUIKit 1.0.9 unfortunately it crashes after I update the data
Here's log

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert item 8 into section 0, but there are only 8 items in section 0 after the update'

And I use the same function self.localPosts.replace(value)

@srdanrasic
Copy link
Contributor

Looks like I have to spend some more time on this :) @retsohuang is it collection or table view?

@retsohuang
Copy link

That issue appears both CollectionView and TableView.

@srdanrasic
Copy link
Contributor

Hi @retsohuang, could you provide some more context around the issues? I'm having hard time reproducing this...

@retsohuang
Copy link

I'll try to create demo project that reproduce this issue.

@AnthonyMDev
Copy link
Contributor

This looks like its related to ReactiveKit/ReactiveUIKit#3.

Try removing the skip(1) as is described in that issues and see if that fixes the issue like it did for me.

@AnthonyMDev
Copy link
Contributor

I'm 99% sure that this is fixed by ReactiveKit/ReactiveUIKit#5. I'd love to get this pulled in ASAP @srdanrasic
Thanks!

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

No branches or pull requests

4 participants