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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to have sections in FirebaseTableViewDataSource #8

Closed
kermankohli opened this issue Sep 12, 2015 · 17 comments
Closed

Ability to have sections in FirebaseTableViewDataSource #8

kermankohli opened this issue Sep 12, 2015 · 17 comments

Comments

@kermankohli
Copy link

It'd be really helpful if FirebaseTableViewDataSource.m allowed for UITableViewSections based on a specific variable. Basically something similar to NSFetchedResultsController used for Core Data 馃憤

@asciimike
Copy link
Contributor

@kermankohli Yep, this would definitely be useful. I put some thought into this earlier, and my original thought was to use separate refs, so you would create a FirebaseTableViewDataSource and then add a section to it, and a section would contain a ref (or more likely a query). The approach mentioned above could allow for just a single ref, assuming you wanted to filter the data into sections based on the content of a single field.

This might fit in well for our eventual support for better client side filtering, so definitely on the roadmap after auth. It requires an array of arrays to support sections, adding inSection:indexPath.section adding numberOfSections... as [metaArray count] and then updating the correct array every time an event is raised. PR's welcome if you wanted to try before we got to it :)

@kermankohli
Copy link
Author

@mcdonamp Alright, thanks for that! I'm not implementing Firebase right now but will be looking to move in about a month - do you reckon it'd be implemented by then?

@asciimike
Copy link
Contributor

Thanks to @pmodernme, it might be earlier: #13

@kermankohli
Copy link
Author

@mcdonamp looks pretty good, thanks @pmodernme! 馃憤

@pmodernme
Copy link

pmodernme commented Oct 3, 2015 via email

@kermankohli
Copy link
Author

Hey guys just had a question,

If the refQuery was users/john/tasks (which contains the IDs of all the users), how would filtering/sorting work in that case?

@pmodernme
Copy link

@kermankohli
As my sectioning fork is currently...

If you don't set the predicate, or the sort descriptors (set them to nil), it will sort the snapshots alphabetically by key.

If you set the sortDescriptors (this is an array of NSSortDescriptors), it will sort first by the zeroth sort descriptor, then the first, and so on. The sort descriptor queries the snapshot. So you could do:

@[[[NSSortDescriptor alloc] initWithKey:@"value.origin_date" ascending:NO]];
This will order by the origin_date child of the snapshot, with the largest value at the top.

or

@[[[NSSortDescriptor alloc] initWithKey:@"key" ascending:YES]];
This will order by the key of the snapshot (the last key path of the ref).

You have to use a key path like value.name to query the value.

Predicates work much in the same way. You set the predicate to an NSPredicate.

[NSPredicate predicateWithFormat:@"%K == %@", @"value.hidden", @NO];

Only snapshots with the child hidden with a value of FALSE will be shown.

I hope that helps!

@kermankohli
Copy link
Author

@pmodernme

Thanks for such a detailed write up! Just read back on what I wrote and I ended up confusing my self 馃憥

What I meant to say was if the refQuery was /users/john/tasks/ (which contains an array of tasks who's ids correspond to the task class) then how would sorting work, because the values themselves haven't been fetched.

Sorry for the confusion before!

@asciimike
Copy link
Contributor

What you're asking about is join functionality on top of the filtering, which we don't currently support. We're investigating something similar to what Firebase-Util offers in that respect, but, to be honest, doing all of that client side is really not something that we're interested in doing from a performance standpoint.

@kermankohli
Copy link
Author

Yeah it's kinda an edge case which might not be worth developing for. Would it be fine if I structured my data to look like /tasks/john/taskX rather /tasks/(tasks from /users/john/tasks/)?

@asciimike
Copy link
Contributor

It's a totally valid use case, it's just not something we support out of the box on the backend (being the type of NoSQL database we are). It's fine if you structure it that way, the issue is just that potentially pulling down all that task data and sorting it client side could be a pretty expensive operation.

Typically I recommend only pulling down the minimum amount of data required to sort/filter client side and then attach listeners on the relevant tasks/objects and grab those more full changes. Typically, for instance, you aren't filtering off the title or the description of the task (and those are relatively big), but you might be filtering off timestamps/tags, so keep the title/description in tasks/$taskID while putting other info in users/john/tasks/$taskID or similar.

@kermankohli
Copy link
Author

Right gotchya. In my case I need to sort/filter by 5 keys, so in that case I'll need to store all that information in users/john/tasks/$taskID?

@pmodernme
Copy link

Ah! I have something similar in my own app. You have to sort or fetch by
something in the snapshot.
In mine, I have a list of subscriptions with basically just a timestamp,
Boolean, and an id for the data it relates too. The predicate is based on
the Boolean, and the sort is based on the timestamp. Then I use another ref
to populate the cell.

@kermankohli
Copy link
Author

That makes sense! Doesn't it lead to duplication of data though? Or is this normal when you denormalize data (I'm new to this haha)?

@asciimike
Copy link
Contributor

Yes, it can, though this is more normal in a NoSQL database--disk space is cheap, your time is not. It's effectively creating indices rather than relying on the DB to do so. Plus, with multi-writes in Firebase, it's easy to keep these values in sync.

@asciimike
Copy link
Contributor

Closing because pull #13 deals with this

@cgossain
Copy link

cgossain commented Mar 27, 2018

I've built an NSFetchedResultsController type solution for the Firebase Realtime Database, and I've just open sourced it. I've been using this in a few of my production apps for some time now.

https://github.com/cgossain/FirebaseResultsController

I'd love to hear your thoughts.

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