簡體   English   中英

顯示來自 CoreData 的數據

[英]Show Data from CoreData

我有一個使用 CoreData 數據庫的 SwiftUI 項目,其中包含一個 Product 實體。 它有 2 個屬性,id 和條形碼。 然后我創建了一個似乎有效但會導致空 [Products] 的獲取請求。

當視圖出現在屏幕上時,我收到錯誤

線程 1:ForEach 上的 EXC_BREAKPOINT(代碼 = 1,子代碼 = 0x1df9bda98) ,沒有其他信息。

我試圖讓它盡可能簡單,但我仍然有問題。 我試圖完全刪除 xcdatamodel 文件並重新創建它,但我仍然遇到問題。

private struct ProductList: View {
@FetchRequest(
    entity: Product.entity(),
    sortDescriptors: []
) var products: FetchedResults<Product>


var body: some View {
    VStack {
        List{
            ForEach(products, id: \.id) { product in
                ProductRow(product: product)
            }
        }
    }

}

}

private struct ProductRow: View {
var product: Product

var body: some View {
    Text(product.barcode ?? "No name given")
}

}

有問題的代碼的屏幕截圖

在使用@FetchRequest之前,您必須首先將 Core Data 托管對象上下文注入環境,否則您的products不是空的,而是 nil

有關更多詳細信息,請參閱https://github.com/andrewcbancroft/BlogIdeaList-SwiftUI/tree/master/BlogIdeaList-SwiftUI

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM