Computed properties on Firebase

286 views
Skip to first unread message

Cory Imdieke

unread,
Feb 10, 2016, 12:41:45 PM2/10/16
to Firebase Google Group
I'm trying to figure out the best way to handle something like this.

I'm coming from Parse, but that doesn't really matter so much for this.

I've got a pretty simple User object which stores some basic metadata for a user account. Here's what I have so far:

class User {

        class func currentUserRef() -> Firebase? {

                let fb = Firebase(url: "https://firebaseIO.com")

                if let uid = fb.authData.uid {

                        // /users/<uid>

                        return fb.childByAppendingPath(FBUserPath).childByAppendingPath(uid)

                } else {

                        return nil

                }

        }

        // MARK: - Keys

        static let email = "email"

        static let firstName = "firstName"

        static let lastName = "lastName"

        static let profileImageData = "profileImageData"

}


All works pretty well. I can easily get a ref to the current user which is nice, and I have my keys defined so I don't mess up typing them later. But I also have one more property I want to access, which is "fullName" and that is the first name and the last name. Here is the Objective-C code from my old Parse model:

- (NSString *)fullName{

        if(self.firstName && self.lastName)

                return [NSString stringWithFormat:@"%@ %@", self.firstName, self.lastName];

        else if(self.firstName && !self.lastName)

                return self.firstName;

        else

                return self.lastName;

}


Not super complex of course, but there's really no way of doing this on Firebase because there aren't any subclasses. How have you guys done this sort of thing?

I guess I could create a class function which calculates the full name from the first and last name, but that's not very elegant. I could create a class function which takes a snapshot and returns the full name which is a bit more elegant, but not perfect. Any great solutions that I haven't thought of?

Jay

unread,
Feb 11, 2016, 6:13:55 PM2/11/16
to Firebase Google Group
Why don't you take the fullName function as posted and make it part of the User class?

That concatenation seems to be what you are doing already and it does what you are after.

Our User class handles everything dealing with a user; from creating it and writing it to Firebase to timestamping when changes occur to reading it in. No need for additional subclasses unless you want to extend your original User class further.

Or... am I missing something?


Cory Imdieke

unread,
Feb 17, 2016, 4:39:37 PM2/17/16
to Firebase Google Group
Sorry I didn't see your response earlier, I must have missed it.

Let's pretend this is for an arbitrary data class. Like a Person, and they aren't logging in. How would you handle this style of computed property on data that doesn't already exist as a Firebase class?

Jay

unread,
Feb 17, 2016, 6:16:31 PM2/17/16
to Firebase Google Group
I assume that computed property means concatenating the firstName + lastName?

Firebase doesn't have classes like Parse did. With Parse you could subclass a parse object and extend it.

With Firebase you really have just one thing to interact with data, a snapshot. The snapshot can contain a single node of data or multiple nodes.

You really need to just roll your own classes and add Firebase functionality to them - that will enable you to read and write data and have each class be responsible for managing itself.

So let me give a really super long winded example that may or may not provide and answer.


Snapshots are pretty cool and contain a key, and a value. The key is the node name and the value can be thought of and worked with like an NSDictionary.

We have a C class called UserClass with some private properties

UserClass
   firstName
   lastName
   fbKey


and public methods

initFromSnapshot
getDisplayName


Suppose I want to read in my users to populate a tableView.

I observe my users node in Firebase with childAdded. Here's the structure

users
   user_id_0
        firstName
: David
        lastName
: Bowie
  user_id_1
        firstName
: Ziggy
        lastName
: Stardust


The childAdded call will cause Firebase to iterate and return each user in my users node one at a time as a snapshot. It will also respond to future childAdded events and notify my app in case any users are added.

As each user is returned as snapshot, i do this

UserClass user = [UserClass new]
[user initWithSnapshot:snapShot]

each user is then stuffed into an array.

the initiWithSnapshot method takes the data from the snapshot, and stores the key:value pairs: firstName and lastName in the firstName and lastName private properties.

I also take the snapshot.key and store it in the fbKey property so I can update that node later, say if the user name needs to be changed.

So now when I want to populate my tableView, I iterate over my users array, reading each user object for each row in the tableView.

The tableView views (or cells) are populated with the string that is returned from my userClass->getDisplayName method.

return lastName + ", " + firstName //pseudo code

So now all of the users are listed by last name.

A couple of things I didn't mention:

Also within our userClass I have an method called  updateFirebaseUser.  If I need to update the user name, I do that in the class and the class knows to then write that data out to the FireBase node stored in fbKey.

Another neat thing is that if a firstName and lastName is updated in the class, and fbKey is nil, the class knows its a new user and automatically creates it in firebase.

Of course, there are a 1000 different ways to go about these steps but hopefully this helps.

Kato Richardson

unread,
Feb 17, 2016, 6:36:58 PM2/17/16
to Firebase Google Group
Jay,

Great answer! Thanks for being involved in the community and making it awesome.

I'd also note that, while it doesn't directly answer the question posed here, some of the FirebaseUI-iOS examples can be very helpful in understanding how to ferry data between Firebase and iOS controllers/views, particularly for dealing with Table/Collection views.

☼, Kato

--
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/b22cd91b-4d30-4328-a8c4-6b32a90ac624%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages