Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to work with Firebase #181

Closed
leonidSpiri opened this issue Jan 2, 2021 · 6 comments
Closed

How to work with Firebase #181

leonidSpiri opened this issue Jan 2, 2021 · 6 comments

Comments

@leonidSpiri
Copy link

Hi, guys. I'm not very good at swift ui and i have a question about firebase database)
i need to get values from database and then put them into BarChartView. but if i do it with onAppear method, reloaded only values. the graph itself does not reloaded. can you help me please?
thank you

@jevonmao
Copy link

@lenchikspirdonchik I'm glad to assist you. Can you share your code snippet so I can see how are you updating your values?

@leonidSpiri
Copy link
Author

leonidSpiri commented Jan 13, 2021

@jevonmao
Thank you)

`
@State var numbers:[Int] = [1,1,1,1,1,1,1,1] // so i've got an array. now it's just with ones.
var body: some View {

    BarChartView(data: ChartData(values: [("Батарейки", numbers[0]), ("Бумага", numbers[1]), ("Техника", numbers[2])]), title: "Sales", legend: "Quarterly", form: ChartForm.extraLarge)
      .onAppear {
            let myUid = currUser()?.uid
            if (myUid != nil){
                getStatistics().getGarbage(uid: myUid!) { (done) in // func that get array from database 
                    numbers = done
                }
            }
        }

`

@jevonmao
Copy link

jevonmao commented Jan 14, 2021

I will try to setup a project with similar code and see if I can recreate. But for now, can you use print() to check if values in done are correct?
Also see if it helps if you initialize numbers as empty array like @State var numbers:[Int]=[]

@leonidSpiri
Copy link
Author

@jevonmao
Yeap, I’ve already tried to debug, so I use print(). And ‘done’ array return good values. So there aren’t any problems with getting values from db.
I don’t try to use empty’s array, so thank you. I’ll try it later.

@jevonmao
Copy link

jevonmao commented Jan 15, 2021

I found a solution! 🎉🎉🎉🎉 @lenchikspirdonchik

TL;DR

To fix your issue, first initialize your numbers array as empty. Then use an if statement to only render the charts once the array has been filled with numbers.

Explanation

The issue you were facing is because when the @State var numbers was initialized, you gave it some placeholder values like [1,1,1,1,1,1]. SwiftUI instantly pass that data into the BarChartView. However, the networking takes time, and the BarChartView's properties are not SwiftUI @State or @Binding meaning they will never be updated. My method use an if statement and only render out the BarChartView after the array is filled with proper values.

struct ContentView: View {
    
    ///Initialized as empty array
    @State var numbers:[Int] = []

    var body: some View {

        ///This can be any parent view, put the onAppear method on this parent view
        VStack {

            ///Check if numbers is empty, and only render the BarChartView once the array have been populated with values
            if numbers != []{

                BarChartView(data: ChartData(values: [("Батарейки", numbers[0]), ("Бумага", numbers[1]),  ("Техника", numbers[2])]), title: "Sales", legend: "Quarterly", form: ChartForm.extraLarge)

            }
          }

        .onAppear {
        ///Mock a networking call, the numbers will be set to [8,12,5] after a 5 second delay
          DispatchQueue.main.asyncAfter(deadline: .now()+5){
              numbers = [8,12,5]
          }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

@leonidSpiri
Copy link
Author

@jevonmao
Thank you very much. it works great! good code)
and another question, maybe you now) - how to add listener to mapkit (apple)? user tap on map, and listener return lat lang?
thx again)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants