android - WorkManager 做一个异步工作
问题描述
我有一个 Android 聊天应用程序,由于我在业务模型中存在一些连接问题,我希望有一个执行以下操作的定期任务。
class PendingMessagesWorker(
appContext: Context,
params: WorkerParameters
) : CoroutineWorker(appContext, params), KoinComponent {
override suspend fun doWork(): Result = coroutineScope {
// 1. connect to a Socket synchronously
// 2. if connected then send a socket message that requests
// from server to response with pending messages
// 3. IF and WHEN server responds, handle the new messages
Result.success()
}
}
问题是,在步骤 2 - 3 之间,我应该在请求中添加回调还是添加延迟(x 秒)以使工作人员“等待”结果?
如果我在请求中添加回调,工人不是要完成并销毁所有线程吗?
解决方案
推荐阅读
- c++ - 如何在通用 inttype 上设置/检查 msb?
- excel - 将值传输到范围底部的循环错误地覆盖了值
- winforms - 无法从 Azure 逻辑 App Monitoring Rest Api 检索超过 30 个工作流运行详细信息
- php - 防止通过 AJAX 调用的 MySQL 存储过程出现“Commands Out of Sync”错误
- c# - 从通用接口转换为对象返回 null
- opencv - 图像配准的边缘/结构匹配
- buildroot - Buildroot 构建问题
- java - 链接多个 CompletableFuture 调用,每个调用都有自己的异常逻辑是一种好习惯吗?
- c# - 将对象对齐到中心
- java - Jenkins 级别的 ant 插件将使用系统或自定义 java home