0

I am trying to pull information from my Collection called A and want the unique values from the field origin for each document within the collection. So, say if I have three documents B, C, and D with B having and origin of Dominican Republic, C having an origin of United States, and D having an origin of United States. Then I want to display Dominican Republic and United States. I do not want to display Dominican Republic, United States, and United States. I thought putting id: .origin would display unique Origin values, but that is not the case.

My code does not return the unique values of origin:

struct ShowCigarOriginsView: View {
    
    @StateObject var vm = CigarsViewModel()
    
    var body: some View {
        
            ScrollView {
                
                ForEach(vm.cigars, id: \.origin){ cigar in
                    
                    NavigationLink(destination: Text("TEST"),
                                   label: {
                        
                        HStack(spacing: 16) {
                            WebImage(url: URL(string: cigar.nameImageURL))
                                .resizable()
                                .scaledToFit()
                                .frame(width: 100, height: 50)
                                .clipped()
                                .cornerRadius(50)
                                .overlay(RoundedRectangle(cornerRadius: 10)
                                    .stroke(Color(.label), lineWidth: 0)
                                )
                            VStack{
                                Text("\(cigar.origin)")
                                    .foregroundColor(Color(.label))
                                    .frame(maxWidth: .infinity, alignment: .center)
                            }
                            Spacer()
                        }.padding(.horizontal)
                    })
                    Divider()
                        .padding(.vertical, 8)
                }
            }.navigationTitle("Origins")
    }
}
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Lyon
  • 1
  • 2
  • 1
    Firestore doesn't do unique aggregation queries. You'll have to write code to collapse any duplicates that you find in the query you're doing now. – Doug Stevenson Feb 05 '23 at 19:41
  • You can use a set `let origins = Array(Set(cigars.map { $0.origin } ))` – Paulw11 Feb 05 '23 at 20:40

0 Answers0