Vous êtes sur la page 1sur 9

Documentation for FireBaseRealTimeDataBase:

saving single user details and list of user details:

– Create and setup project with screens and navigation and outlets and
button functions

– open firebase link : https://firebase.google.com/?authuser=0

– click on docs and then click on get started for iOS. it will navigate to guide
section from there select realtime database and after that click on get
started
– open firebase console link : https://console.firebase.google.com/

– if you didnʼt login first login into firebase console and then create a project
( provide - project name, country)
– After successfully created project overview window will appear

– in project overview screen click on ‘ Add firebase to your iOS app ‘ option

– provide your project details and follow the all steps are listed there

– After you successfully followed all steps firebase will successfully added to
your project project overview window will appear with your project name

– now click on Database option to modify realtime database rules ( read,


written)

– in Database there is one section that createDatabase click on that


– now select ‘ start in test mode ‘ and click on enable

– now data base window will appear real time data base if user add any data
it will reflect here we can monitor that user data from this window we can
modify the rules

– now open terminal and install the realtime data base framework into your
project
(https://firebase.google.com/docs/database/ios/start?authuser=0)

– After you follow the all steps from that top link click on structure data,
structured data section will explain you how to structure a user details
before you save into firebase realtime database

– After that click on Read and Write data ( in this section we will get full code
to save data and retrieve saved data and update and delete data from
firebase

– 1) code for save or inserting data into firebase


*******************
self.ref.child("users").child(user.uid).setValue(["username": username]) //
here users is the name given to that database table view we can change it
as per our requirement
// " username “ is key and username is value

. here user.uid is userUnique id if you mention a unique id it means you are


saving single user data

. for saving list of users data replace code inlace of .child(user.uid)


with .childByAutoId()

. if you mention childByAutoId() it means the realtime database will create a


unique id for every user

*******************

– 2) code for getting saved data


*******************
. we will get the data by using table name

. ref.child("users").observeSingleEvent(of: .value, with: { (snapshot) in


// Get user value
print("snapshot",snapshot)

if !(snapshot.value is NSNull)
{
self.dicFirebaseData = snapshot.value as! NSDictionary
}
// snapshot.value will always return dictionary data

*******************
– 3) code for update user data
*******************
. updation will happen with path ex: if you want update 3rd user details
from list database or details of single user we will update fields using table
name and userid and key

ref.child("users/\(userid)/name").setValue(txtUserName.text!)
//here users is tableview name and userid is unique id and name is key of
updation value

*******************

– 4) code for deleting user data from firebase


*******************
. ref.child("users/\(userid)”).removeValue()

*******************

Vous aimerez peut-être aussi