Im currently tasked with building a social network using Firebase as a back-end. Im pleasantly surprised by how usable and generally liberating Firebase is, allowing a solitary developer like me to create both front and back-ends without too much hassle. I dont want to create a full tutorial here on creating a social network with Firebase. But I can create an overview of the process with some code snippets here for anyone else looking to undertake something like this.
First of all Ive sketched out (literally) my database structure for the Firebase back-end. Ive copied what they did with their FriendlyPix app, which is an excellent example, but lacking some more thorough documentation. Ive already written an explanation of that database structure in an article here. Thats very close to the structure Ive got with a few additions for properties they dont have.
Ive also outlined how they do Auth in that article. For my app Ive set up Firebase via Cocoapods, and Ive done the basic set up on the Firebase Console.
For authorization, I have a RegisterViewController with UITextField's for username, password (plus re-type), and also first name and surname fields. When my user clicks to register, this happens :
Four fairly simple steps there. First call to create the user using FirebaseAuth. If that works, we get a User (Firebase type) object back, which contains amongst other things, our 'uid', which is the unique identifier for that user.
Second, we do a change request to add the displayName property of that user into Firebase Auth. Third, we go to our Firebase database and we populate a whole new entity into the People database. The key for this entity, is the unique uid that we got back from registering the user. The values are a URL to the photo (more in future on this), the full name (made from whatever the user entered in the first name / surname fields), and a search index with a reversed version of the name for quicker searching.
Lastly, that user is signed in, which will give the app access to the data it needs to see. This signing in is being observed in other locations in the app, like this :
So the signing in that happened at the end of the registration code, once complete, causes this listener to fire, as it notice the sign-in success. The Firebase User object is passed in here so again we can use the 'uid' property. At this point I set up an observer to go and retrieve that data that I just wrote to the database in the People array.
So in the method above, I firstly remove any previous observers that I may have created, in an array of type [DatabaseQuery]. This ensures Im not making Firebase do more work than it needs to, and slowing the app down in the process. Then I create an observer to have a look at the database and let me know first what is in the database at the specified location (which is - in the people array, in a key which is my users uid), and to then update me should whatever in there change.
The result of that call returns 'snapshot', which is another Firebase type. Within that object is a property called 'value', which can contain an NSDictionary, NSArray, NSNumber or NSString. To handle the contents there, I have an initialiser for my local user object, which is called GZUser.
So quite simply, I have just taken back the data that I wrote during the registration process, that Firebase stored in its JSON format. I have taken that back as a dictionary and I have initialised my local GZUser object with that dictionary (that was packaged in a Firebase snapshot).
Finally that data is used to populate the displayName field in my UI. But that observer will carry on watching the space for changes, until I remove that observer. Next up I have a look at uploading data to the Firebase Database, in this post here.
Subscribe to:
Post Comments (Atom)
Social Network with Firebase 3 - Infinitely scrolling UITableView of Firebase Items
Ive already talked about the basics of a Firebase social network project , and adding posts in two other articles. Here I want to explain ...
-
Ive already outlined my database structure for my Firebase based social network. Ive done the auth for new users , and Ive added each new ...
-
Im doing an awful lot of work at the moment with AVMutableVideoCompositions, and AVAssetExportSessions. When exporting a video using the la...
No comments:
Post a Comment