当前位置:网站首页>One line of code to solve CoreData managed object properties change in SwiftUI problem of animation effects
One line of code to solve CoreData managed object properties change in SwiftUI problem of animation effects
2022-07-31 23:37:00 【Giant panda Hou Pei】

概述
在CoreData后台支持的SwiftUI项目中,为了简化实现,We can directly use the properties of the managed object asSwiftUIView state change driver.
不过,Doing so may result in no animation when managed object properties change:

如上图所示,The expansion of the notification time panel view is made byCoreDataTriggered by a property change of a managed object,But we even use withAnimation Block wrap property change code,still no animation.
如何解决呢?
其实,只需一行代码即可搞定!
So,别再等待,Let‘s Fix It!
一行代码!
首先,来看一下源代码:
extension Item {
@nonobjc public class func fetchRequest() -> NSFetchRequest<Item> {
return NSFetchRequest<Item>(entityName: "Item")
}
@NSManaged public var enableNotify: Bool
}
struct ItemInfoView: View {
@EnvironmentObject var item: Item
var body: some View {
VStack {
Button(action: {
withAnimation {
// Change managed objectsitem的enableNotify属性
item.enableNotify.toggle()
}
model.moc.saveIfNeed()
}){
Image(systemName: "bell.circle")
.foregroundColor(item.enableNotify ? .blue : .gray)
}
if item.enableNotify {
// Show reminder time panel...
}
}
}
}
虽然我们将item对象用 @EnvironmentObject 修饰,Hope to have animation feedback,but nothing actually happened.
其实,遇到这种情况,We just need to explicitly send the change notification to the managed object itself.
因为CoreDataAny managed class ofObservableObject协议,Of its instance contains a namedobjectWillChangePublisher object of,we just let it send the message,即可驱动SwiftUIView refresh to animate:
Button(action: {
withAnimation {
item.enableNotify.toggle()
// Explicitly refresh the interface,触发动画
item.objectWillChange.send()
}
model.moc.saveIfNeed()
}){
Image(systemName: "bell.circle")
.foregroundColor(item.enableNotify ? .blue : .gray)
}
现在,Let's take a look at the operation effect:

The long-lost animation is back again,棒棒哒!
题外话
The problem described in this blog post is simple,很快就搞定了!
Let's talk againXcode开发CoreDataTips for the project.
一般来说,我们在Xcode中对于CoreDataManaged classes always take an auto-generated configuration method(即Class Definition):

这时,XcodeWill automatically generate the definition of the managed class for us,Some friends may want to take a look at the code that actually generates the managed class,却不知何去何从.
这里教大家一招,按住option键,Left mouse button click on any managed object class name,Then click the blue button at the bottom of the pop-up floating window. 托管类名+CoreDataClass.swift 文本:

此时,Xcodewill jump to the definition file of the managed class:

最后,鼠标右键点击XcodeThe filename at the top of the managed class file window,can show its actual storage path:

Click on the corresponding catalog item,即可在Finder中打开.里面存放着所有XcodeAutomatically generated managed class definition file.
总结
在本篇博文中,We solved it with just one line of codeCoreDataManaged object property changes fail to triggerSwiftUIView animation problem;We've also covered how to quickly viewXcode自动生成CoreDataManaged class definition file.
Do you guys think it's easy??
最后,感谢大家观赏,再会!
边栏推荐
猜你喜欢

编译型语言和解释型语言的区别

【ACM】2022.7.31训练赛

《ArchSummit:时代的呐喊,技术人听得到》

Payment module implementation

基于simulink的Active anti-islanding-AFD主动反孤岛模型仿真

Handwritten a simple web server (B/S architecture)

ICML2022 | 深入研究置换敏感的图神经网络

C# Rectangle basic usage and picture cutting

Network security - crack WiFi through handshake packets (detailed tutorial)

(26) About menu of the top menu of Blender source code analysis
随机推荐
MySQL数据库‘反斜杠\’ ,‘单引号‘’,‘双引号“’,‘null’无法存储
标段参数说明
内核对设备树的处理
浏览器下载快捷方式到桌面(PWA)
硬件设备计算存储及数据交互杂谈
【Acwing】The 62nd Weekly Game Solution
LeetCode 第 304 场周赛
Interview assault 69: TCP reliable?Why is that?
Difference Between Stateless and Stateful
消息队列消息存储设计(架构实战营 模块八作业)
Unity-LineRenderer显示一条线
lua入门案例实战123DIY
SQL27 View user details of different age groups
/etc/resolv.conf的作用
How to import a Golang external package and use it?
面试突击69:TCP 可靠吗?为什么?
什么是客户画像管理?
[QNX Hypervisor 2.2 User Manual]9.14 set
The difference between adding or not adding the ref keyword when a variable of reference type is used as a parameter in a method call in C#
/etc/sysconfig/network-scripts configure the network card