在Firestore Swift中使用getDocuments()进行查询时遇到困难

在Firestore Swift中使用getDocuments()进行查询时遇到困难,swift,firebase,google-cloud-firestore,Swift,Firebase,Google Cloud Firestore,这是我第一次使用Firestore查询,我正在努力解析数据。我通常在获取文档时使用相同的设置(可以正常工作),但是当我将其附加到查询时,它就不起作用了 我正在尝试查询访问次数最多的商店的数据库,以便稍后将其设置为收藏夹 我的代码: func findFavouriteShop(completed: @escaping ([String]) -> Void) { // Variables let dispatch = DispatchGroup() var dummy

这是我第一次使用Firestore查询,我正在努力解析数据。我通常在获取文档时使用相同的设置(可以正常工作),但是当我将其附加到查询时,它就不起作用了

我正在尝试查询访问次数最多的商店的数据库,以便稍后将其设置为收藏夹

我的代码:

func findFavouriteShop(completed: @escaping ([String]) -> Void)
{
    // Variables
    let dispatch = DispatchGroup()
    var dummyDetails = [String]()
    // References
    let db = Firestore.firestore()
    let userID = Auth.auth().currentUser?.uid
    let groupCollectionRef = String("visits-" + userID! )
    
    // Query the database for the document with the most counts
    dispatch.enter()
    db.collectionGroup(groupCollectionRef).order(by: "count", descending: true).limit(to: 1).getDocuments { (snapshot, error) in
        if let err = error {
            debugPrint("Error fetching documents: \(err)")
        }
        else {
            print(snapshot)
            guard let snap = snapshot else {return}
            
            for document in snap.documents {
                
                let data = document.data()
                
                // Start Assignments
                let shopName = data["shopName"] as? String
                let count = data["count"] as? String
                // Append the dummy array
                dummyDetails.append(shopName!)
                dummyDetails.append(count!)
         }
        dispatch.leave()
    }
    dispatch.notify(queue: .main, execute: {
    print("USER number of documents appended: \(dummyDetails.count)")
    completed(dummyDetails)}
    )
}
使用Print语句时,似乎guard语句将函数踢出。处理器未到达for循环以执行分配。打印快照时,它返回一个空数组


我肯定我使用了错误的表示法,但我不确定在哪里。

有很多值得评论的地方,比如你选择集合组而不是集合(可能这就是你需要的),为什么你将结果限制在一个文档中,但却觉得需要查询集合,集合的命名(似乎很奇怪),用于获取多个店铺但创建仅返回单个店铺的函数的查询,使用可能为整数的count属性的字符串,并使用字符串数组返回单个店铺的多个组件,而不是使用自定义类型

也就是说,我认为这应该让你走上正确的方向。我已经创建了一个自定义类型来向您展示如何开始这个过程,但是要使它达到您需要的位置,还有很多工作要做。但这是一个很好的起点。此外,不需要分派组,因为您在文档解析中没有执行任何其他异步工作

class Shop {
    let name: String // constant
    var count: Int // variable
    
    init(name: String, count: Int) {
        self.name = name
        self.count = count
    }
}

func findFavouriteShops(completion: @escaping (_ shops: [Shop]?) -> Void) {
    guard let userID = Auth.auth().currentUser?.uid else {
        completion(nil)
        return
    }
    var temp = [Shop]()
    
    Firestore.firestore().collection("visits-\(userID)").order(by: "count", descending: true).limit(to: 1).getDocuments { (snapshot, error) in
        guard let snapshot = snapshot else {
            if let error = error {
                print(error)
            }
            completion(nil)
            return
        }
        for doc in snapshot.documents {
            if let name = doc.get("shopName") as? String,
                let count = doc.get("count") as? String {
                let shop = Shop(name: name, count: count)
                temp.append(Shop)
            }
        }
        completion(temp)
    }
}
您可以在此完成处理程序中返回
结果
类型,但在本例中,我选择了
Shop
类型的可选数组(只是为了展示灵活性)。如果该方法返回
nil
,则表示有错误,否则数组中要么有商店,要么没有商店。我也不知道你是在寻找一个商店还是多个商店,因为在你的一些代码中,你似乎想要一个,而在代码的其他部分,你似乎想要多个

findFavouriteShops { (shops) in
    if let shops = shops {
        if shops.isEmpty {
            print("no error but no shops found")
        } else {
            print("shops found")
        }
    } else {
        print("error")
    }
}
删除orderBy()和limit()并查看是否可以获取任何文档