Search code examples
swiftdatabasefirebasefirebase-realtime-databaseuipageviewcontroller

Send data form PageVC to Firebase


I have a UIPageViewController with three ViewControllers. Is it possible to enter data in each VC and via the button in the last VC send all entered data to the FirebaseDatabase as one record?

Thanks for the help!


Solution

  • You can do it many ways.

    Way One

    Create global variables and store your data to them. In last VC just assemble them the way you want and send it to your firebase database.

    Way Two

    You can create a singleton class with necessary attribute. Access the singleton object in every ViewController and stores your data, in last ViewController get you data from singleton and send it to firebase. To learn more about singleton see this article.

    Way three

    You can make a base class of this three ViewController classes. In base class store your data. In last ViewController access the base class and make you necessary object to send into firebase.

    Way you can send data to firebase

    Keep your database reference.

    var ref: DatabaseReference!
    ref = Database.database().reference()
    

    In button click action send your data or object to firebase like below way.

    self.ref.child("users").child(user.uid).setValue(["username": username])
    

    To learn more about firebase follow the official documentation.

    Note: The problem you have can solve in different way. I just listed down some way here. Hope it helps you.