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

Dealing with table updates #996

Closed
bobspryn opened this issue Dec 10, 2013 · 11 comments
Closed

Dealing with table updates #996

bobspryn opened this issue Dec 10, 2013 · 11 comments
Labels

Comments

@bobspryn
Copy link

I'm trying to think if there's a more RAC way to deal with table updates.

Basically I have a table that has 4 or 5 distinct states. As a user goes through building a question, they trigger a state update by doing something like clicking a button to advance, or clicking a pseudo back button to move back.

Now all the cells can observe the state on the viewModel and update themselves accordingly. Where it gets hair is all the rows/sections that are inserted/removed/reloaded (to update height).

If I didn't need to run beginUpdates and endUpdates with all the respective table changes in between, it would be pretty easy to filter and map the changes into some nice clean code, but those begin and end calls really throw a wrench in the the works.

So I end up with code like this:

[self.tableView beginUpdates];

if(viewState.integerValue == TCAskQuestionStateAPIBroad) {
    if(self.viewModel.previousAskQuestionViewState == TCAskQuestionStateTypeQuestion) {
        // reload the question row
        [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
        // insert the broad category rows
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:TableSectionAPICategories] withRowAnimation:UITableViewRowAnimationRight];
    } else {
        // coming the other way
    }
} else if (viewState.integerValue == TCAskQuestionStateAPIGranular) {

} else if (viewState.integerValue == TCAskQuestionStateAPISomething) {

}
//etc

[self.tableView endUpdates];

I thought about having a beginUpdatesSubject that mapReplaces into the view state signal and splitting lots of signals off of that, but then I have to also somehow observe for all those other signals to complete to call endUpdates. In the end it didn't end up looking much better than a nested if.

Thoughts?

@nikolaykasyanov
Copy link
Contributor

Why not wrap several table updates into some kind of batch? And when some view model signal sends next (a batch), call beginUpdate/endUpdate and perform all updates from batch.

I'd love to hear some best practices in managing table/collection views too.

@notxcain
Copy link

I'm in the process of crafting TableViewModel right now. Here is the implementation of change object. This objects are sent by change signal of view model.
https://gist.github.com/denis-mikhaylov/7889166

You need to create category on UITableView and implement QWSectionedList protocol. Then simply send applyToList:tableView.

@notxcain
Copy link

Here is a protocol (WIP) for table view model
https://gist.github.com/denis-mikhaylov/7889347

@bobspryn
Copy link
Author

@denis-mikhaylov Interesting strategy. I like the concept. Thanks! Curious if others have thoughts on this too.

@bobspryn
Copy link
Author

@denis-mikhaylov When you wrap that up, it would be awesome if you put together a quick sample app showing it off. Good learning tool.

@notxcain
Copy link

@sprynmr sure I will!

@kastiglione
Copy link
Member

@sprynmr Have a look at #1032.

@bobspryn
Copy link
Author

@denis-mikhaylov Back to this again. Looking at your list which is pretty cool. Curious if you made updates?

@notxcain
Copy link

@sprynmr Well. After the introduction of #1032 it became as trivial as observing collection of collections and animating corresponding changes. So I decided not to waste time on sectioned list.

@bobspryn
Copy link
Author

Makes sense. I'm just not using 3.0 yet.

@notxcain
Copy link

Me neither. Just implemented this using rac_valuesAndChanges:forKeypath:options:observer:

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

No branches or pull requests

5 participants