Empty Array

923 views
Skip to first unread message

Herman Chan

unread,
Dec 11, 2014, 5:00:53 PM12/11/14
to fireba...@googlegroups.com
Hi, 

It seems like Firebase doesn't work well with empty array.

I have a location like this: database/users/user_id/follows and inside follows is an array.  Now, when I set it, it seems to work fine and it will look like a dictionary just like how it was explain here: https://www.firebase.com/blog/2014-04-28-best-practices-arrays-in-firebase.html.  However, when I go onto the Firebase database browser and delete the node "followers", the entire tree under /database/users is gone.  The same will happen if I set "follows" to be an empty array in code.

The only way around this for now is for me to set an array with one element of empty string, is it a recommended workaround?

regards,
Herman

Jacob Wenger

unread,
Dec 11, 2014, 6:58:23 PM12/11/14
to fireba...@googlegroups.com
Hi Herman,

Nodes in Firebase only exist if they contain any data. The corollary to this is that you can write to any node in Firebase (no matter how deeply nested) on the fly, without having to worry about the ancestors existing. This is most definitely a feature, not a bug.

If you store something other than followers in the user node, such as a name, the node will not be deleted when the followers are removed. What is the actual use case you are trying to accomplish? Why do you need to store the empty string around? Once I have some more details, I can offer some more assistance.

Jacob

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/48663bf2-b831-4c15-bb41-b87281e293d7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Herman Chan

unread,
Dec 11, 2014, 7:01:47 PM12/11/14
to fireba...@googlegroups.com
Thanks for the reply Jacob.

I don't necessary have to set followers to an empty array.  I can just simply remove the node, which I try to do in code and in the data browser.  Once I did that, the whole try starting from "/users" is gone.

Herman 

Jacob Wenger

unread,
Dec 11, 2014, 7:11:39 PM12/11/14
to fireba...@googlegroups.com
Hi Herman,

Yes, this is expected behavior if that followers node was the only stored data. The Firebase viewer does not display nodes which do not have any data. So If your data looks like this:

{
  "a": {
    "b": {
      "c": "hello"
    }
  }
}

And you do rootRef.child("a").child("b").child("c").remove(), your data will not look like this:

{
}

Jacob

Zack Morris

unread,
Dec 11, 2014, 8:07:09 PM12/11/14
to fireba...@googlegroups.com
> On Dec 11, 2014, at 5:01 PM, Herman Chan <herm...@gmail.com> wrote:
>
> Thanks for the reply Jacob.
>
> I don't necessary have to set followers to an empty array. I can just simply remove the node, which I try to do in code and in the data browser. Once I did that, the whole try starting from "/users" is gone.

Just wanted to chime in, but I’ve found it useful to think of the tree structure as residing in the logic of the app rather than in Firebase itself. So it’s not so much that a node isn’t there if its contents are empty, but more like it’s representing empty data at that location. Parent nodes are dictionaries, and they disappear when they don’t have any children, so it’s not that surprising that an array would behave the same way.

If you want to prevent your app from crashing when it encounters an empty node, you can check for null and return an empty array:

Objective-C:

NSArray *snapshotValue = snapshot.value != [NSNull null] ? snapshot.value : @[];

Javascript:

var snapshotValue = snapshot.val() !== null ? snapshot.val() : [];

If you need to mark the location as existing even when it’s empty (so you can see its tree view in Dashboard), it might be better to store a placeholder like false there. Then you won’t have to worry about distinguishing between an empty array represented as [''] and an array that really is holding the single string ['']. Just check the type in your observer and have your code return an empty array if the node’s type is not array:

Objective-C:

[firebase setValue:array.count ? array : @(NO)];

NSArray *snapshotValue = [snapshot.value isKindOfClass:[NSArray class]] ? snapshot.value : @[]; // works for both null and false

Javascript:

firebase.set(array.length ? array : false);

var snapshotValue = Array.isArray(snapshot.val()) ? snapshot.val() : []; // works for both null and false. can also substitute: snapshot.val() instanceof Array

I hesitate to advocate this method since it stores extraneous data in Firebase, but maybe it could get you by while you are deciding how your hierarchy is structured.

Hope this helps,

Zack Morris

Herman Chan

unread,
Dec 11, 2014, 8:15:24 PM12/11/14
to fireba...@googlegroups.com
ah... ok..

so if I have

{
  "a": {
    "b": {
      "c": "hello"
      "d": "world"
    }
  }
}

 rootRef.child("a").child("b").child("c").remove() will work and "d" will still be intact?

herman

Jacob Wenger

unread,
Dec 11, 2014, 8:28:37 PM12/11/14
to fireba...@googlegroups.com
Yes, that is correct Herman.

And thanks for chiming in with another person's approach, Zach. The more the merrier!

Jacob

Reply all
Reply to author
Forward
0 new messages