I'm searching for a clean way to retrieve (and sometimes save) data from Firebase in Swift. It's annoying me that all my database calls are written in the middle of the view controller code. So I'm looking for some kind of custom data service class. I found this tutorial that's close to what I want: http://www.mobilecyberpunks.com/?p=82.
They promised a Part II but I cannot find this second part, so I guess this was never made. In this second part they promised to cover retrieving and saving data with this custom data service (which is the most important part of the whole thing for me).
I'm thinking of an API class (like in the tutorial) and when I'm retrieving data, and it finishes retrieving from firebase, I save it in a data set in this api class. Then I will posting a notification with Notification Center. But I'm am not sure whether this is best practice or a good way to do this.
Has anyone an idea how to do this (finishing this tutorial I found or in another way)?
Thanks in advance!
I started to use this solution and polished it a little bit, and I came to a pretty handy solution.
I created a custom class named FirebaseAPI. This is a singleton class. This class contains all the methods for Firebase (Authentication, Database, Storage, ...).
Example:
FirebaseAPI.swift
Constants.swift
AppDelegate.swift
I can give you a example of calling methods in the FirebaseAPI in a ViewController, but an example of such a method is given in the code of the AppDelegate.swift up here (the FirebaseAPI.shared.logInUser method).
Used this structure in 3 different projects up till now and it works fluently!
Making a custom Class for the communicating is generally a good idea if you need extensive function's and make numerous calls to your server.
The two preferred methods for this are:-
Protocol-Delegate
Method_completionBlocks:
Below answer contains both.
Custom Class
Your ViewController