Retrieving Items From One Array In Two Different Builds Returns Non-consistent Results

- 1 answer

swift 4.1, Xcode 10

I am fetching documents from Google Firestore and creating corresponding objects from these. These objects are later put into an array. The code that provides the array looks like this:

for document in querySnapshot!.documents {
    // Data is fetched and "parsed" here.
   if let coords = document.get("Geopoint"), let prijmajKarty = document.get("Card"), let name = document.get("Name"), let monOn = document.get("mondayOn"), let tueOn = document.get("tuesdayOn"), let wedOn = document.get("wednesdayOn"), let thuOn = document.get("thursdayOn"), let friOn = document.get("fridayOn"), let satOn = document.get("saturdayOn"), let sunOn = document.get("sundayOn"), let monOff = document.get("mondayOff"), let tueOff = document.get("tuesdayOff"), let wedOff = document.get("wednesdayOff"), let thuOff = document.get("thursdayOff"), let friOff = document.get("fridayOff"), let satOff = document.get("saturdayOff"), let sunOff = document.get("sundayOff")  {
    // Instances of VecerkaAnnotation are created here and added in an array for further use.
    let point = coords as! GeoPoint
    let lat = point.latitude
    let lon = point.longitude
    let coord = CLLocationCoordinate2D(latitude: lat, longitude: lon)
    let newVecerka = VecerkaAnnotation(myCoordinate: coord) = name as! String
    newVecerka.prijmajiKarty = prijmajKarty as! Bool
    newVecerka.monOn = monOn as! Int
    newVecerka.monOff = monOff as! Int
    newVecerka.tueOn = tueOn as! Int
    newVecerka.tueOff = tueOff as! Int
    newVecerka.wedOn = wedOn as! Int
    newVecerka.wedOff = wedOff as! Int
    newVecerka.thuOn = thuOn as! Int
    newVecerka.thuOff = thuOff as! Int
    newVecerka.friOn = friOn as! Int
    newVecerka.friOff = friOff as! Int
    newVecerka.satOn = satOn as! Int
    newVecerka.satOff = satOff as! Int
    newVecerka.sunOn = sunOn as! Int
    newVecerka.sunOff = sunOff as! Int

I am printing the whole array just after its put together and then printing each item one by one to compare the content of the array vs the objects that are tapped, here's the result:

[<Vecerka.VecerkaAnnotation: 0x600005c12ee0>, 
<Vecerka.VecerkaAnnotation: 0x600005c1b480>, 
<Vecerka.VecerkaAnnotation: 0x600005cee620>, 
<Vecerka.VecerkaAnnotation: 0x600005c1bb60>]

Printed items:

<Vecerka.VecerkaAnnotation: 0x600005c12ee0>
<Vecerka.VecerkaAnnotation: 0x600005c1b480>
<Vecerka.VecerkaAnnotation: 0x600005cee620>
<Vecerka.VecerkaAnnotation: 0x600005ceec60>

You can see that the last printed item doesnt correspond to any ID in the array.

I built the app again just a few seconds later, without any changes to the code:

[<Vecerka.VecerkaAnnotation: 0x600000e61a40>, 
<Vecerka.VecerkaAnnotation: 0x600000e601e0>, 
<Vecerka.VecerkaAnnotation: 0x600000e60be0>, 
<Vecerka.VecerkaAnnotation: 0x600000e61cc0>]

<Vecerka.VecerkaAnnotation: 0x600000e61a40>
<Vecerka.VecerkaAnnotation: 0x600000e601e0>
<Vecerka.VecerkaAnnotation: 0x600000e60be0>
<Vecerka.VecerkaAnnotation: 0x600000e61cc0>

And you can see that on the second build everything works just fine.

I tested this on many different builds, tried running both on simulator and real device, the result is the same.

Could this be an issue with the communication between the app and Firestore?

If anyone can help me with this it would be greatly appreciated, I have no idea what is going on behind the scenes so if anyone can shed some clarity on this one, it would be perfection.

Thanks a million! :))



So the problem was obviously somewhere else.

I was calling the method that creates the map annotations before the array of the annotations actually finished filling. Because I forgot to think asynchronously.

The problem is that the firebase call is asynchronous so the array has to have a properity observer and update the mapView (create the annotations) accordingly, when the array has changed.