Searching the database outside of the view

How do I perform a search using SwiftData within an observable data model

For example: If I have a database of books that contains the property isbn, how would I perform a search to confirm I either had or did not have a book with the isbn I am searching for?

Using CoreData I use a fetch request:

let predicate = NSPredicate(format: "%K == %@", #keyPath(Book.isbn), isbn)
let fetchRequest: NSFetchRequest<Book> = Book.fetchRequest()
...
let book = try context.fetch(fetchRequest). // returns [book] use isEmpty for not found.
...

if book.isEmpty {
   saveBook()
}

I have a number of queries I perform on the database to drive the logic within the app, this is just one example.

Post not yet marked as solved Up vote post of t-j-c Down vote post of t-j-c
474 views