0

For an app for visual impaired persons, I want to show POI's in a view. I use code below. Although the lowest level (requestNearbyLocations) prints info to the debug screen, I'm not able to return this data (locdata) to the calling method, so nor to the view.

Main extract of the code used:

struct ContentView: View {    
    var Apple: AppleData = AppleData()
    
    var body: some View {
        Text(Apple.text)
            .onAppear {
                Apple.requestLoc()
            }
        } 
}

With

class AppleData {
    
    var text = ""
    
    func requestLoc() -> Void {
        Task {
            async let mytext = requestNearbyLocations()
            text = await mytext
        }
    }
    
    func requestNearbyLocations() async -> String {
        var region = MKCoordinateRegion()
        region.center = CLLocationCoordinate2D(latitude: 52.060049, longitude: 4.542196)
        
        var loctext = ""
        let request = MKLocalPointsOfInterestRequest(center: region.center, radius: 100.0)
        request.pointOfInterestFilter = MKPointOfInterestFilter(excluding: [.restaurant, .cafe])
        let search = MKLocalSearch(request: request)
        
        do {
            let response = try await search.start()
            print(response.mapItems)
            for item in response.mapItems {
                print( item.name! )
                loctext = item.name!
            }
        }
        catch {
            print(error)
        }
        return loctext
    }    
}

What have I missed here?

Rob
  • 415,655
  • 72
  • 787
  • 1,044
WimJanse
  • 13
  • 2
  • More than likely a wrapper of some kind. Nothing in your code is telling SwiftUI to reload the body. Try the [Apple SwiftUI Tutorials](https://developer.apple.com/tutorials/swiftui) – lorem ipsum Jun 05 '22 at 12:03
  • You say "I'm not able to return this data (locdata)" but your code contains nothing called `locdata`. So what are we even talking about? – matt Jun 05 '22 at 14:42
  • I did some cut/paste on the original code, left out the wrapper and just return one item (loctext, sorry I mentioned locdata). My problem is that I need to do more processing on the “text” property in the AppleData class, and although the text awaits the result of the call to requestNearbyLocations, the “text” String remains empty after the call – WimJanse Jun 05 '22 at 15:34
  • 1
    Thanks Rob, I now can compose a text string with all POIs relative to local coordinates of the (blind) user, showing distance and direction to the POI and have this spoken by voice-over. Thanks again – WimJanse Jun 06 '22 at 15:15
  • No, we don't need to see more code. This is a perfectly clear [MCVE](https://stackoverflow.com/help/mcve). We need the code to be the minimum required to manifest the problem, as well as to be complete enough to manifest the problem at hand. This code snippet is more than enough. In fact, it already is too much (e.g., we don’t need to see that `@main` `struct`; we probably don’t need to see all the standard `import` lines, either). – Rob Jun 06 '22 at 15:24

1 Answers1

0

You want to “observe” AppleData, which should “publish” changes in text.

Thus, declare AppleData to be an ObservableObject and text to be @Published:

class AppleData: ObservableObject {
    @Published var text = ""

    ...
}

And the ContentView should designate that object as an @ObservedObject:

struct ContentView: View {
    @ObservedObject var apple = AppleData()

    var body: some View {
        Text(apple.text)
            .onAppear {
                apple.requestLoc()
            }
    }
}

See Managing Model Data in Your App.

Rob
  • 415,655
  • 72
  • 787
  • 1,044