Scooby6
Scooby6

Reputation: 21

Key path value type 'FetchedResults<NotesHeader>' cannot be converted to contextual type 'FetchRequest<NotesHeader>'

I am new to SwiftUI (using XCode 14) I get the error after refactoring my code, taking logic out of my view (which used to work) XCode does not present me with an error on any particular line (so I know exactly which line is at fault) but rather displays the error looking at the failed build log.

Totally lost and any advice would be appreciated.

The only lines I can think of maybe causing the issue is the declaration of the fetch happening in my class ...

    @Published @FetchRequest(sortDescriptors: [], predicate: NSPredicate(format: "favourite == %@", true as NSNumber), animation: Animation.default) var favouriteNotes: FetchedResults<NotesHeader>
    @Published @FetchRequest(sortDescriptors: [], predicate: NSPredicate(format: "favourite == %@", false as NSNumber), animation: Animation.default) var notes: FetchedResults<NotesHeader>

Again, totally new to SwiftUI ... totally lost ... any help would be appreciated.

Upvotes: 1

Views: 409

Answers (1)

Scooby6
Scooby6

Reputation: 21

Got this sorted now ...

Trying to refactor the code lead to issues on something like ...

@Published @FetchRequest(sortDescriptors: [], predicate: NSPredicate(format: "favourite == %@", true as NSNumber), animation: Animation.default) var favouriteNotes: FetchedResults<NotesHeader>
@Published @FetchRequest(sortDescriptors: [], predicate: NSPredicate(format: "favourite == %@", false as NSNumber), animation: Animation.default) var notes: FetchedResults<NotesHeader>

What I didn't realise at the time is that while that code (without the @Publish) of course would work in a view ... it DOES NOT work that way when fetching core data in a class.

A YouTube video by Swiftful Thinking got me on the right track and now all is well. Link to the video: https://www.youtube.com/watch?v=BPQkpxtgalY&t=613s

Best of luck

Upvotes: 1

Related Questions