HarmonyOS @Sendable怎么和@ObservedV2协同工作

HarmonyOS
9h前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
aquaa

当前在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)
      }
    }
  }
}
分享
微博
QQ
微信
回复
6h前
相关问题
HarmonyOS Sendable装饰的对象怎么获取
78浏览 • 1回复 待解决
ivanti CSA 是怎么工作的?
3444浏览 • 0回复 待解决
协同开发出错怎么处理啊?有懂的吗?
3304浏览 • 1回复 待解决
HarmonyOS V2V1的使用区别?
196浏览 • 0回复 待解决
HarmonyOS 2in1怎么禁止最大化
5浏览 • 0回复 待解决
HarmonyOS Sm2DES加解密问题
456浏览 • 1回复 待解决
HarmonyOS V2V1的明显区别?
230浏览 • 1回复 待解决