I'm trying to abstract some components into smaller parts. For this I created the following listing:
struct ArticleList: View {
var fetchRequest: FetchRequest<Article>
var results: FetchedResults<Article> { fetchRequest.wrappedValue }
init() {
fetchRequest = FetchRequest<Article>(
entity: Article.entity(),
sortDescriptors: []
)
}
var body: some View {
ForEach(results) { article in
Text(article.name ?? "")
}
}
}
Now I have a container, which will display the list component, plus some additional things if conditions inside the child components are met:
struct Container: View {
var body: some View {
let articleList = ArticleList2()
return Group {
if articleList.results.isEmpty {
Text("Add")
}
articleList
}
}
}
My problem is now that the code crashes with the following exception:
Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
Debugging this further the console provides me the following feedback:
(lldb) po self.results
error: warning: couldn't get required object pointer (substituting NULL): Couldn't load 'self' because its value couldn't be evaluated
Debugging po self.fetchRequest
works and it contains an instance of the FetchRequest<Article>
instance. po self.fetchRequest.wrappedValue
provides the same error as self.results
above.
Does anyone has an idea why this code is crashing and what a possible workaround could be?
Thanks.