关于以下问题,我把头撞在桌子上。我有一个嵌套的DataStore(为方便起见),通过@EnvironmentObject集成到视图中。如果我更改了数据存储区中的某些内容(例如,将一个新对象放入其中-效果很好),则UI不会更新。
在控制台中,我还获得了由于获取的结果发生更改而导致的print(“ Object Changed”)的结果。但是,只有在重新启动应用程序后,UI才会更新。怎么了?
非常感谢!
我的测试代码如下:
数据存储区:
import Foundation
import Combine
class DataStore : ObservableObject{
@Published var documents = DocumentStore()
@Published var attachments = AttachmentStore()
}
附件商店
//
// AttachmentStore.swift
import SwiftUI
import Combine
import CoreData
class AttachmentStore: NSObject, ObservableObject {
// MARK: Private Properties
let persistenceManager = PersistenceManager()
private lazy var fetchedResultsController: NSFetchedResultsController<ManagedAttachment> = {
let fetchRequest: NSFetchRequest<ManagedAttachment> = ManagedAttachment.fetchRequest()
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "dateCreated", ascending: false)]
let fetchedResultsController = NSFetchedResultsController(
fetchRequest: fetchRequest,
managedObjectContext: self.persistenceManager.managedObjectContext,
sectionNameKeyPath: nil,
cacheName: nil)
fetchedResultsController.delegate = self
return fetchedResultsController
}()
private var attachment: [ManagedAttachment] {
return fetchedResultsController.fetchedObjects ?? []
}
// MARK: Public Properties
@Published var Items : [ManagedAttachment] = []
public var allItems: [ManagedAttachment] {
return self.attachment.filter {$0 == $0}
}
func setup(){
self.Items = allItems
}
// MARK: Object Lifecycle
override init() {
super.init()
fetchDocuments()
setup()
}
// MARK: Public Methods
public func allAttachments(for id: UUID) -> [ManagedAttachment]
{
let attachments = self.allItems.filter {$0.parentID == id}
return attachments
}
public func create(attachment: Attachment) {
ManagedAttachment.create(object: attachment, in: self.persistenceManager.managedObjectContext)
saveChanges()
}
public func delete(attachment: ManagedAttachment){
persistenceManager.managedObjectContext.delete(attachment)
self.saveChanges()
}
public func update(){
self.saveChanges()
}
// MARK: Private Methods
private func fetchDocuments() {
do {
try fetchedResultsController.performFetch()
dump(fetchedResultsController.sections)
} catch {
fatalError()
}
}
private func saveChanges() {
guard persistenceManager.managedObjectContext.hasChanges else { return }
do {
try persistenceManager.managedObjectContext.save()
self.objectWillChange.send()
print("Object Changed")
} catch { fatalError() }
}
}
extension AttachmentStore: NSFetchedResultsControllerDelegate {
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
self.objectWillChange.send()
print("Object Changed")
}
}
TestView
import SwiftUI
struct TestView: View {
@EnvironmentObject var data : DataStore
var body: some View {
VStack{
Button(action: {
self.data.attachments.create(attachment: Attachment(url: "Test"))
}
) {
Text("Add")
}
// Print the Items in the DataStore
ForEach(data.attachments.Items, id:\.self){Item in
Text(Item.url).font(.caption)
}
}
}
}
答案 0 :(得分:0)
好的,我的问题是我从附件存储发送了willChange,但没有从数据存储发送,因此它没有更新。因此,解决方案是手动调用dataStore.objectWillChange.send()。谢谢!