Async location streamer using new concurrency model in Swift

Async location streamer using new concurrency model in Swift

Features

  • Using new concurrency swift model around CoreLocation
  • Streaming current locations asynchronously
  • Customizable in terms of accuracy
  • Errors handling

How to use

1. Add to info the option “Privacy – Location When In Use Usage Description”

2. Add or inject LMViewModel into a View

    @EnvironmentObject var model: LMViewModel

3. call ViewModel method start() within async environment of the View

             Task{
                 do{
                     try await viewModel.start()
                 }catch{
                     self.error = error.localizedDescription
                 }
             }

4. Bake into visual presentation async stream of data from “locations” property of the ViewModel

    @ViewBuilder
    var coordinatesTpl: some View{
        List(viewModel.locations, id: \.hash) { location in
            Text("\(location.coordinate.longitude), \(location.coordinate.latitude)")
        }
    }

5. Showcase error

    ///Access was denied by  user
    case accessIsNotAuthorized

ViewModel API

public protocol ILocationManagerViewModel: ObservableObject{
        
    /// List of locations
    @MainActor
    var locations : [CLLocation] { get }
    
    /// Start streaming locations
    func start() async throws
    
    /// Stop streaming locations
    func stop()
}

SwiftUI example of using package

async-location-swift-example

if you are using the simulator don’t forget to simulate locations

Documentation(API)

  • You need to have Xcode 13 installed in order to have access to Documentation Compiler (DocC)
  • Go to Product > Build Documentation or ⌃⇧⌘ D

GitHub

View Github