firebase / firebase/firebase-ios-sdk

Crash when trying to `updateData`/`setData` for individual fields.

Open
#9,523 21 comments 1 reaction 1 assignee Assigned to @morganchen12 View on GitHub
api: firestore Swift API
Dominant language
C++
Stars
6.7k
Forks
1.8k
Avg merge
2d 18h
Merged PRs (30d)
75

Description

### [REQUIRED] Step 1: Describe your environment

* Xcode version: 13.2.1 (13C100)
* Firebase SDK version: 8.14.0
* Installation method: `Swift Package Manager`
* Firebase Component: Firestore
* Target platform(s): `iOS`

### [REQUIRED] Step 2: Describe the problem

#### Steps to reproduce:

The IOS Application crashes when trying im trying to use asynchronously (didn't tested it synchronously) one of the following:
1. `... .updateData([ "deviceModels": FieldValue.arrayRemove(oldDeviceModels) })`

2. `... .updateData([ "deviceModels": FieldValue.arrayUnion(newDeviceModels) })`

3. `... .updateData([ "deviceModels": newDeviceModels })`

4. `... .setData([ "deviceModels": newDeviceModels })`

The only error im getting is `Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT)`. But when going trough the stack trace I can see that it was caused by the function `.updateData([])`.

However `FieldValue.delete()` works just fine, as well as overwriting the hole document instead of just the field (via `.setData(from: categoryModel)`.

I hope this is not an issue on my side, but if so im sorry to bother you. If you need more information don't hesitate to ask. Thank you for your time!

#### Relevant Code:

##### Models

```swift
import Foundation
import FirebaseFirestoreSwift

struct CategoryModel: Identifiable, Codable {
@DocumentID var id: String?

let name: String
var deviceModels: [DeviceModel]
}

struct DeviceModel: Identifiable, Codable {
let id: String

let name: String
let imageURL: URL
let model: String
let brand: String
let purchaseDate: Date
let purchasePrice: Double
let warrantyPeriod: Date
let imageAssets: [URL]
let pdfAssets: [URL]
let repairModels: [RepairModel]
}

struct RepairModel: Identifiable, Codable {
let id: String

let name: String
let repairDate: Date
let repairPrice: Double
let warrantyPeriod: Date
}
```

##### FirestoreViewModel

```swift
public func arrayRemove(document: String, field: String, array: [DeviceModel]) async {
//self.actionState = .isLoading(action: .arrayRemove) - Not Important
do {
try await self.db.collection(self.collection).document(document).updateData([
field: FieldValue.arrayRemove(array)
])
//self.actionState = .none - Not Important
} catch {
//self.actionState = .error(error: error) - Not Important
}
}
```

##### View (SwiftUI)

```swift
internal let categoryModel: CategoryModel
@EnvironmentObject private var firestoreViewModel: FirestoreViewModel

// - Some view
.onDelete { offsets in
let deviceModelsToDelete = offsets.map { self.categoryModel.deviceModels[$0] }
Task.detached {
await self.firestoreViewModel.arrayRemove(document: self.categoryModel.id!, field: "deviceModels", array: deviceModelsToDelete)
}
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.