- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试通过 NSManagedObject 更新核心数据中的属性。一旦我更新它,我就会保存上下文并成功保存。
问题
上下文保存后,UI (SwiftUI) 不会使用新值更新它。如果我将一个全新的子项添加到核心数据(插入)中,UI 就会更新。
我尝试过的:
这是一个 SwiftUI 错误吗?原样状态
//only updating the data
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let fetchReq = NSFetchRequest<Card>(entityName: "Card") //filter distributorID; should return only one Card
fetchReq.resultType = .managedObjectResultType
fetchReq.predicate = NSPredicate(format: "id == %@", params["id"]!) //I get an array with cards
var cards :[Card] = []
cards = try context.fetch(fetchReq)
//some other functions
cards[0].counter = "3" //
//...
self.objectWillChange.send() //doesn't do anything?
try context.save()
//sucessfully, no error. Data is there if I restart the app and "force" a reload of the UI
//Core Data "Card"
extension Card :Identifiable{
@nonobjc public class func fetchRequest() -> NSFetchRequest<Card> {
return NSFetchRequest<Card>(entityName: "Card")
}
//...
@NSManaged public var id: String
//....
}
//Main SwiftUI - data is correctly displayed on the card
@FetchRequest(entity: Card.entity(),
sortDescriptors: [],
predicate: nil)
var cards: FetchedResults<Card>
List {
ForEach(cards){ card in
CardView(value: Int(card.counter)!, maximum: Int(card.maxValue)!,
headline: card.desc, mstatement: card.id)
}
最佳答案
如果第一个代码块是 ObservableObject 的一部分,那么它不会认为第三个代码块( View 一)依赖于它,并且如果依赖项没有更改,则 View 不会更新。
尝试this方法。
但是,如果存在未提供的依赖项,则将保存和发布的顺序更改为
try context.save()
self.objectWillChange.send()
关于ios - FetchedResults 不会触发和 SwiftUI 更新,但上下文会成功保存它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61843543/
我在实现扩展函数以确定实体是否包含任何结果时遇到了一些问题。最终目标是确定实体是否有结果,如果没有,则在应用程序启动时将其播种(对于某些实体,如国家/地区列表等...) 这是我能够得到的地方,但是 s
我有一个由 NSFetchedResultsController 管理的表格 View 。 我的问题是,我试图根据计算值过滤数据,这需要查看核心数据实体上的相关值。 为了提供进一步的上下文:我有一个名
我正在尝试通过 NSManagedObject 更新核心数据中的属性。一旦我更新它,我就会保存上下文并成功保存。 问题 上下文保存后,UI (SwiftUI) 不会使用新值更新它。如果我将一个全新的子
我在列表中有一些项目,我正在向其中添加滑动删除功能。使用删除功能时,我收到一条错误消息,告诉我 FetchedResults对象没有成员“删除”。发生什么事了? @Environment(\.mana
我有一个应用程序,当用户首次启动应用程序时,它会显示一个自定义的无数据单元格。 当用户输入第一个条目时,我的 fetchedResultsController 的 fetchedResults 会被更
我正在为我的 tableViewController 使用 FetchedResultController 的东西,一旦我得到结果,我就用获取的对象创建一个 NSArray self.sortedAr
我目前正在使用 CoreData FetchedResults 和 SwiftUIs PickerView。这样做时,我在使用 FetchedResults 填充 PickerView 时遇到了问题:
我有一个父 View ,它执行 @FetchRequest并通过 FetchedResults到 subview 。一切正常, subview 能够解析 FetchedResults .但是,我不知道
当您离开并返回时,SwiftUI FetchedResult View 无法更新。 我创建了一个简单的待办事项列表应用程序作为示例。该应用程序由 2 个实体组成: 一个 TodoList,可以包含许多
我是 SwiftUI 的新手,部分是 Swift,所以希望有人能对我明显缺少的东西有所了解。原谅乱码!我还没有机会重构。 我正在使用 @FetchRequest属性包装器拉出所有列表 Memories
我按照本教程学习了动态过滤器和 CoreData: https://www.hackingwithswift.com/quick-start/ios-swiftui/dynamically-filte
我只想将一个绑定(bind)传递到一个核心数据 - 实体到 ForEach 循环内的 SwiftUI SubView 屏幕,这样我就可以编辑该实体的属性,我可以保存我的上下文并获取自动更新的 View
我是一名优秀的程序员,十分优秀!