当前位置:网站首页>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??
最后,感谢大家观赏,再会!
边栏推荐
- 【FPGA教程案例43】图像案例3——通过verilog实现图像sobel边缘提取,通过MATLAB进行辅助验证
- 如何撰写出一篇优质的数码类好物推荐文
- /etc/sysconfig/network-scripts 配置网卡
- 什么是动态规划,什么是背包问题
- SVN server construction + SVN client + TeamCity integrated environment construction + VS2019 development
- [1161. The maximum sum of elements in the layer]
- Program processes and threads (concurrency and parallelism of threads) and basic creation and use of threads
- C# Rectangle basic usage and picture cutting
- Components of TypeScript
- Weekly Summary
猜你喜欢
程序进程和线程(线程的并发与并行)以及线程的基本创建和使用
Program processes and threads (concurrency and parallelism of threads) and basic creation and use of threads
(26)Blender源码分析之顶层菜单的关于菜单
新产品如何进行网络推广?
嵌入式开发没有激情了,正常吗?
【Acwing】第62场周赛 题解
C# Rectangle基本用法和图片切割
Google Earth Engine——Error: Image.clipToBoundsAndScale, argument ‘input‘: Invalid type的错误解决
游戏安全03:缓冲区溢出攻击简单解释
Flink 1.13(八)CDC
随机推荐
「APIO2010」巡逻 题解
NgRx 里 first 和 take(1) 操作符的区别
程序进程和线程(线程的并发与并行)以及线程的基本创建和使用
SQL注入 Less46(order by后的注入+rand()布尔盲注)
#yyds干货盘点# 面试必刷TOP101:链表中环的入口结点
How to import a Golang external package and use it?
[Reading Notes -> Data Analysis] 02 Data Analysis Preparation
(26)Blender源码分析之顶层菜单的关于菜单
MySQL数据库‘反斜杠\’ ,‘单引号‘’,‘双引号“’,‘null’无法存储
新产品如何进行网络推广?
[QNX Hypervisor 2.2用户手册]9.16 system
Flutter教程之四年开发经验的高手给的建议
Unity-LineRenderer显示一条线
SQL injection Less54 (limited number of SQL injection + union injection)
vim的基本使用-底行模式
hboot与recovery、boot.img、system.img
vim的基本使用-命令模式
I don't know what to do with sync issues
基于单片机GSM的防火防盗系统的设计
VOT2021 game introduction