Home Ask Login Register

Developers Planet

Your answer is one click away!

bryken February 2016

Using Firebase (iOS-Swift) for user's Read/Write data storage

I'd like to start using Firebase as my main BaaS now that Parse is closing. At the moment, Firebase's real-time capabilities are interesting and I see using them in the future, but for now I'd primarily like to use Firebase as a way to store a User and there associated data. With Parse, this was super easy, which required only uploading an object with the associated user attached to the object. I could then filter by the objects which were associated with that user.

However, in Firebase everything is Event driven as opposed to on-demand and you can't filter by security rules. In general, I like this approach, but I'm having trouble applying it to what I need today. I don't have much of background in networking/databases, so its possible some of this stuff is really basic.

Here is what I'm looking to do:

My Firebase data structure looks like this currently:

xnterval
   users
      100b920f-d2c5-4578-9f49-ecf38ef71302
      32a8dbf2-009a-44c5-a188-ca2c280ba135
      d53b0b37-d773-4cf5-af94-74635fc76f1f
         name: "Joe Schmo"
         provider: "password"
         username: "jschmo"
         workouts
            -K9wZ4_Rt-xBdVZt93d4
                workout_id: "-K9wZ4_Rt-xBdVZt93d3"
   workouts
      -K9wZ4_Rt-xBdVZt93d3
            interval_period: 180
            intervals: 2
            name: "Workout"
            owner: "d53b0b37-d773-4cf5-af94-74635fc76f1f"
            rest_period: 150
            work_period: 30

enter image description here

Inside the User/"uid" I have a workouts list which is order by childByAutoId and contains the ID of a single workout.

What I want to be able to do, is get a list of all the user's workouts and then retrieve that complete workout list from the workouts section.

I know the PATH directly to the users workouts root/"uid"/workou

Answers


Jay February 2016

A super quick solution: add the uid of the user to the workout_id node.

Then you can query on the workouts node for uid equal to the user of whose workouts you want to retrieve.

workouts
   workout_id_0
     interval_period: 180
     internvals: 2
     uid: user_id
   workout_id_1
     interval_period: 180
     internvals: 2
     uid: user_id

A second option:

If you know the path to the user workouts

/users/uid_id/workouts

you could observe the node by value, which would read in all of the children in that node into a snapshot. Then iterate over the snapshot to get specific data. I personally would load each item in the snapshot into an array of dictionaries or maybe even workout object.

Post Status

Asked in February 2016
Viewed 3,691 times
Voted 6
Answered 1 times

Search




Leave an answer


Quote of the day: live life