简体   繁体   English

暂停正在运行的协程

[英]Suspend a running Coroutine

I can suspend a coroutine by calling suspendCoroutine<String> { cont -> continuation = cont } inside a coroutine but how do i suspend it from outside of coroutine and get a continuation back to resume it later?我可以通过在协程内部调用suspendCoroutine<String> { cont -> continuation = cont }来挂起协程,但是如何从协程外部挂起它并让后续继续恢复它?

Edit:编辑:

The job of coroutine is to receive json messages from server using jackson streaming api and store them using sqlite and also notify activities of new message if callbacks are not null (activities are running) The job of coroutine is to receive json messages from server using jackson streaming api and store them using sqlite and also notify activities of new message if callbacks are not null (activities are running)

Now when the service (which holds coroutine) is already running and updating database, if an activity connects to it, i want activity to first fetch all the messages from database and then continue receiving new messages from service.现在,当服务(包含协程)已经在运行并更新数据库时,如果活动连接到它,我希望活动首先从数据库中获取所有消息,然后继续从服务接收新消息。

Problem:问题:

  • If i let coroutine run while activity is synchronizing messages from database, new messages might not be synchronized fully/properly or end up in the middle of old messages, reason i want coroutine to be paused until synchronization is completed.如果我让协程在活动从数据库同步消息时运行,新消息可能无法完全/正确同步或最终在旧消息中间,原因我希望协程暂停直到同步完成。

  • I cannot check if i should suspendCoroutine before jackson starts waiting for new message, it will quickly pass this condition.在 jackson 开始等待新消息之前,我无法检查是否应该暂停协程,它会很快通过这个条件。

  • I cannot check if i should suspendCoroutine while jackson is waiting for new messages from network (blocked on read), it cannot be done.jackson 正在等待来自网络的新消息(读取时被阻止)时,我无法检查是否应该暂停协程,这是无法完成的。

  • I cannot check if i should suspendCoroutine after jackson has received a message because then I will have to wait for a new message to start synchronization.在 jackson 收到消息后,我无法检查是否应该暂停协程,因为那时我将不得不等待新消息开始同步。

Edit2:编辑2:

I think best solution is to just start synchronization and add a check if activity is already synchronizing after data is received and before any callbacks are made or data is stored to database , that way, coroutine can freely block on read and not insert new data until activity is finished synchronizing.我认为最好的解决方案是开始同步并添加一个检查活动是否已经在接收到数据之后以及在进行任何回调或将数据存储到数据库之前进行同步,这样,协程可以自由地阻塞读取而不插入新数据,直到活动完成同步。

I assume you are opening two different coroutines instead of one, which gives you 0 control to which one runs first:我假设您正在打开两个不同的协程而不是一个,这使您可以 0 控制哪个协程首先运行:

You could just do all the work inside one coroutine and the execution of it would be as imperative programming is:你可以在一个协程中完成所有工作,它的执行就像命令式编程一样:

someScope.launch(someDispatcher){
  val dataFromDb = fethDbData()
  if(dataFromDb.size == expectedSizeOrSomething){
     //start api call
     val apiResponse = response.fetchNetworkData()
     if (response.isSuccessful){
       parsePerhaps()
       insertNewDataToDatabase()
      }
   }
}

That should be it, by the description you make.根据您的描述,应该是这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM