WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

Read Data from Firebase to View

Forums > SwiftUI

Hello all,

I'm still a beginner and have a question, I'm trying to read data from the database with Firebase how can I pass this to a text? I want to pass "testUser" and display it.

anyone have an idea :-)

import SwiftUI
import FirebaseDatabase
import Firebase

struct uebergeben: View {

    var body: some View {

        VStack {

            Text(datenLesen)

        }
    }

    func datenLesen() {
        let user = Auth.auth().currentUser!.uid

        Database.database().reference().child("benutzer").child(user).child("nuterinfo").child("nutzername").observeSingleEvent(of: .value, with: { (snapshot) in
            if let testUser = snapshot.value as? String {
                print(testUser)
            }

        })
    }
}

struct uebergeben_Previews: PreviewProvider {
    static var previews: some View {
        uebergeben(test: "Daten Abrufen")
    }
}

1      

@twostraws has held a few live coding sessions (Swift on Sundays, for example) where he opens the floor to questions from the audience. I remember a few folks asking Paul to dedicate a session to integrating FireBase into a Swift app.

I remember him declining and understanding the logic of his reply. There are existing Apple technologies (CoreData, iCloud) that perform similar functions. This is not to say FireBase (and other technologies) aren't useful. But Hacking with Swift is an incredible site dedicated to (wait for it....) SWIFT. Swift and SwiftUI are changing as such a rapid pace, it's difficult just to keep up with Apple's updates and additions. Adding external toolsets, libraries, and dependencies would be overwhelming.

While there may be plenty of folks here who have dabbled with FireBase, or even successfully integrated it into a Swift application, I am suggesting you may not find a lot of support in these forums for your FireBase questions.

Ray Wenderlich Ray has a nice series, with Peter Friese showcasing FireBase.

Of course Google has their own videos: Google FireBase

These (and other) sites have up-to-date example for using FireBase with Swift, something you'll (most likely) not find here.

That being said, Hacking with Swift forums are great for sharing ideas and knowledge. I might suggest you approach your problem a different way.

You state, "I'm trying to read data from the database with Firebase how can I pass this to a text?"

This may not be the approach you should take. Instead consider this. Your first design descisions should be to code a data structure to hold the collection of data that you want to display. This will be a data source.

Next you want to design your views in such a way that the views display only the data that is in your application's data source. That's it. Your screens should not display ANYTHING unless it is in your application's data source. If you change something in your data source, then your views should sense this, and update their views. Get this part working first.

Finally, you want to think about transferring data from CoreData and iCloud, (or from FireBase, mySQL, FileMaker server, eBay API, GoogleMaps API, etc, etc, etc) into your application's data source.

By separating these pieces, your application's views should not care where the data comes from. From a file? From the internet? From a website's API? From the iPhone's photo library? From FireBase? The views don't need to know.

These are all lessons that you'll find in Hacking with Swift. This terrific site can help you design your data sources, develop compelling views, and link their functionality. Your last step will be to figure out how to grab raw data from FireBase, parse it, and stow it away in your application's data source. Those steps you can find on other sites.

Good luck! Please report back with your progess.

PS: These are my opinions. I do not speak for @twostraws. I am just sharing my past and current observations.

2      

Hacking with Swift is sponsored by Fernando Olivares

SPONSORED Fernando's book will guide you in fixing bugs in three real, open-source, downloadable apps from the App Store. Learn applied programming fundamentals by refactoring real code from published apps. Hacking with Swift readers get a $10 discount!

Read the book

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.