3

I want to read a document, get a field from that document, and set a variable to that field's value

I expected my variable declared outside the Firebase getDocument function to be written to. The actual results are that the variable is being written to within the Firebase getDocument function but outside the function it is empty.

Here's what I have tried:

[1]: Modifying variable within firebase function - this didn't work for me because I can't translate it well with my current Swift skills

[2]: How to set variables from a Firebase snapshot (swift) - this didn't work for me because the implementation deviates by a lot from what I have right now


//open the user Firebase database by documentID
     let userDocument = userdb.collection("users").document(userDocumentId)

     var userjobsData = [[String:Any]]()

     userDocument.getDocument { (docums, error)  in

         if let docum = docums, docum.exists {

         //grab all jobs data
         userjobsData = docum.get("jobData") as! [[String:Any]]

         //sort jobs by category in alphabetical order
         userjobsData = (userjobsData as NSArray).sortedArray(using: [NSSortDescriptor(key: "category", ascending: true)]) as! [[String:AnyObject]]

         }
        //Here userjobsData contains data
        print(userjobsData)
     }
     //Here userjobsData is empty
     print(userjobsData)

1 Answer 1

2

Actually what is happening in your case Firebase fetching data is asynchronous task and takes some time to fetching data , in mean time you are reading your userjobsData which is empty since Firebase request has not been completed .

What you can do is actually perform needed operation after fetching data from firebase .

Adding Sample code for your reference.

private func fetchDataFromFirebase(){

  let userDocument = userdb.collection("users").document(userDocumentId)

     var userjobsData = [[String:Any]]()

     userDocument.getDocument { (docums, error)  in

         if let docum = docums, docum.exists {

         //grab all jobs data
         userjobsData = docum.get("jobData") as! [[String:Any]]

         //sort jobs by category in alphabetical order
         userjobsData = (userjobsData as NSArray).sortedArray(using: [NSSortDescriptor(key: "category", ascending: true)]) as! [[String:AnyObject]]

          self.perfomAction(firebaseResult : userjobsData)
          // now pass this data to your need function like
         }

  }
}

private func perfomAction(firebaseResult : [[String:Any]]){
  // perform your job here
}
Sign up to request clarification or add additional context in comments.

3 Comments

How could I display firebaseResult in SwiftUI?
its upto you , how make use array of [String:Any] . what all are properties in this , extract using keys get the value .
The problem I have now is that when I use this in SwiftUI i get an infinite loop struct FilterScreenSwiftUIView: View { @State var jobs = [Job]() @available(iOS 13.0, *) var body: some View { fetchDataFromFirebase() return List(jobs) { job in JobRow(job: job) } }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.