模型 returns 数据后未在 didSelectRowAt 中分配变量

Variable not being assigned in didSelectRowAt after model returns data

我有以下 Model returns 数据到我的 View Controller:

    func getRecipeSelected(docId: String, completionHandler: @escaping () -> Void) {
        db.collection("recipes").document(docId).getDocument { document, error in
            if let error = error as NSError? {
            }
            else {
                if let document = document {
                    do {
                        self.recipe = try document.data(as: Recipe.self)
                        
                        let recipeFromFirestore = Recipe(
                            id: docId,
                            title: self.recipe!.title ?? "",
                            analyzedInstructions: self.recipe!.analyzedInstructions!)
                        
                        DispatchQueue.main.async {
self.delegateSpecificRecipe?.recipeSpecificRetrieved(recipeSelected: recipeFromFirestore)
                        }
                    }
                    catch {
                        print("Error: \(error)")
                    }
                }
            }
        }
        completionHandler()
    }

这是我的 View Controller:

    var entireRecipe: Recipe? = nil

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        documentID = recipeDocIdArray[indexPath.row]
        
        model.getRecipeSelected(docId: documentID) {
            print("ISSUE HERE: \(self.entireRecipe)") // FIXME: THIS IS NIL THE FIRST TIME IT IS CALLED
        }
    }

我的问题是 entireRecipe 没有从我的视图控制器的完成处理程序中的 model 分配数据。如果我点击单元格 时间,那么第一次点击的数据将分配给该完成处理程序。

如何在 第一次 时将返回的数据分配给该范围内的 entireRecipe

您正在调用委托方法而不是调用 completionHandler。而且您还在异步块中调用委托方法,该方法在 completionHandler 之后调用。不需要连续两个。您可以像这样使用 completionHandler:

func getRecipeSelected(docId: String, completionHandler: @escaping (Recipe?) -> Void) {
        db.collection("recipes").document(docId).getDocument { document, error in
            if let error = error as NSError? {
            }
            else {
                if let document = document {
                    do {
                        self.recipe = try document.data(as: Recipe.self)
                        
                        let recipeFromFirestore = Recipe(
                            id: docId,
                            title: self.recipe!.title ?? "",
                            analyzedInstructions: self.recipe!.analyzedInstructions!)
                        
                        completionHandler(recipeFromFirestore)
                    }
                    catch {
                        print("Error: \(error)")
                        completionHandler(nil)
                    }
                }
            }
        }
    }
var entireRecipe: Recipe?

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    documentID = recipeDocIdArray[indexPath.row]
        
    model.getRecipeSelected(docId: documentID) { [weak self] model in
        self?.entireRecipe = model
    }
}