Wednesday, October 28, 2026
HomeiOS Developmentios - Tips on how to create array of objects in firebase

ios – Tips on how to create array of objects in firebase

[ad_1]

The explanation why it’s “updating” as an alternative of “including” when you find yourself working above statements for second time since you are utilizing the identical DocumentID, which you specify as “automobile”.

If you’re working a for-loop perform so as to add a number of automobile paperwork, you possibly can modify your codes just like beneath instance.

Methodology 1

let vehicleDocID: Int = 0

let automobile = Car(make: make, mannequin: modell, value: value, mileage: mileage)

for automobile in vehicleS {
     vehicleID += 1
     db.assortment("Admin").doc("vehicleID").setData(from: automobile) 

}

Alternatively, you possibly can merely use .add() perform to get defaultID, which I personally suggest.

Methodology 2

var ref: DocumentReference? = nil
ref = db.assortment("Admin").addDocument(information: [
    "make": "make",
    "model": "modell",
    "price": "price",
    "mileage": "mileage"
]) { err in
    if let err = err {
        print("Error including doc: (err)")
    } else {
        print("Doc added with ID: (ref!.documentID)")
    }
}

enter image description here

Lastly, in case, if the variety of automobiles are massive (>100), I recommend you utilize batch writes so as to add the paperwork to Firestore, because it ensures higher efficiency. For batch writes, you might seek advice from beneath Firestore documentation.

https://firebase.google.com/docs/firestore/manage-data/transactions

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments