Saving into a list in Firebase

2,404 views
Skip to first unread message

Ugur Turan

unread,
Sep 28, 2016, 6:54:51 AM9/28/16
to MIT App Inventor Forum

How do we use the "Firebase.AppendValue" block in ai2? I am getting errors. the list is in a json format. I used ABG's blocks as seen attached, but they do not add anything and give the error "invalid json object in database". 

What I'm trying to do is here: I want to keep a global list. the app will be used by 5 people and all will append data to it. but ı can't make a list in firebase. it saves ai2 lists as shown in firebaselisttry image. 


how can I make a list and append to it? 

Thanks.

Ghica

unread,
Sep 28, 2016, 8:58:13 AM9/28/16
to MIT App Inventor Forum
It is not necessary to create your own JSON when you want to store a list in Firebase. The Firebase component will do that for you. When you use the Firebase.AppendValue block, it does what it says it does, append the value to a list if it is a list. If you stay with AI apps for your Firebase, then this will work fine. In a previous of the Firebase component there was the problem of atomicity of updating something which is alleviated by adding the AppendValue block.
Cheers, Ghica.

Abraham Getzler

unread,
Sep 28, 2016, 2:40:58 PM9/28/16
to MIT App Inventor Forum
I agree with @Ghica.
Use the Firebase.AppendValue block.

My JSON conversion blocks are inappropriate for this purpose.

You can drive yourself mad trying to escape escape characters.

I had to adopt severe limitations on my Rock Paper Scissors design before the Firebase.AppendValue block came out.
Design methodology
To avoid conflicts from simultaneous updates; we will try to follow these rules on the server side 
never store summaries (counts, maxima, sums, analyses)
never update anything in place unless you own it and no one else reads it.
never keep lists, only subtags.
only insert.
Also, because a FirebaseDB query with a unique tag will return as a value the JSON of everything under that tag (but not the tag),  we will include a duplicate of the unique parent ID under the subtag ID, to make it accessible through the lookup-in-pairs block.


ABG

Ugur Turan

unread,
Sep 29, 2016, 12:47:20 PM9/29/16
to MIT App Inventor Forum


Ok. I tried using the append block, something is not right. I saved a list of lists first, then I used the append block to add another list to the original list of lists, so there are three lists in the list. but whenever I use append, the last appended list returns as a list but the previous ones are not lists anymore. Any ideas?

Abraham Getzler

unread,
Sep 29, 2016, 12:54:28 PM9/29/16
to MIT App Inventor Forum
I'm missing the block(s) that reload global try1 from FireBase.
Show them?

The FireBase database contents screen shot would help too.

ABG

Ugur Turan

unread,
Sep 29, 2016, 1:20:24 PM9/29/16
to mitappinv...@googlegroups.com

 believe I found a mistake here. when I save data for the first time, see the first characters:

then when I append to it:

here is the line copied directly from the database:

"[\"[0, ali, 1]\",\"[0, ali, 2]\",\"[0, ali, 3]\",[\"0\",\"ali\",\"4\"]]"

The last one appended is the part in red. Any ideas? 

[Edit] I found that if I save a simple list and append to it, all works fine. As an alternative, I'll keep my lists as strings split by [, and append them to the firebase list.

Abraham Getzler

unread,
Sep 29, 2016, 2:20:37 PM9/29/16
to MIT App Inventor Forum
Looking at the square brackets at the front,
it looks like the 2D list got turned into a 1D list.

The inner [ is wrapped in quotes.

Would need to see where that happened.

ABG 

Ugur Turan

unread,
Sep 29, 2016, 2:31:29 PM9/29/16
to MIT App Inventor Forum
well, it happens everytime I append to a list of lists. actually, I messed up one app due to database design errors, so now I'm trying to design the db from the start, so this is not an app yet, but a trial of saving options for the database. now it is clear that I can't save lists of lists in firebase. sad but true...

Ghica

unread,
Sep 29, 2016, 2:51:29 PM9/29/16
to MIT App Inventor Forum
I think that you should post a minimal .aia that shows the problem in a reproducible way, to give the AI developers a chance to correct it.
Cheers, Ghica.

Abraham Getzler

unread,
Sep 29, 2016, 2:53:22 PM9/29/16
to MIT App Inventor Forum
 I can't save lists of lists in firebase

How about saving lists of csv texts of sublists?

Alternatively, since your data should have nice column names,
use subtags (a/1/alpha/...) separated by slashes to store fields in rows,
like I did in my Rock Paper Scissors experiment?
 
(see other posts in this thread).

ABG

Abraham Getzler

unread,
Sep 29, 2016, 2:54:39 PM9/29/16
to MIT App Inventor Forum

wolberd

unread,
Oct 27, 2016, 7:52:00 PM10/27/16
to MIT App Inventor Forum
Hi. I found this post as I have a similar error and believe that Firebase append (or load) is not working correctly when you store a list of lists under a tag.

I'm storing my data with a single tag, the value is a list of lists (a list of notes, where a "note" is a list of three items containing the user name, note input, and date. When data comes back to me (getValue/gotValue or DataChanged) the subitems come back as text with brackets around them and commas in between, i.e., the sublist doesn't come back as a list. So I get an error when I try to select list item on the sublist.

My code is attached.

Dave
NoteTakerFirebaseAppendTimeUser2.aia

Abraham Getzler

unread,
Oct 27, 2016, 10:27:40 PM10/27/16
to MIT App Inventor Forum
Appending to 2D tables is pretty much a lost cause.

Save the 2D data as a 1D list of csv texts.

See attached.

ABG


blocks.png
NoteTakerFirebaseAppendTimeUser2_ABG.aia
2016-10-27 22_23_51-Genymotion for personal use - Custom Phone - 4.4.4 - API 19 - 768x1280 (768x1280.png

Abraham Getzler

unread,
Oct 27, 2016, 10:38:19 PM10/27/16
to MIT App Inventor Forum
Before you go lobbying for 2D append support in FireBase,
be sure to read the FireBase data design guide at

They likes their data flat and redundant.

ABG


Ugur Turan

unread,
Oct 28, 2016, 3:05:03 AM10/28/16
to MIT App Inventor Forum
The solution I found was to store the sub-list as text joined by a seperator. the append to list block works if it's just a list. you can do the same, join the data using "," and append the resulting text into a table.

wolberd

unread,
Oct 28, 2016, 1:03:18 PM10/28/16
to MIT App Inventor Forum
Abraham, thanks for the firebase article, that is helpful in terms of best practices on how to design data using firebase, and Ugur thanks for the explanation of using text as sublist-- I'll encourage my students to do that for their assignment

I would advocate, however, for fixing the Firebase component to follow the principle: whatever I put in, I get the same thing back. I don't thing a get/got or datachanged should return a sublist that was put in as a list as a string-- this is not what a programmer would expect and will lead to lots of frustrations. It is also inconsistent with TinyDB.

Thanks,
Dave
Reply all
Reply to author
Forward
0 new messages