swiftswift-data

SwiftData Fetch Specific Object With UUID


I am trying to fetch specific book from DB i try a lot of things on net but every time i get more errors. The think i dont understand is why this is so hard ? Just fetch an object with id.

@Model
final class Book: Identifiable {
    var id = UUID()
    var name: String
    var category: Category?
    var author: Author?
    var printDate: String
    var printLenght: Int
    var language: String
    var currentPage: Int = 0
    var isbn: String?
    
    var isFavorite: Bool = false
    var isBookmark: Bool = false
    
    @Relationship(deleteRule: .nullify, inverse: \Category.books)
    @Relationship(deleteRule: .nullify, inverse: \Author.books)
    
    init(name: String, printDate: String, printLenght: Int, language: String) {
        self.name = name
        self.printDate = printDate
        self.printLenght = printLenght
        self.language = language
    }
}

This is my model in swiftUI app i just want to fetch a Book object i created earlier.


Solution

  • I fix my problem with UUID String.

    {$0.id.contains(read.bookId)}

    When I change the UUID to UUID String I can now use it to fetch a specific book. I don't know how safe is that but for now i have no complains.