Search code examples
iosarraysswiftfirebase-realtime-databasechildren

How to build structured realtime firebase database for iOS, swift?


I am currently setting up my realtime firebase database to my iOS application. It is my first time trying to structure user data in a firebase database, and I am really, really struggling with understanding a few key things.

A bit of context to my application's database needs:

When a new user is created, the attributes assigned directly to the user are:

  • Age
  • Email
  • Username
  • Nationality

Later on, the user needs the option of creating personal diaries! Each of these diaries being arrays/lists of objects... Where each object in a diary furthermore holds a few attributes in a list/array.

After reading everything I could find anywhere, I picture my database something like this: Database Structure

I am terribly sorry if it becomes too specific - I will try to make the question as open as possible:

My question is:

How to create the different "children" programmatically and how to find the keys leading back to them, so I can refer to them at other times again? (when editing an attribute in a child).

A few methods I have tried:

  • setValue([ArrayOfObjects]) --> This creates the desired array, but I can't seem to find e.g. index 3 in this array, to allow user to change his/her email later on.

  • childByAutoID() --> This as well creates my array, but gives several other problems: User can only store one diary, still can't find the paths to specific indexes...

  • setValue(), andPriority() --> Can't seem to make the priority function. (Is this function also outdated??)

  • And a few more...

If anyone can tell me how to achieve just the first few steps in setting up my database structure, I will be forever grateful - I have spent literally all day on it and I am not moving forward ...

Or, at least tell me, if I am on the right track regarding my desired setup of the database. Is it flat enough? Is there a smarter way to store all these user created lists?

Thank you so much! :-)


Solution

  • I don't know Swift so my examples are in Dart but the methods are similar I believe.

    First off, I would split the Users node into two. One to hold the user data, which is normally pretty static, and the other to hold the diaries. You would use the same uid key as reference to both. This way you have less nesting to worry about and therefore it is much easier to CRUD the data. If you are using Firebase to authenticate your users then I would use the unique key that Firebase creates for each user as the keys for these two nodes.

    Then...

    To create a user data node record the Dart code would be something like:

    referenceUserData.child(<authenticated user id>).set({
      "age": <age value>,
      "email": <email value>,
      "name": <name value>,
    });
    

    To create a user diary node object record the Dart code would be something like:

    referenceUserData.child(<authenticated user id>).child(<diary key>).child(<diary object key>).set({
      "object info value 1": <object value>,
      "object info value 2": <object value>,
      "object info value 3": <object value>,
    });
    

    You could also create all the object records at once by writing them as a List (array) using .set().

    You also need to decide what your diary key should be. You could use Firebase to generate a unique key by using .push().set().

    To read eg. the user data then your call could be:

    referenceUserData
      .child(<authenticated user id>)
      .once()
      .then(
    (DataSnapshot snapshot) {
      print(snapshot.key);
      if (snapshot.value != null) {
        print(snapshot.value);
        <code to process your snapshot value>
      }
    };
    

    BTW, 'priority' is legacy from the early days of Firebase RTDB so I wouldn't try to use it.