SwiftUI预览-意外数据 [英] SwiftUI Previews - unexpected data
问题描述
我想了解如何在SwiftUI Canvas Preview中使用示例数据.我进行了一些搜索,但没有找到解决问题的答案.
I would like to understand how to work with example data in SwiftUI Canvas Preview. I have done some searching, but didn't find answer to my problem.
问题:
为什么我的 ItemView.swift 视图的预览显示了我提供的示例数据与在实时预览中创建的核心数据模型的混合?
Why Preview of my ItemView.swift view shows a mixture of example data I have provided + Core Data models created in Live Preview?
预览代码示例
struct ItemView_Previews: PreviewProvider {
static var previews: some View {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
//Test data
let testItem = Item.init(context: context)
testItem.name = "Abc"
return ItemView(filter: false)
.environment(\.managedObjectContext, context)
}
}
预期行为:我期望看到带有测试数据的UI预览.因此,换句话说,我期待看到"Abc"在预览中.
Expected behaviour: I was expecting to see UI preview with test data. So in other words, I was expecting to see "Abc" in Preview.
实际行为但是我看到的是混合项目列表:"Abc"重复几次.测试"重复几次(因为它是在实时预览中使用加号按钮创建的Core Data对象的item.name属性).
Actual behaviour But what I am seeing is a list of mixed items: "Abc" repeated few times. "Test" repeated few times (as it's item.name attribute of Core Data objects being created with plus button in Live Preview).
那是为什么?当我专门提供测试值(testItem.name ="Abc")时,为什么会看到Core Data对象?
Why is that? Why am I seeing Core Data objects, when I have specifically provided test values (testItem.name = "Abc")?
-
完整示例
ContentView.swift
import SwiftUI
import CoreData
struct ContentView: View {
@Environment(\.managedObjectContext) var managedObjectContext
@State var paidFilter :Bool? = nil
var body: some View {
NavigationView {
List {
ItemView(filter: paidFilter)
}
.listStyle(PlainListStyle())
.navigationTitle(Text("Items"))
.navigationBarItems(
trailing:
Button(action: {
let item = Item(context: self.managedObjectContext)
item.name = "Test"
do {
try self.managedObjectContext.save()
}catch{
print(error)
}
}) {
Image(systemName: "plus.circle.fill")
.font(.title)
}
)
}
.navigationViewStyle(StackNavigationViewStyle())
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
return ContentView()
.environment(\.managedObjectContext, context)
}
}
ItemView.swift
import SwiftUI
struct ItemView: View {
@Environment(\.managedObjectContext) var managedObjectContext
var fetchRequest: FetchRequest<Item>
var items: FetchedResults<Item> { fetchRequest.wrappedValue }
init(filter: Bool?) {
fetchRequest = FetchRequest<Item>(entity: Item.entity(), sortDescriptors: [])
}
var body: some View {
VStack {
ForEach(items, id: \.self) {item in
Text("\(item.name ?? "test123")")
}
}
}
}
struct ItemView_Previews: PreviewProvider {
static var previews: some View {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
//Test data
let testItem = Item.init(context: context)
testItem.name = "Abc"
return ItemView(filter: false)
.environment(\.managedObjectContext, context)
}
}
推荐答案
为什么我的ItemView.swift视图的预览显示了我提供的示例数据与在实时预览中创建的核心数据模型的混合?
Why Preview of my ItemView.swift view shows a mixture of example data I have provided + Core Data models created in Live Preview?
-
下面的代码行将为您的持久数据启动上下文,从而使所有要获取的已存储核心数据对象可用
the below line initiates context to your persistent data, so makes available all stored core data object to be fetched
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
下面的代码行在上面创建的上下文中创建了一个新对象,因此它处于上下文中(尽管已存储),并且可与其他已存储的对象一起获取.
the below line creates a new object in the context created above, so it is in context (though to stored) and available for fetching alongside of others stored.
let testItem = Item.init(context: context)
因此从此上下文中获取对象...
Thus fetching object from this context ...
return ItemView(filter: false)
.environment(\.managedObjectContext, context)
...使所有-存储和创建的-在预览中可用.
... makes all - stored and created - available in Preview.
这篇关于SwiftUI预览-意外数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!