In the following function, it seems like the flow of events is going crazy! If anyone could help me understand what's going on, I'd really appreciate it!
Instead of printing 1, 2, 3, etc like I would expect, it's printing things in the following order:
1
14
2
3
4
...
12
13
5
6
7
8
...
9
8
...
7
8
...
10
11
5
6
7
8
...
10
11
func fetchRecipes(query: String) {
/
UIApplication.shared.isNetworkActivityIndicatorVisible = true
var finalRecipes = [Recipe]()
let owned = self.loadIngredients()
var count = 0
print("1")
viewModel.recipes(matching: query) { recipes in
print("2")
DispatchQueue.main.async {
print("3")
for item in recipes {
print("4")
self.viewModel.recipe(id: item.recipeId) { recipe in
print("5")
DispatchQueue.main.async {
print("6")
let ingredients = recipe?.ingredients
for ingredient in ingredients! {
print("7")
for thing in owned! {
print("8")
if (ingredient.localizedCaseInsensitiveContains(thing.name)) {
count += 1
print("9")
}
}
}
if true {
print("10")
}
print("11")
count = 0
}
}
}
print("12")
self.insertRecipes(recipes: finalRecipes)
UIApplication.shared.isNetworkActivityIndicatorVisible = false
self.loadingMore = false
}
print("13")
}
print("14")
}
func insertRecipes(recipes: [Recipe]) {
if recipes.count > 0 {
self.tableView.beginUpdates()
viewModel = viewModel.added(recipes: recipes)
var indexPathsToInsert = [IndexPath]()
let start = self.tableView.numberOfRows(inSection: 0)
let end = start + recipes.count - 1
for i in start...end {
indexPathsToInsert += [IndexPath(row: i, section: 0)]
}
self.tableView.insertRows(at: indexPathsToInsert, with: .automatic)
self.tableView.endUpdates()
}
}
}
Thanks so much!