I have a SwiftUI view where the bulk of the logic displays a list of recipes with a LazyVGrid
. The code looks something like this:
struct RecipeGrid: View {
let recipes: [Recipe]
var body: some View {
return LazyVGrid {
ForEach(countries) { country in
Text(recipe.title)
}
}
}
}
This is the hello world of SwiftUI. Straightforward stuff. Two real-world requirements make things a bit more interesting/challenging.
First, we have over 20,000 recipes. So we don't want to pass everything in as an array, as suggested by 1 and 2.
Second, there are two different ways we get the recipes
.
The first is to show the most popular recipes using a FetchRequest
. It looks something like this:
@FetchRequest(sortDescriptors: [SortDescriptor(\.likes, order: .reverse)])
private var recipes: FetchedResults<Recipe>
The second is to get all recipes using the same ingredient. There is a ManyToMany relationship between Ingredient
and Recipe
. The declaration of Ingredients
looks like this:
class Ingredient: Entity {
@NSManaged var recipes: NSOrderedSet
}
I am wondering what would be the recommended way to declare the recipes
inside RecipeGrid
so that it will support both the use cases above.
I have tried the following
struct RecipeGrid: View {
let recipes: RandomAccessCollection
...
}
The above doesn't work. Swift gives the following error message: Protocol 'RandomAccessCollection' can only be used as a generic constraint because it has Self or associated type requirements
.
What is the best practice way to approach this? I guess another way to ask my question is:
How do I declare the associated type for RandomAccessCollection?