Display custom information according to user location FireStore

I'm trying to display custom information according to userLocation(i.g, ft the user is in Los Angeles, He will get Text("X"). If he is in San Diego, he will get Tex("Y")). For this, I'm using Firestore. There, I have a collection called TEST --> auto ID --> "Name": "Test", Los Angeles: :true

This is how I get userLocation:

class LocationViewModel: NSObject, ObservableObject, CLLocationManagerDelegate{
@Published var locationManager = CLLocationManager()
@Published var userLocation : CLLocation!
@Published var userAddress = ""
@Published var noLocation = false

func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
    switch manager.authorizationStatus {
    case .authorizedWhenInUse:
        self.noLocation = false
    case .denied:
        self.noLocation = true
        self.noLocation = false

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    self.userLocation = locations.last

func extractLocation(){
    CLGeocoder().reverseGeocodeLocation(self.userLocation) { (res, err) in
        guard let safeData = res else {return}
        var address = ""
     //   address += safeData.first?.name ?? ""
     //   address += ", "
        address += safeData.first?.locality ?? ""
        self.userAddress = address

Then, I have this code to getDocuments:

struct Testtt: Identifiable{
var id: String = UUID().uuidString
var name: String

class TesteViewModel: NSObject,ObservableObject{
@StateObject var LocationModel = LocationViewModel()
let db = Firestore.firestore()
@Published var testsss = [Testtt]()

func testeApp(){
    db.collection("Test").whereField(LocationModel.userAddress, isEqualTo: true).getDocuments { (querySnapshot, err) in
        guard let documents = querySnapshot?.documents else {return }
        self.testsss = documents.map { (queryDocumentSnapshot) -> Testtt in
           let data = queryDocumentSnapshot.data()
            let name = data["Name"] as? String ?? ""
            return Testtt(name: name)

  • Before, instead of LocationModel.userAddress, I manually added the location and it worked fine. But, after using it, the project launched but I got 2 error:

Purple error: Accessing StateObject's object without being installed on a View. This will create a new instance each time.

Thread 1 error: Invalid field path (). Paths must not be empty, begin with '.', end with '.', or contain '..'"

On debug I got: Terminating app due to uncaught exception 'FIRInvalidArgumentException', reason: 'Invalid field path (). Paths must not be empty, begin with '.', end with '.', or contain '..''

The app crashes right after I press the button that takes me to this class's view

Read more here: https://stackoverflow.com/questions/64948882/display-custom-information-according-to-user-location-firestore

Content Attribution

This content was originally published by Mateus Neves at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: