Skip to content

Crash when trying to updateData/setData for individual fields. #9523

Open
@lucaszischka

Description

@lucaszischka

[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
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
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)
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)
        }
    }

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions