Saturday, August 13, 2016

Supper cool way to store Array to Firebase

https://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html
Great article, I was really confused at the beginning. However, the magic happens.

I store a JSON file to firebase,

{
"guest": {
"randomGidForGuestOne":{
"gid": "randomGidForGuestOne",
"guestAndPlusOneNumber": "1",
"guestAndPlusOneList":{"0": "randomGidForGuestOne","1":"randomGidForGuestTwo"},
"gImage":"gs://fir-databasepra.appspot.com/guestImage/randomGidForGuestOne.png"
},
"randomGidForGuestTwo":{
"gid": "randomGidForGuestTwo",
"guestAndPlusOneNumber": "0",
"guestAndPlusOneList":{"0": "randomGidForGuestTwo"}
}
}
}

and got back a dictionary with array in snapshot.  So it looks like this


{
"guest": {
"randomGidForGuestOne":{
"gid": "randomGidForGuestOne",
"guestAndPlusOneNumber": "1",
"guestAndPlusOneList": ["randomGidForGuestOne","randomGidForGuestTwo"],
"gImage":"gs://fir-databasepra.appspot.com/guestImage/randomGidForGuestOne.png"
},
"randomGidForGuestTwo":{
"gid": "randomGidForGuestTwo",
"guestAndPlusOneNumber": "0",
"guestAndPlusOneList":{"0": "randomGidForGuestTwo"}
}
}
}


Supper cool!!! Detail please see github.

https://github.com/lingzt/FirebaseDatabasePra/commit/e282cac346e747b22a669d26d000bb7e84caecb3 


No comments:

Post a Comment