ios – Duplicated doc in assortment when added to Firestore


There’s a handlerDoneTapped() operate, which is utilized when the Completed button is clicked, which saves information from the fields, together with the picture and path to it, into the Firestore.

    func handleDoneTapped() {
        self.uploadImage()      // For Storage and path to it         

Contained in the handleDoneTapped() operate, there’s one other uploadImage() operate that uploads a picture to Storage and saves the path to the pic parameter.

    @ObservedObject var viewModel = NewItemView()

    func uploadImage() {
        let storage =
        let picData: Knowledge = pickedImages[0].jpegData(compressionQuality: 0.5)!
        let path = "itemImages/(UUID().uuidString).jpg"
        let ref =
        let metadata = StorageMetadata()
        metadata.contentType = "picture/jpg"
        _ = ref.putData(picData, metadata: metadata, completion: { (storageMetaData, error) in
            if error != nil {
                print(error?.localizedDescription as Any)
            } else {

                self.viewModel.singleitem.pic = path      // I specify the trail to the pic parameter [singleitem - also from NewItemView()]
                self.viewModel.updateOrAddItem()        // I make modifications by means of the operate from NewItemView()

@ObservedObject var viewModel = NewItemView() – refers us to Firestore parameters for saving information.

  class NewItemView: ObservableObject {

  @Printed var singleitem: SingleItem

    init(singleitem: SingleItem = SingleItem(title: "", creator: "", description: "", pic: "")) {
    self.singleitem = singleitem
      .sink { [weak self] singleitem in
        self?.modified = true
      .retailer(in: &self.cancellables)

  non-public var db = Firestore.firestore()
  non-public func addItem(_ singleitem: SingleItem) {
    do {
        var addedItem = singleitem
        addedItem.userId = Auth.auth().currentUser?.uid
        _ = strive db.assortment("gadgets").addDocument(from: addedItem)
    catch {

  non-public func updateItem(_ singleitem: SingleItem) {
    if let documentID = {
      do {
          strive db.assortment("gadgets").doc(documentID).setData(from: singleitem)
      catch {
  public func updateOrAddItem() {          // This func I name inside uploadImage() to replace path for pic parameter
                                           // (bc with out that pic: "" -is empty inside Firestore)
      if != nil {
    else {

 func handleDoneTapped() {         // This func I name after I tapped Completed button so as to add or replace merchandise

and eventually SingleItem struct with all vars:

struct SingleItem: Identifiable, FirestoreProtocol {
    @DocumentID var id : String?
    @ServerTimestamp var createdTime: Timestamp?
    var title : String
    var creator : String
    var description : String
    var userId : String?
    var pic : String
  enum CodingKeys: String, CodingKey {
    case id
    case title
    case creator
    case description = ""
    case pic

So, I perceive that I name the self.viewModel.updateOrAddItem() operate twice, but when I do not use this operate inside uploadImage(), then the doc is saved with out the pic parameter, which incorporates the path to the picture in Storage.
However after I’m utilizing this operate inside uplaodImage(), two equivalent paperwork are saved into the Firestore with the distinction that one in all them incorporates the path to the picture within the pic subject.

On the whole, I am a little bit confused, I attempted to vary the parameters for coming into information utilizing these features with a view to filter out a replica doc that doesn’t include the mandatory info, however all the pieces results in the truth that the doc is saved with out a pic subject (path inside is empty), or two, which one in all them is required, and the opposite is just not.

Perhaps somebody will see the place I am making a mistake. Additionally I’ve supplied solely the mandatory code that’s related to the issue, a part of the code, in addition to brackets could also be lacking.


Leave a Reply