Skip to content

How to upload data

Erick Navarro edited this page Aug 30, 2017 · 1 revision

As is explained before you can have the data structure you want, the library will query whatever POJO on any node you have uploaded to. I do can give you a couple of recommendations.

The simple way

This is straight forward, but is not recommended. Your data structure could be like this

{
    "locations": {
        "wadknawoidjaw9d": {
            "key": "wadknawoidjaw9d",
            "name": "Some turistic atraction",
            "latitude": -33.429072,
            "longitude": -70.603748
        },
        "w9sjaw0jd": {
            "key": "w9sjaw0jd",
            "name": "Maybe some company office",
            "latitude": -33.439072,
            "longitude": -70.613748
        },
        "wdnaw09djwdjawpodm": {
            "key": "wdnaw09djwdjawpodm",
            "name": "Some delivery point",
            "latitude": -33.439072,
            "longitude": -70.593748
        }
    }
}

For uploading data to that you have to simply:

DatabaseReference root = FirebaseDatabase.getInstance().getReference();
DatabaseReference locations = root.child("locations");
//Lets assume you have your model ready and you only need the key
String key = locations.push().getKey();
yourModel.setKey(key);
locations.child(key).setValue(yourModel);

Later you will be able to query thoose values because you can pass a second String ass empty in the query.

The problem here is, if you have too much data, a query like that could be slow. This questions in SO have more about it.

Indexing data

This is the way I recommend you to upload your data. A key concept when working with Firebase Database is denormalization. This means that duplicating data to make better queries is something you want to do. Another key concepto is indexing the data, wich means there is a special node with a simple list to get the reference to get the data, this is commonly known as a look up table.

In one node, you will have a simplified version, in another a complete version, and you can have many other nodes with list of references.

{
    "places": {
        "cl": {
            "wadknawoidjaw9d": {
                "category": "category1",
                "name": "center included",
                "latitude": -33.429072,
                "longitude": -70.603748
            },
            "wdnaw09djwdjawpodm":{
                "category": "category2",
                "name": "lat minus long plus included",
                "latitude": -33.439072,
                "longitude": -70.593748
            },
            "oiwueowuepowuepowue":{
                "category": "category4",
                "name": "not mentionable minus 0.04",
                "latitude": -33.469072,
                "longitude": -70.633748
            }
        }
    },    
    "locations": {
        "cl":{
            "wadknawoidjaw9d": {
                "key": "wadknawoidjaw9d",
                "name": "center included",
                "latitude": -33.429072,
                "longitude": -70.603748
            },
            "wdnaw09djwdjawpodm": {
                "key": "wdnaw09djwdjawpodm",
                "name": "lat minus long plus included",
                "latitude": -33.439072,
                "longitude": -70.593748
            },
            "oiwueowuepowuepowue": {
                "key": "oiwueowuepowuepowue",
                "name": "not mentionable minus 0.04",
                "latitude": -33.469072,
                "longitude": -70.633748
            }
        }
    },
    "places_category": {
        "cl": {
            "wadknawoidjaw9d": "category1",
            "wdnaw09djwdjawpodm": "category2",
            "oiwueowuepowuepowue": "category4"
        }
    },
    "favorites": {
        "user_uid_1": {
            "cl": {
                "wadknawoidjaw9d": "true",
                "w9sjaw0jd": "true"
            }
        }
    }
}

In this case the node places have the full object, it's only have 1 extra attribute (the category), in comparison to the locations node. For your application it could be more, photos, descriptions, rating, etc. The benefit of the reduce locations node is to improve data transfer for the user. There are other 2 extra nodes, places_category and favorites. Both have a list of references. With this structure if you want to show the user a RecyclerView with their favorites or with the places of some category, using a FirebaseIndexRecyclerAdapter is very simple. Or adding every locations near by to a Google Map. In both cases when the user interact with the object, you have the key, so you can directly get the full object to show the user the details.

To upload data to this structure, you need to do a multiple set value at the same time. This method is called updateChildren() and use a Map<String, Object> to do it so. So, whenever a user creates a place, you have to upload a simplified reference to the locations node and the full reference to the places node.

YourModel reduced = new YourModel();
reduced.setLatitude(latitude);
reduced.setLongitude(longitude);
reduced.setName(name);
reduced.setKey(key);

YourModel place = new YourModel();
place.setLatitude(latitude);
place.setLongitude(longitude);
place.setName(name);
place.setCategory(category);
place.setKey(key);

DatabaseReference root = FirebaseDatabase.getInstance().getReference();
Map<String, Object> map = new HashMap<>();
map.put("locations/"+countryIso+"/"+key, reduced);
map.put("places/"+countryIso+"/"+key, place);
map.put("places_category/"+countryIso+"/"+key, category);

root.updateChildren(map);

After this you have to capture other user interactions, such as creating a favorite and upload it to that node.

The above examples have the key included, because when the user interact with something that represent the object (a marker in the map, by example), then you can use the key to reach the complete version of the uploaded data. Remember, the full version of your object, can have a lot of attributes, to provide a lighter data transfer to the user a simplified version lead to a complete heavier node.

You can read more about indexing and denormalization in the Firebase Documentation and in this great series of videos by David East

Mind Blown

Clone this wiki locally