我正在使用这样的枚举传递coredata模型(config
)的对象
ItemModel
但是当我尝试在保存并关闭父视图未更新的视图之后使用托管对象上下文更新.sheet(item: $activeSheet, onDismiss: { self.activeSheet = nil }) { item in
switch item {
case .newDoc:
NewDocumentView(ref: nil).environment(\.managedObjectContext, context)
case .detailFolder(let data):
DetailedFolderView(item: data, dirName: data.wrappedName, updater: viewUpdater).environment(\.managedObjectContext, context)
case .detailDoc(let file):
DetailDocumentView(item: file, fileName: file.wrappedItemName)
.environment(\.managedObjectContext, context)
}
}
中的ItemModel
对象时,该视图仅在重新启动应用程序之后更新>
在更新DetailDocumentView
中传递的coredata模型(ItemModel
)的对象之后,谁能建议一种更新父视图的方法。欢迎任何建议:)
在这个问题上停留了几个小时,尝试使用可观察的对象,尝试在上下文更新时使用发布者,但是到目前为止,它仍然起作用:(
在文件中点击以显示DetailDocumentView
DetailDocumentView
我以前用来展示多张纸的枚举案例如下
/// creates a grid to display data
/// - Parameters:
/// - geometry: setup dimension for the grid view
/// - folders: files required
/// - Returns: returns a grid
private func gridView(_ geometry: GeometryProxy, files: FetchedResults<ItemModel>) -> some View {
QGrid(files,
columns: 2,
columnsInLandscape: 0,
vSpacing: 20,
hSpacing: 35,
vPadding: 10,
hPadding: 10,
isScrollable: false,
showScrollIndicators: false) { file in
FileItemView(item: file)
.environment(\.managedObjectContext, context)
.onTapGesture {
self.activeSheet = .detailDoc(file: file)
}
}
}
我在父类中这样声明它:
/// for presenting sheets
enum HomeViewActiveSheet: Any {
case newDoc
case detailFolder(folder: FolderModel)
case detailDoc(file: ItemModel)
}
// so that it can be used in the .sheet init
extension HomeViewActiveSheet: Identifiable {
var id: ObjectIdentifier {
ObjectIdentifier(Self.self)
}
}