Firebase 迅捷+;Firestore-限制查询中的结果数

Firebase 迅捷+;Firestore-限制查询中的结果数,firebase,google-cloud-firestore,swiftui,Firebase,Google Cloud Firestore,Swiftui,我在Firestore中有一个餐厅数据库,我试图查询并返回餐厅集合中4个项目的列表。目前我的查询是: import SwiftUI import Firebase import SDWebImageSwiftUI struct ExploreView: View { @State var selectedTab = "Explore" @State var data: [RestaurantObject] = [] @State var metro

我在Firestore中有一个餐厅数据库,我试图查询并返回餐厅集合中4个项目的列表。目前我的查询是:

import SwiftUI
import Firebase
import SDWebImageSwiftUI

struct ExploreView: View {

    @State var selectedTab = "Explore"
    @State var data: [RestaurantObject] = []
    @State var metro = "Orlando"

let db = Firestore.firestore()

var body: some View {
    NavigationView {
        ScrollView(.vertical, showsIndicators: false) {
            VStack {
                HStack {
                    Text("Explore Orlando")
                        .font(.title)
                        .fontWeight(.bold)
                        
                    Spacer()
                    NavigationLink (destination: LocationsView()) {
                        Text ("Change")
                            .font(.callout)
                    }
                }.padding(.horizontal)
                .padding(.bottom, 30)
                // Cuisine Slider
                HStack {
                    Text("All cuisines")
                        .font(.title2)
                        .fontWeight(.bold)
                    Spacer()
                    NavigationLink (destination: CuisinesView()) {
                        Text("View all")
                    }
                }.padding(.horizontal)
                .padding(.bottom, 20)
                ScrollView (.horizontal, showsIndicators: false) {
                    HStack(spacing: 12.0)  {
                        CuisineTile(image: "cuisine_breakfast", name: "Brunch")
                        CuisineTile(image: "cuisine_bbq", name: "BBQ")
                        CuisineTile(image: "cuisine_brazilian", name: "Brazilian")
                        CuisineTile(image: "cuisine_caribbean", name: "Caribbean")
                        CuisineTile(image: "cuisine_cuban", name: "Cuban")
                        CuisineTile(image: "cuisine_mexican", name: "Mexican")
                        CuisineTile(image: "cuisine_seafood", name: "Seafood")
                        CuisineTile(image: "cuisine_soulfood", name: "Soul Food")
                            .padding(.trailing, 31)
                    }.offset(x: 16)
                }
                // Featured Section
                VStack {
                    HStack {
                        Text("Featured Eats")
                            .font(.title2)
                            .fontWeight(.bold)
                            .foregroundColor(Color.white)
                        Spacer()
                    }.padding(.horizontal)
                    .padding(.top)
                    HStack {
                        Text("Join us every month as we highlight business owners with uplifting and inspiring stories.")
                            .foregroundColor(Color.white)
                            .font(.subheadline)
                        Spacer()
                    }.padding(.horizontal)
                    .padding(.vertical, 5)
                    ZStack {
                        VStack {
                            HStack {
                                Image("placeholder_feature")
                                    .resizable()
                            }.padding(.horizontal)
                        }
                        VStack {
                            Spacer()
                            HStack {
                                Text ("The story behind Papa Llama. Orlando's newest Peruvian restaurant.")
                                    .font(.body)
                                    .foregroundColor(Color.white)
                                    .padding()
                                Spacer()
                            }.background(Color("CharcoalGray"))
                            .cornerRadius(10, corners: [.bottomLeft, .bottomRight])
                            .padding(.horizontal)
                        }
                        
                    }
                    HStack {
                        Button(action: /*@START_MENU_TOKEN@*//*@PLACEHOLDER=Action@*/{}/*@END_MENU_TOKEN@*/) {
                            Text ("Read More")
                                .font(.subheadline)
                                .fontWeight(.medium)
                                .padding(.horizontal, 10)
                                .padding(.vertical, 5)
                                .background(Color.black)
                                .foregroundColor(.white)
                                .overlay (
                                    RoundedRectangle(cornerRadius: 6) .stroke(Color.white, lineWidth: 2)
                            )
                        }.padding(.top, 12)
                        .padding(.horizontal)
                        Spacer()
                    }
                    .padding(.bottom)
                }.background(Color.black)
                .padding(.vertical)
                VStack {
                    HStack {
                        Text("Orlando Eats")
                            .font(.title2)
                            .fontWeight(.bold)
                        Spacer()
                        Text("View all")
                    }.padding(.horizontal)
                    .padding(.top, 20)
                    VStack {
                        ScrollView(.horizontal, showsIndicators: false) {
                            HStack(spacing: 12.0) {
                                ForEach((self.data), id: \.self.restaurantID) { item in
                                    ExploreTile(image: item.restaurantImage, name: item.restaurantName)
                                }
                                
                            }.offset(x: 16)
                            
                        }
                        
                    }.padding(.bottom, 30)
                }
                Spacer()
            }
        }
        .onAppear {
            print("Function called")
            self.getRestaurants()
            
        }
        .navigationBarTitle("")
        .toolbar {
            ToolbarItem(placement: .principal) {
                Image("ue_logo")
                    .resizable()
                    .scaledToFit()
                    .frame(width: 20, height: 20)
            }
        }
    }
    
}

func getRestaurants() {
    self.data.removeAll()
    self.db.collection("businesses").whereField("metro", isEqualTo: self.metro).limit(to: 4).getDocuments() {(querySnapshot, err) in
        if let err = err {
            print("Error getting documents \(err)")
        } else {
            for document in querySnapshot!.documents {
                let id = document.documentID
                let name = document.get("name") as! String
                let image = document.get("photo") as? Array ?? [""]
                self.data.append(RestaurantObject(id: id, name: name, image: image[0] ))
            }
            print("This result returns the following items:")
            print(data.count)
        }
    }
}
}



extension View {
    func cornerRadius(_ radius: CGFloat, corners: UIRectCorner) -> some View {
    clipShape( RoundedCorner(radius: radius, corners: corners) )
}
}


struct RoundedCorner: Shape {

var radius: CGFloat = .infinity
var corners: UIRectCorner = .allCorners

func path(in rect: CGRect) -> Path {
    let path = UIBezierPath(roundedRect: rect, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
    return Path(path.cgPath)
}
}

class RestaurantObject: ObservableObject {
@Published var restaurantID: String
@Published var restaurantName: String
@Published var restaurantImage: String

init(id: String, name: String, image: String) {
    restaurantID = id
    restaurantName = name
    restaurantImage = image
}
}
但是,控制台显示:

调用的函数 调用的函数

此结果返回以下项: 4. 此结果返回以下项: 八,


函数的编写方式或结构顺序是否有问题?我打算从集合中返回4个项目,每个项目都包含“名称”和“照片”数组中的第一张照片。

为什么您希望得到不同的输出?请编辑问题以显示您希望从此查询中收到的文档。另外,你确定getRestaurants不会被调用两次吗?它似乎会调用getRestaurants两次,但我不确定为什么会调用getRestaurants,这是基于函数的。也许在我尝试获取“photo”数组中的第一项时出现了错误。如果看不到更多代码,我们就无法添加太多内容。我们不知道这些呼叫来自何处,也不知道原因。我看到其他人说SwiftUI lifecycle应用程序和.onAppear有一个bug,被调用了两次。使用此处提到的解决方案解决了