Jared
Jared

Reputation: 2217

Android Firebase Database Loading Faster

I am using the Firebase database for Android and am having some speed issues. When retrieving many custom objects it takes a long time (like 1/2 - 1 second for each). I am loading a bunch of Poll objects I have stored in FB.

Here is an example of how I am currently doing it for every poll:

pollRef = rootRef.child(getString(R.string.poll_ref)).child(myPollIds.get(polls.size()));

        pollRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (isAdded()) {
                    Poll poll = dataSnapshot.getValue(Poll.class);

I am trying now to load individually download specific parts of the poll that I need to use. It's getting pretty complex. Is there a better way to only get parts of objects from the database efficiently?

For example, I only want the highlighted parts of the object. enter image description here

Thanks in advance! :)

Upvotes: 1

Views: 1887

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317362

Firebase Realtime Database doesn't give you the ability to cherry-pick individual attributes like you're hoping. The most efficient way to get only a slice of data from a larger structure is to copy all the relevant bits into a whole different structure, and request that other structure instead. That sort of duplication is typical for an optimized NoSQL database - the data will typically be copied as many times as necessary to optimize for the particular queries that will be common in an app. The downside of this is when you update one of the copied values, you need to take care to copy it to all other location where it could live.

The name of this data organization strategy is called denormalization.

Upvotes: 5

Related Questions