Tuesday, March 13, 2018

Social Network with Firebase 2 - Making a Post

Ive already outlined my database structure for my Firebase based social network.  Ive done the auth for new users, and Ive added each new user to an array at the root of my JSON database called Users.  Now for the first stage of allowing the user to create data : making posts.

As the database structure shows, my posts will also live in a 'posts' array at the root of the database structure.  Within that array, each post will have a unique key, which by the way consists mainly of a timestamp, which allows them to be easily sorted by date.  Within each key will live the post data, which consists most importantly of : id of the user that made the post, the date + time it was made, the text body of the post, longitude / latitude at which the post was made, and other things.  The gist of this blog post here is to simply describe how Firebase makes it easy for my app to add to the database.

Ive created a UIViewController for entering the post and an accompanying .xib file to contain the layout of it.  I didnt do the layout for this within a storyboard.  For reasons why and to see how to set up a UIViewController with an .xib, see here.

So assume that you have an interface of some kind that allows the user to enter text for the post body, which in my case includes a post title and post body text.  My social network is location based, so I'm using iOS location services to get the current GPS position of the users device so I can save lat and long data to this post too.  Incidentally, for simplicity - my JSON upload consists almost entirely of text elements.

So back to the point - what I basically want to do is to add this post to my posts array in the database.  First thing I need is a unique id for the post, which will be its key in the JSON array in the database.  To do this, i use a method called 'childByAutoId'.



So the ref property I made simply refers to the root of the database. My posts live in an array with key "posts" which is why its on the second line there, and following that I get my unique key for a new element in that array. Now I simply need to upload some JSON to that ref, containing the properties I want to store.

To do this, first Ive got my own local class which will represent the post data. In my case its called GZPost. It has properties like this :


I initialize it with a custom initializer that takes the unique key I have just made, which will be the posts 'id' locally (and its key in the JSON structure in Firebase).


Within that class I have a function called 'updateDictionary'.  All this does is convert my properties into a Dictionary of type [String : Any], which will be uploaded to the Database.  The func looks like this :


So as you can see, it is simply taking its own properties and turning them into a Dictionary of the type that I can upload to Firebase. One of the beauties of Firebase is that - that dictionary can contain any properties I like, and I never need to go into Firebase itself and change anything at the backend. Whatever I send is stored automatically, which frees up so much time for the developer. At any point I can just add an array or a property wherever I choose in the structure of the Dictionary that I send.

Point to note here also - you can see it has an 'Author' property. This contains the id of the user who made the post, and in the case where its me making the post, thats obviously my id. I have a local class called GZUser which contains my id so I can get that from there, or I can also query Firebase for the current users' Id, like this :


The final method I have on this GZPost class is called 'referencePathString'. All this does is return the path to this item in Firebase. It takes the local 'postId' property and adds it to 'posts' which is the key for the posts array in the database. It looks like this :


So thats all the parts of the puzzle - now I just put them all together to upload my new post to Firebase. Using those methods specified above, the upload code looks like this :


To test this out, you can go to your Firebase Console, click on Database on the left, and just watch the new post data appear in your posts Array online.

Next I'll be outlining how to observe an array reference such as the posts one there and displaying the data in a UITableView.

No comments:

Post a Comment

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 ...