-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
SnapshotParser Documentation and Performance #1037
Comments
@ahaverty thanks for the suggestion, we should improve the docs on The caching behavior is in It caches the result of the operation that turns a I think if you extended |
Thanks for the suggestion @samtstern Am I wrong in saying I'm restricted by the BaseSnapshotParser interface requiring a single type? Am I digging a hole if I try go further than that? We're now thinking of something like a big Everything.class, passing that to the snapshot parser and mapping to our subclasses from our bind view. It sounds like it's going to get messy, so I'd appreciate any more ideas 🙏 |
I think you could use |
That makes way more sense.. Thanks @samtstern I'll give it a shot. |
@ahaverty are all your models representing completely different data? If not, you can use a base class that contains the common fields and pass that around. Then use some sort of |
Thanks @SUPERCILEX We've structured it like that alright:
I was hoping there'd be no problem just checking the type and casting using generics. It worked, but it seems to have slowed down the recyclerview massively, especially with glide/images involved. @SUPERCILEX I'm not familiar with kotlin, is the same possible with Java? (If not, is it possible to mix in that kotlin with my existing stuff?) Thanks for the fast help 😃 |
It should totally work in Java, but it might be a little longer and uglier. 😄 Looking at your data, I would create a base model with a type and value field of type |
Thanks @SUPERCILEX I actually have my generics working nicely, using the I can't prove 100% yet, but scrolling appears to be silky smooth when using FirebaseUI options with a single class + your nice caching SnapshotParser's versus using a custom SnapshotParser without the caching. Making an educated guess, I'm thinking the two The single model generic seems to go all the way to the core of FirebaseUI, so I'm thinking of possibly taking @samtstern 's advice about implementing caching for multiple classes, but maybe doing it in my activity/onBindViewHolder for now. I've got datasnapshot.getKey(), so I can probably just create a new LruCache for each new model class. Would love to hear any ideas, thanks for the help! |
@ahaverty as long as you do the intensive processing in a |
@SUPERCILEX & @samtstern Got it working beautifully with your simple SnapshotParser ! (Medium post to follow once we get FirebaseFunctions + FirebaseUI working in production 😉) SnapshotParser<HomeFeedCell> snapshotParser = snapshot -> {
GenericTypeIndicator<HashMap<String, Object>> rawIndicator =
new GenericTypeIndicator<HashMap<String, Object>>() {
};
HashMap<String, Object> rawHashMap = snapshot.getValue(rawIndicator);
String type = (String) rawHashMap.get("type");
HomeFeedCell homeFeedCell = null;
switch (type) {
case "merchantOrdinary": {
GenericTypeIndicator<HomeFeedCell<MerchantCellValue, MerchantCellInteraction>> genericTypeIndicator =
new GenericTypeIndicator<HomeFeedCell<MerchantCellValue, MerchantCellInteraction>>() {
};
homeFeedCell = snapshot.getValue(genericTypeIndicator);
break;
}
case "advert": {
GenericTypeIndicator<HomeFeedCell<AdvertCellValue, AdvertCellInteraction>> genericTypeIndicator =
new GenericTypeIndicator<HomeFeedCell<AdvertCellValue, AdvertCellInteraction>>() {
};
homeFeedCell = snapshot.getValue(genericTypeIndicator);
break;
}
}
return homeFeedCell;
};
FirebaseRecyclerOptions<HomeFeedCell> options =
new FirebaseRecyclerOptions.Builder<HomeFeedCell>()
.setQuery(homeViewCellsReference, snapshotParser)
.build(); |
Sorry @samtstern , I prematurely closed this out of happiness. I forgot about the docs, I'll reopen, but consider my part ii) solved. Thanks again! |
@samtstern This issue can be closed since we pushed the changes to master 👍. |
Thanks @SUPERCILEX @ahaverty be sure to let us know when that medium post comes out! |
Hi Firebase,
I'm using the SnapshotParser to allow my onBindViewHolder to decide what model to map data to.
It took me awhile to figure out how to do this, as I wanted to use generics with various different models in a single recycler view (Harnessing the power of firebaseUI 🤘).
I found this answer from last year from @puf . But it looks like SnapshotParser is the new way to do this?
Two things:
.setQuery(myReference, MyModel.class)
? Are we losing out on any caching etc by using the SnapshotParser?Thank you!
The text was updated successfully, but these errors were encountered: