此前今日事鸿蒙版卡片数据刷新还是有点问题。
问题:
安装应用后把卡片放到桌面,在app内新增一条数据,服务卡片不刷新,需要把app进程删除重新打开,才能正常刷新数据。
原因:
卡片渲染在系统进程,创建和调用都是系统触发,即EntryFormAbility里的代码其实运行在系统进程里面,跟app进程有内存隔离。没法做到数据实时共享。
懒得上图了,卡片交互流程直接上官网看吧。
这里主要解决问题。
需要通过harmony提供的跨进程通信模块
import commonEventManager from '@ohos.commonEventManager'
来把卡片id发给app内进行保存,才能保证在app内能实时拿到卡片id,才能保证数据能刷新到卡片上。
核心代码:
import commonEventManager from '@ohos.commonEventManager'
class SubscriberClass {
event_form_id = "form_id"
publishCount: number = 1
subscriber?: commonEventManager.CommonEventSubscriber
// 发布者
publish(eventType: string ,data:string){
// next中data支持导入类型
commonEventManager.publish(eventType,{data},(err)=>{
if(err){
// 失败只发3次
if(this.publishCou