Wednesday, October 28, 2026
HomeiOS Developmentios - SWIFTUI/FIREBASE: Firebase information just isn't being fetched/mapped accurately

ios – SWIFTUI/FIREBASE: Firebase information just isn’t being fetched/mapped accurately

[ad_1]

https://www.loom.com/share/72e124ba35fe44489e472c1d442c751a

^^HERE IS A QUICK VIDEO WHERE I DEMO THE BUG AND THE CODE!!! ANY HELP WOULD BE EXTREMELY APPRECIATED! THANK YOU!

import Firebase
import FirebaseAuth
import FirebaseFirestoreSwift
import SDWebImageSwiftUI
import SwiftUI

//THE VIEWMODEL

@MainActor
class ContactsViewModel: ObservableObject {

  @Printed var person: Consumer
  @Printed var userRequestInboxUsers = [User]()
  @Printed var emergencyContactUsers = [User]()

  init(person: Consumer) {
    self.person = person
    self.fetchTheUsersRequests()
    // self.fetchTheUsersContacts()
    self.fetchContacts()
  }

  //this perform works accurately
  func fetchTheUsersRequests() {

    guard let uid = person.id else { return }
    let question = COLLECTION_FOLLOWERS.doc(uid).assortment("inbox").whereField(
      "currentStatus", isEqualTo: "isPending")

    question.addSnapshotListener(includeMetadataChanges: true) { snapshot, error in
      if let error = error {
        print("There was an error querying the inbox requests: (error.localizedDescription)")
      } else {

        for request in snapshot!.paperwork {

          COLLECTION_USERS.doc(request.documentID).getDocument { snapshot, error in
            if let error = error {
              print("There was an error fetching the person information: (error)")
            } else {
              guard let userRequestInInbox = attempt? snapshot?.information(as: Consumer.self) else { return }
              self.userRequestInboxUsers.append(userRequestInInbox)
            }
          }
        }
      }
    }
  }

  //that is the primary perform I wrote to attempt to fetch the contacts
  //this func works in mapping the person nonetheless it initially reveals duplicate information.... I feel it is because im appending as a substitute of assigning
  func fetchTheUsersContacts() {
    guard let uid = person.id else { return }

    let question = COLLECTION_FOLLOWERS.doc(uid).assortment("inbox").whereField(
      "currentStatus", isEqualTo: "emergencyContact")

    question.addSnapshotListener(includeMetadataChanges: true) { snapshot, error in
      if let error = error {
        print("There was an error querying the emergency contacts: (error.localizedDescription)")
      } else {
        for userContact in snapshot!.paperwork {
          COLLECTION_USERS.doc(userContact.documentID).getDocument { snapshot, error in
            if let error = error {
              print("There was an error fetching the person information: (error)")
            } else {
              guard let contact = attempt? snapshot?.information(as: Consumer.self) else { return }
              self.emergencyContactUsers.append(contact)
            }
          }
        }

      }
    }
  }

  //that is the Second perform I wrote to attempt to fetch the contacts
  //this perform doesnt duplicate the information nonetheless it doesnt correctly map to my person object. It solely provides me entry to the customers's ID
  func fetchContacts() {

    guard let uid = person.id else { return }

    let question = COLLECTION_FOLLOWERS.doc(uid).assortment("inbox").whereField(
      "currentStatus", isEqualTo: "emergencyContact")

    question.addSnapshotListener(includeMetadataChanges: true) { snapshot, error in

      guard let paperwork = snapshot?.paperwork else {
        print("no contacts but...")
        return
      }
      self.emergencyContactUsers = paperwork.compactMap { (mapUser) -> Consumer? in
        print(mapUser.documentID)
        return attempt? mapUser.information(as: Consumer.self)
      }

    }
  }

}

//THE USER OBJECT

struct Consumer: Codable, Identifiable, Equatable {

  //firebase uid turns into the deal with
  @DocumentID var id: String?

  //The username cant have duplicates within the database + is how customers search different customers so as to add them to their particular associates listing
  var fullName: String?
  var userName: String?
  var age: Double?
  var intercourse: String?
  var weight: Double?
  var peak: String?
  var eyeColor: String?
  var hairColor: String?
  var homeAddress: String?
  var identifyingFeatures: String?
  var shoeSize: Double?
  var telephone: String?
  var profileImageUrl: String?

  //the place the present location of the person is saved and the property referred to as at any time when a person must share their loco
  //var location: CLLocation?
}

//THE VIEW

struct ContactsView: View {

  var person: Consumer
  @ObservedObject var viewModel: ContactsViewModel

  init(person: Consumer) {
    self.person = person
    self.viewModel = ContactsViewModel(person: person)
  }

  var physique: some View {

    NavigationView {
      ScrollView {

        Rectangle()
          .body(peak: UIScreen.principal.bounds.peak * 1 / 10)
          .foregroundColor(.white)

        VStack(spacing: 20) {
          NavigationLink {
            SearchView()
          } label: {
            SearchButton()
          }

          Divider()

          NavigationLink {
            UserRequestList(person: person)
          } label: {
            ZStack {
              RoundedRectangle(cornerRadius: 25)
                .body(width: 200, peak: 70)

              Textual content("Your present requests").foregroundColor(.white)
            }
          }
        }

        Divider()

        Textual content("Your Emergency Contacts:").daring()
          .foregroundColor(.inexperienced)
          .padding(.vertical)

        ForEach(viewModel.emergencyContactUsers) { person in
          NavigationLink {
            EmergencyContactsDetailView(person: person)
          } label: {
            UserRequestCell(person: person)
              .body(maxWidth: .infinity)
              .foregroundColor(.inexperienced)
          }
        }

        Divider()

        Rectangle()
          .body(peak: UIScreen.principal.bounds.peak * 1 / 10)
          .foregroundColor(.white)

      }
    }
    .onAppear {
      print(viewModel.emergencyContactUsers.description)
    }
    .navigationBarTitle("")
    .navigationBarHidden(true)
    .navigationBarBackButtonHidden(true)
  }

}

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments