Skip to content

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. ๐Ÿ™Œ

License

Notifications You must be signed in to change notification settings

markiv/SwiftAsyncShims

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

2 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

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 
}

About

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. ๐Ÿ™Œ

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages