> 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