firebase / firebase/firebase-ios-sdk
Async/Await missing for setData
- Dominant language
- C++
- Stars
- 6.7k
- Forks
- 1.8k
- Avg merge
- 2d 14h
- Merged PRs (30d)
- 72
Description
### [REQUIRED] Step 1: Describe your environment
* Xcode version: 13.2.1
* Firebase SDK version: 8.10.0
* Installation method: `Swift Package Manager`
* Firebase Component: Firestore
* Target platform(s): `iOS`
### [REQUIRED] Step 2: Describe the problem
When using the new async/await methods in swift, they seem to be missing when setting an Encodable object to the Firestore.
#### Steps to reproduce:
Try the below code using the library. It will throw these warnings:
`No 'async' operations occur within 'await' expression`
`No calls to throwing functions occur within 'try' expression`
```swift
func setFriend() async throws {
try await db.collection("friends").document("123").setData(from: friend, merge: true)
}
```
That's because it's not seeing an async version of the function.
But, if you were to try:
```swift
func setFriend() async throws {
try await db.collection("friends").document("123").setData(["some": "data"], merge: true)
}
```
This works because async/await is available for that function.
### My possible solution
I created this extension that seems to solve it. If we all agree, could this go in the main library? That would give us full support for setting data via async/await.
```swift
extension FirebaseFirestore.DocumentReference {
func setData(from: T, merge: Bool = false) async throws {
let encoder = Firestore.Encoder()
let data = try encoder.encode(from)
try await setData(data, merge: merge)
}
}
```
Contributor guide
Assessment
This issue has not been assessed yet.