0

How can I get the numberOfMarkers out of the reading method of Firebase in Swift? if I use the function in the {} this will save and I will be can use it not in the {}?

docRef = Firestore.firestore().document("Markol/Markers")
docRef.getDocument{ (docSnapshot, error) in
    guard let docSnapshot = docSnapshot, docSnapshot.exists else {return}
    let myData = docSnapshot.data()
    let numberOfMarkers = myData?["NumberofMarkers"] as? Int ?? 0
}
//Here i want to get the let numberOfMarkers


    var markerArrayList = [GMSMarker]()
    func makeAMarker(_ Latitude:Double , _ Longitude:Double , _ Title:String,Snippet:String) -> GMSMarker{
        let GmMarker = GMSMarker()
        GmMarker.position = CLLocationCoordinate2D(latitude: CLLocationDegrees(Latitude), longitude: CLLocationDegrees(Longitude))
        GmMarker.title = Title
        GmMarker.snippet = Snippet
        GmMarker.icon = UIImage(named: "smallStoreIcon")
        return GmMarker
    }
OfekG
  • 9
  • 4

2 Answers2

1

getDocument is an asynchronous task, so numberOfMarkers is only accessible before the closing }.

Do whatever you want with numberOfMarkers inside the getDocument listener, you may need to refactor your existing code to accommodate this. For example:

docRef = Firestore.firestore().document("Markol/Markers")
docRef.getDocument{ (docSnapshot, error) in
    guard let docSnapshot = docSnapshot, docSnapshot.exists else {return}
    let myData = docSnapshot.data()
    let numberOfMarkers = myData?["NumberofMarkers"] as? Int ?? 0
    processMarkers(numberOfMarkers, myData)
}

If this approach isn't clear, try posting more of your code in your question so others can help you restructure.

Jake Lee
  • 7,549
  • 8
  • 45
  • 86
  • there is a way to set a new var out side of the {} and use it same as numberOfMarkers? and what the last line do? – OfekG Jan 14 '19 at 14:22
  • I suggest you read the link I sent. Basically anything *outside* the `getDocument{}` cannot read values from inside it. If you post more code we can help you restructure it. – Jake Lee Jan 14 '19 at 14:23
  • @OfekG In your original question please! – Jake Lee Jan 14 '19 at 14:27
0

No you can't. Variable/constant is always visible just inside scope where is declared, between curly braces {...}.


What you probably want to do is to get this value to return it or use somewhere else. Don't do it, since getting data from Firestore is asynchronus task, use completion handler instead and return value (or nil if you don’t have value) as completion's parameter when you have it

func call(completion: @escaping (Int?) -> Void) {
    ...
    docRef.getDocument{ docSnapshot, error in
        guard let docSnapshot = docSnapshot, docSnapshot.exists else {
            completion(nil)
            return 
        }
        let myData = docSnapshot.data()
        let numberOfMarkers = myData?["NumberofMarkers"] as? Int
        completion(numberOfMarkers)
    }
}

then when you need to call it

call { numberOfMarkers in // code inside this closure is called with parameter of type `Int?` when you receive data and call completion from inside `call`
    if let number = numberOfMarkers {
        ... // do something with it
    }
}

... here you can use it for next purpose

Robert Dresler
  • 10,580
  • 2
  • 22
  • 40