当前位置:网站首页>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??
最后,感谢大家观赏,再会!
边栏推荐
- uniapp小程序检查、提示更新
- [QNX Hypervisor 2.2 User Manual]9.14 set
- Network security - crack WiFi through handshake packets (detailed tutorial)
- [QNX Hypervisor 2.2 User Manual]9.16 system
- 消息队列存储消息数据的MySQL表格
- /etc/resolv.conf的作用
- SQL injection Less46 (injection after order by + rand() Boolean blind injection)
- MySQL数据库‘反斜杠\’ ,‘单引号‘’,‘双引号“’,‘null’无法存储
- When can I use PushGateway
- 对象缓存服务的思考和实现
猜你喜欢
[Cloud Residency Co-Creation] [HCSD Big Celebrity Live Broadcast] Personally teach the secrets of interviews in big factories
【MATLAB项目实战】LDPC-BP信道编码
C#中引用类型的变量做为参数在方法调用时加不加 ref 关键字的不同之处
Unity-通过预制件和克隆方法动态实现各个UGUI下控件的创建和显示
高等代数_证明_任何矩阵都相似于一个上三角矩阵
Document management and tools in the development process
C# Rectangle basic usage and picture cutting
什么是客户画像管理?
不知道该怎么办的同步问题
基于mysql的消息队列设计
随机推荐
When can I use PushGateway
Learn about C# anonymous methods
Interview assault 69: TCP reliable?Why is that?
面试突击69:TCP 可靠吗?为什么?
IPD流程专业术语
简单的vim配置
基于simulink的Passive anti-islanding-UVP/OVP and UFP/OFP被动反孤岛模型仿真
C# Rectangle basic usage and picture cutting
网络安全--通过握手包破解WiFi(详细教程)
信奥学习规划 信息学竞赛之路(2022.07.31)
TypeScript 的组件
Input and output optimization
vim的基本使用概念
Flutter教程之四年开发经验的高手给的建议
Dry goods | 10 tips for MySQL add, delete, change query performance optimization
mysql having的用法
I don't know what to do with sync issues
Thinking and Implementation of Object Cache Service
"SDOI2016" Journey Problem Solution
Document management and tools in the development process