在HarmonyOS NEXT开发中@Sendable怎么和@ObservedV2协同工作?

在HarmonyOS NEXT开发中@Sendable怎么和@ObservedV2协同工作?

阅读 624
1 个回答

当前在class上,是不能支持@Sendable与@ObservedV2的,就是一个对象,不能既是多线程共享对象,也是可观察对象
基于当前@Sendable与@ObservedV2不可混用的问题,本处提供两种解决方案:
1.Sendable对象整体跟踪,示例代码:

import taskpool from '@ohos.taskpool'; 
import { SendableData } from './SendableData'; 
 
@Concurrent 
function threadGetData(param: string): SendableData { 
  let ret = new SendableData() 
  ret.name =param + "-o" 
  ret.age = Math.floor(Math.random()*40) 
  ret.likes = Math.floor(Math.random()*100) 
  return ret // Here return a new SendableData 
} 
 
 
@ObservedV2 
class ViewModelWithSendable { 
  @Trace data?: SendableData // 加@Trace可以整体换掉这个对象 
  @Trace sendParam: string = "chen" 
 
  runTask() { 
    taskpool.execute(threadGetData, this.sendParam).then((value: object) => { 
      this.data = value as SendableData // 整个替换数据 
      this.sendParam = this.data.name 
    }) 
  } 
} 
 
@Entry 
@Component 
struct Index { 
  mySendData: ViewModelWithSendable = new ViewModelWithSendable() 
 
  build() { 
    Column() { 
      Text(`SendParam=${this.mySendData.sendParam}`).fontSize(30).fontColor(Color.Red) 
      Button("runTask").onClick(this.mySendData.runTask.bind(this.mySendData)) 
      if (this.mySendData.data) { 
        Row() { 
          Text(this.mySendData.data.name).fontSize(30).layoutWeight(4) 
          Text(this.mySendData.data.age.toString()).fontSize(30).fontColor(Color.Blue).layoutWeight(1) 
          Text(this.mySendData.data.likes.toString()).fontSize(30).fontColor(Color.Green).layoutWeight(1) 
          Text(this.mySendData.data.follow ? "已关注" : "关注").fontSize(30).fontColor(Color.Green).layoutWeight(2) 
        } 
      } 
    } 
  } 
}

2.Sendable对象局部更新,示例代码:

import taskpool from '@ohos.taskpool'; 
import { SendableData } from './SendableData'; 
 
@Concurrent 
function threadLike(param: SendableData): boolean { 
  param.likes++ 
  return true 
} 
 
@Concurrent 
function threadFollow(param: SendableData): boolean { 
  param.likes++ 
  return true 
} 
 
@Concurrent 
function threadReset(param: SendableData): boolean { 
  let likes: number = param.likes 
  // ..... 
  return true 
} 
 
 
@ObservedV2 
class ViewModelWithSendable { 
  data: SendableData = new SendableData() // 无需跟踪整体变更 
  @Trace sendParam: string = "chen" 
  @Trace likes: number = 0 // 定义响应式数据 
  @Trace follow: boolean = false // 定义响应式数据 
 
  iLike() { 
    taskpool.execute(threadLike, this.data).then((value: object) => { 
      this.likes = this.data.likes // 仅对变更属性进行赋值,用于UI刷新 
    }) 
  } 
 
  iFollow() { 
    taskpool.execute(threadFollow, this.data).then((value: object) => { 
      this.follow = this.data.follow // 仅对变更属性进行赋值,用于UI刷新 
    }) 
  } 
  reset() { 
    // 先变更数据,界面随后更新,再使用线程去工作 
    this.likes = 0 
    this.data.likes = this.likes 
    this.follow = false 
    this.data.follow = this.follow 
    taskpool.execute(threadReset, this.data) 
  } 
} 
 
@Entry 
@Component 
struct Index { 
  mySendData: ViewModelWithSendable = new ViewModelWithSendable() 
 
  build() { 
    Column() { 
      Row() { 
        Button("Like").onClick(this.mySendData.iLike.bind(this.mySendData)) 
        Button("Follow").onClick(this.mySendData.iFollow.bind(this.mySendData)) 
        Button("Reset").onClick(this.mySendData.reset.bind(this.mySendData)) 
      } 
 
      Row() { 
        Text(this.mySendData.data.name).fontSize(30).layoutWeight(4) 
        Text(this.mySendData.data.age.toString()).fontSize(30).fontColor(Color.Blue).layoutWeight(1) 
        Text(this.mySendData.data.likes.toString()).fontSize(30).fontColor(Color.Green).layoutWeight(1) 
        Text(this.mySendData.data.follow ? "已关注" : "关注").fontSize(30).fontColor(Color.Green).layoutWeight(2) 
      } 
    } 
  } 
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
logo
HarmonyOS
子站问答
访问
宣传栏