2

I am new to Swift and I am wondering why the array is empty at the end of the function at the print("array printed2: \(newArr)") line, when at the first print statement print("array printed1: \(newArr)") it is filled with data.

Does anyone have any corrections or suggestions on how to make the data in the array stay?

Thanks in advance, the code for the function is below.

func testRdArr(){

         var newArr=[Establishment]()
          print("Downloading club data...")
           let db = Firestore.firestore()


               db.collection("clubs").getDocuments() { (querySnapshot, err) in
                   if let err = err {
                       print("Error getting documents: \(err)")
                   } else {
                       for document in querySnapshot!.documents {
                          let data = document.data()





                         let name = data["Name"] as? String ?? "Name Unknown"
                         let description = data["description"] as? String ?? "Description Uknown"
                         let imageURL1 = data["imgUrl"] as? String ?? "Image Unknown"
                         let coordX = data["coordX"] as? String ?? "Unknown"
                         let coordY = data["coordY"] as? String ?? "Unknown"
                         let openingTimes=data["openingTimes"] as? [String] ?? ["Unknown"]
                         let weeklyEvents=data["weekly events"] as? [String] ?? ["Unknown"]
                         let postcode=data["postcode"] as? String ?? "Unknown"
                         let id = document.documentID


                        let x = Establishment(name: name, imageURL1: imageURL1, coordX: coordX, coordY: coordY, postcode: postcode, openingTimes: openingTimes, weeklyEvents: weeklyEvents, description: description, type: "club", id: id)


                           newArr.append(x)

                       }
                   }
                //line below prints the populated array
                print("array printed1: \(newArr)")
                print("end of printing array")
               }
           //at this point array becomes empty, why does this happen and how would you suggest 
           //fixing it?
           print("array printed2: \(newArr)")

      }
v0lk
  • 58
  • 1
  • 6
  • 2
    It doesn't disappear, it hasn't appeared yet. In terms of timing `print("array printed2: \(newArr)")` comes first because `getDocuments()` works asynchronously and the result is returned later. Just do the things you have to do after receiving the data at the end **inside** the completion handler. – vadian Apr 11 '20 at 18:32
  • @vadianit has appeared, at the first print statement, it shows the contents of the array however at the second print statement its empty, I'm not sure if its outside a certain bracket but the array is initialized right at the beginning of the func. – v0lk Apr 11 '20 at 19:18
  • Please learn to understand asynchronous data processing. The **first** print statement is `print("array printed2`, the **second** is `print("array printed1:` The code is **not** executed in the order of the lines! – vadian Apr 11 '20 at 19:19
  • I understand what you mean now, thanks! And no problem, I'm going to master asynchronous data processing. – v0lk Apr 11 '20 at 19:34

1 Answers1

0

The download happens in the background and completes after your print at the bottom of the function. It did not become empty. That line happened before the download was done.

See: Waiting for Asynchronous function call to complete

Lou Franco
  • 87,846
  • 14
  • 132
  • 192