Save & Read Data from Firebase - IOS Swift 4

OK guys, So This time we are going to talk about Firebase.

# <b>What is Firebase? </b>

Base on Google Official Document ,Firebase is <b><i> Google's mobile platform that helps you quickly develop high-quality apps and grow your business. </i></b> But to make it simple you can just think about firebase as a Service provider. It provides many service like , Realtime database , Authentication , Hosting , Cloud Storage ... etc. and this time we are going to use firebase database to store or save and fetch data from our application.

#<b>Getting Started<b>

###<b>1. Create Xcode Project<b>
You have to set up your Xcode Project with Firebase in order to make it work. So first of all , let's us create a Xcode Project. I create a project and name it as FirebaseSampleProject.<img width="322" alt="Screen Shot 2018-05-24 at 1.41.26 PM.png" src="https://qiita-image-store.s3.amazonaws.com/0/211896/1787d27e-8890-cba6-d194-f4320df9c794.png">
after that please click on the project and copy the <b>Bundle Identifier</b>.<img width="1143" alt="Screen Shot 2018-05-24 at 2.00.55 PM.png" src="https://qiita-image-store.s3.amazonaws.com/0/211896/cdf744b7-040f-1e65-da10-da75395b60df.png">

Good job. Now let's go to create firebase project.

###<b>2. Create Firebase Project<b>
Now we finish create our xcode project so now it's time to play with firebase , now let's go to Firebase official website. (below is the link)
https://firebase.google.com

<img width="1539" alt="Screen Shot 2018-05-24 at 1.47.38 PM.png" src="https://qiita-image-store.s3.amazonaws.com/0/211896/285d6800-fdcf-1d3c-f03a-e0b722285c23.png">

at this point I think everybody already has a google account. If not please create and log in. Then click on <b> GET STARTED</b> or  <b>GO TO CONSOLE</b>.

<img width="1539" alt="Screen Shot 2018-05-24 at 1.51.49 PM.png" src="https://qiita-image-store.s3.amazonaws.com/0/211896/5728eab1-d8b2-c6f9-e774-c5f4103b182f.png">

after you see this page , you can create a project by clicking on <b>Add Project<img width="1201" alt="Screen Shot 2018-05-24 at 1.53.40 PM.png" src="https://qiita-image-store.s3.amazonaws.com/0/211896/264c7da2-1ddf-9662-ed98-517bcbdc1751.png">
</b>

fill everything and click <b> CREATE PROJECT </b>. It will take a little time to set up our project. After it finish setting up please click continue then it will route you to our project (please see below picture).<img width="1474" alt="Screen Shot 2018-05-24 at 1.55.57 PM.png" src="https://qiita-image-store.s3.amazonaws.com/0/211896/d922c487-2d82-aa4c-9468-d0f1242bc5e0.png">

To add firebase to our IOS app click on <b>Add Firebase to your IOS app<img width="763" alt="Screen Shot 2018-05-24 at 1.57.52 PM.png" src="https://qiita-image-store.s3.amazonaws.com/0/211896/5563e1ac-0f4a-4d52-f3aa-898f8ad96f09.png">
</b>
now paste the Bundle Identifier that we copy from our xcode project in the IOS bundle ID field and click register<img width="705" alt="Screen Shot 2018-05-24 at 3.04.19 PM.png" src="https://qiita-image-store.s3.amazonaws.com/0/211896/6e6abea7-7b65-fc4c-c9ac-13859dec7740.png">after that we have to download the GoogleService-info.plist and drag it into our Xcode project. after that click Next<img width="899" alt="Screen Shot 2018-05-24 at 3.05.45 PM.png" src="https://qiita-image-store.s3.amazonaws.com/0/211896/632640d4-a395-f8b2-69e0-9ec77fbb9324.png">

at this stage we have to use CocoaPod to install Firebase library. you have to include two pod file which are pod "Firebase/Core" and pod "Firebase/Database" see below picture.

<img width="616" alt="Screen Shot 2018-05-24 at 3.08.33 PM.png" src="https://qiita-image-store.s3.amazonaws.com/0/211896/0b7204ef-e9c1-fb4a-7ced-5bb8b5d99223.png">
after that click next and you will see below screen.
<img width="832" alt="Screen Shot 2018-05-24 at 3.10.15 PM.png" src="https://qiita-image-store.s3.amazonaws.com/0/211896/029136c1-aadf-699a-1a4f-78811271489f.png">

 
ok at this stage we have to go back to our xcode project but since we are using cocoapod so we have to use workspace instead. (see below picture)<img width="742" alt="Screen Shot 2018-05-24 at 3.13.21 PM.png" src="https://qiita-image-store.s3.amazonaws.com/0/211896/adf87d9b-95cc-e516-d963-2784e5425320.png">

after open the project. Go to <b> AppDelegate.swift </b> Import Firebase and add below code.<img width="753" alt="Screen Shot 2018-05-24 at 3.14.45 PM.png" src="https://qiita-image-store.s3.amazonaws.com/0/211896/24cfabcb-da6f-466b-618b-539eea30c0bc.png">

after that run our xcode project. if you follow all the steps, I am sure there will be no problem.

now let go back to firebase website and click on Develop -> Database

<img width="1530" alt="Screen Shot 2018-05-24 at 3.17.22 PM.png" src="https://qiita-image-store.s3.amazonaws.com/0/211896/5d9fbcd5-01b9-694f-b40c-27bd0ed8c847.png">

click on <b>Realtime Database</b> and choose <b> start in locked mode.</b>
<img width="849" alt="Screen Shot 2018-05-24 at 3.18.55 PM.png" src="https://qiita-image-store.s3.amazonaws.com/0/211896/8180c677-01ac-6cd6-428a-995499663eb1.png">

now click on <b>RULES</b> tap and change read , write to true. Don't worry about it since we are just doing a testing. changing it to true so it doesn't need authentication when we do the request from our application.

<img width="913" alt="Screen Shot 2018-05-24 at 3.20.33 PM.png" src="https://qiita-image-store.s3.amazonaws.com/0/211896/863e0d16-87be-4c6b-e806-62cd819eb261.png">

save. Now we have finished setting up.



###<b>3. Save Data into Firebase</<img width="758" alt="Screen Shot 2018-05-24 at 3.28.58 PM.png" src="https://qiita-image-store.s3.amazonaws.com/0/211896/e8d4e900-7c6b-a15f-686a-bd9373964111.png">
</b>

Now open our ViewController and write below code

<img width="608" alt="Screen Shot 2018-05-24 at 3.24.26 PM.png" src="https://qiita-image-store.s3.amazonaws.com/0/211896/2576eed6-3090-a9d6-2ca5-ab6529e55b70.png">

+ first We create the instance of FirebaseDatabase by giving databaseRefer = Database.database().reference()
+ then we are saving the data into our firebase database by using child & childByAutoId & setValue.
    -  Child : the field name
    - childByAutoId : automatically assign the id when created
    - setValue : value of the field name

run the code below and go back to firebase database. you will see those value in it.

###<b>4. Read Data from Firebase</b>

add below code to our ViewController
<img width="758" alt="Screen Shot 2018-05-24 at 3.28.58 PM.png" src="https://qiita-image-store.s3.amazonaws.com/0/211896/b85ff808-fc5b-636a-f879-981b612a06e2.png">

so to read data from firebase we are using DatabaseHandle object to handle the reference from databaseReference. The value will be printing out to the console.

YES! THAT'S IT WE ARE FINISHED!