Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/aws-amplify/amplify-swift
A declarative library for application development using cloud services.
https://github.com/aws-amplify/amplify-swift
aws aws-amplify ios swift
Last synced: 5 days ago
JSON representation
A declarative library for application development using cloud services.
- Host: GitHub
- URL: https://github.com/aws-amplify/amplify-swift
- Owner: aws-amplify
- License: apache-2.0
- Created: 2019-03-21T19:16:08.000Z (almost 6 years ago)
- Default Branch: main
- Last Pushed: 2024-12-09T17:21:12.000Z (14 days ago)
- Last Synced: 2024-12-09T17:44:52.351Z (14 days ago)
- Topics: aws, aws-amplify, ios, swift
- Language: Swift
- Homepage:
- Size: 107 MB
- Stars: 463
- Watchers: 55
- Forks: 198
- Open Issues: 112
-
Metadata Files:
- Readme: README-combine-support.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# Combine support in Amplify
Amplify supports iOS 13+ and macOS 10.15+, and ships with APIs that leverage Swift Concurrency (async/await) to return values. For example, the following returns an array of type `Geo.Place` with search results for coffee shops.
```swift
let places = try await Amplify.Geo.search(for "coffee")
```Some APIs do not return a simple result, such as those that return subscriptions or provide progress updates. In cases where multiple values are expected over time, Amplify typically provides an `AmplifyAsyncSequence` or `AmplifyAsyncThrowingSequence`. These types conform to the `AsyncSequence` protocol and can be iterated over asynchronously. For example, the following subscribes to the creation of new Todos.
```swift
let subscription = Amplify.API.subscribe(
request: .subscription(of: Todo.self, type: .onCreate)
)
```## Amplify.Publisher
In order to support Combine, Amplify includes Amplify.Publisher, which can be used to get Combine Publishers for Amplify APIs, such as those listed above. Specifically, it provides static methods to create Combine Publishers from Tasks and AsyncSequences.
The following examples show how to create Combine Publishers for the above API calls.
```swift
let sink = Amplify.Publisher.create {
try await Amplify.Geo.search(for "coffee")
}
.sink { completion in
// handle completion
} receiveValue: { value in
// handle value
}
``````swift
let subscription = Amplify.API.subscribe(
request: .subscription(of: Todo.self, type: .onCreate)
)let sink = Amplify.Publisher.create(subscription)
.sink { completion in
// handle completion
} receiveValue: { value in
// handle value
}
```#### Cancellation
When using Amplify.Publisher, cancelling a subscription to a publisher also cancels the underlying task. Note, however, that in the case of progress sequences, this would only cancel progress updates, and not the associated task such as a file upload or download. Those associated tasks would need to be cancelled separately, either by calling `.cancel()` on the task itself or by cancelling the parent task.
## Hub.publisher(for:)
The Amplify Hub category exposes only one Combine-related API: `Hub.publisher(for:)`, which returns a publisher for all events on a given channel. You can then apply the standard Combine [`filter`](https://developer.apple.com/documentation/combine/anypublisher/filter(_:)) operator to inspect only those events you care about.