GitXplorerGitXplorer
m

SwiftAsyncShims

public
2 stars
1 forks
0 issues

Commits

List of commits on branch main.
Unverified
e0c774dd9e7266fa6ff715cc424bb3410bb65230

Update documentation and tests

mmarkiv committed 2 years ago
Unverified
6c903866c3f62be756c97e26e4c60bb493460932

Initial commit. CLLocationManager async extension.

mmarkiv committed 2 years ago

README

The README file for this repository.

SwiftAsyncShims

A collection of extensions that add async methods to existing Apple APIs. These allow you to replace delegates and callbacks with modern Swift concurrency ā€“ making code easier to reason about. šŸ™Œ

Installation

Integrate it in your project via the Swift Package Manager. Just add https://github.com/markiv/SwiftAsyncShims.git to your list of dependecies.

CoreLocation

Common Tasks

Use the convenience CLLocationManager initializer with support for common configuration parameters:

let manager = CLLocationManager(distanceFilter: 100)

Ask for permission if necessary, and then get a single location:

if await manager.requestWhenInUseAuthorization().isAuthorizedWhenInUse {
    print("Authorized!")
    do {
        let location = try await manager.requestLocation()
        print(location)
    } catch {
        print(error)
    }
}

You can also ignore the return value and check authorizationStatus as usual.

await manager.requestWhenInUseAuthorization()
ā‹®
if manager.authorizationStatus.isAuthorizedWhenInUse {
    ā‹®
}

Request an asynchronous stream of locations. Because the stream is an AsyncSequence, the call point can use the for-await-in syntax to process each location instance as produced by the stream ā€“ and a simple break statement to stop the stream. This also allows us to use sequence operators such as prefix, map, reduce and max, for example:

if await manager.requestWhenInUseAuthorization().isAuthorizedWhenInUse {
    print("Authorized!")
    do {
        // Get at most 5 locations in an asynchronous stream
        for try await location in manager.requestLocationStream().prefix(5) {
            print(location)
            if location.horizontalAccuracy < 50 {
                print("Stopping the stream...")
                break // stop updating locations
            }
        }
    } catch {
        print(error)
    }
} else {
    // Show Settings button 
}