【问题标题】:For comprehension invoking Future method and returning Future type用于理解调用 Future 方法并返回 Future 类型
【发布时间】:2014-11-30 01:13:58
【问题描述】:

我正在寻找组织以下代码的最佳方法

这是 1.0 版,返回未来的最佳方式是什么

class ComparePrepMgr(factory:IFactoryBuilder) {

  val daoact = factory.actorFactory.getActor[DAOSupervisor] //Returns Option[IActorURI]
    def prepare(testplan:TestPlan) : Future[UnitofWorkRequest] = 
    {
    for (   dao <- daoact;
            testcase <- testplan.testcases;    //Returns testcase instance from list type
            sourceenvfut = getFuture[Vector[EnvironmentInfo]](dao, queryDAO[EnvironmentInfo](new DataQueryExp(ObjectIdEqOp("_id",testcase.sourceenv,false),None))) ;
            destenvfut = getFuture[Vector[EnvironmentInfo]](dao, queryDAO[EnvironmentInfo](new DataQueryExp(ObjectIdEqOp("_id",testcase.destenv ,false),None))) ;
            sourceobjfut = getFuture[Vector[ObjectInfo]](dao, queryDAO[ObjectInfo](new DataQueryExp(ObjectIdEqOp("_id",testcase.source,false),None))) ;
            destobjfut = getFuture[Vector[ObjectInfo]](dao, queryDAO[ObjectInfo](new DataQueryExp(ObjectIdEqOp("_id",testcase.destination ,false),None)));
            sourceenv <- sourceenvfut;destenv <- destenvfut; sourceobj <- sourceobjfut; destobj <- destobjfut 
        ) 
        {
            UnitofWorkRequest(testplan.copy(testcases = List()) , testcase, sourceenv.last, destenv.last, sourceobj.last,destobj.last)   
        }

这是代码的另一种变体,但它迫使我分解我的函数 b/c。我正在根据类似的返回类型安排理解,因此我对上面的代码感兴趣

class ComparePrepMgr(factory:IFactoryBuilder) {

  val daoact = factory.actorFactory.getActor[DAOSupervisor]
  def prepare(testplan:TestPlan) = 
  {
    for (   testcase <- testplan.testcases;
            dao <- daoact
        ) yield prepareFuture(testplan,dao,testcase)
  }

  def prepareFuture(testplan:TestPlan,dao:IActorURI,testcase:TestCaseInfo) = 
  {
    for (   sourceenv<- getFuture[Vector[EnvironmentInfo]](dao, queryDAO[EnvironmentInfo](new DataQueryExp(ObjectIdEqOp("_id",testcase.sourceenv,false),None)));
            destenv <- getFuture[Vector[EnvironmentInfo]](dao, queryDAO[EnvironmentInfo](new DataQueryExp(ObjectIdEqOp("_id",testcase.destenv ,false),None)));
            sourceobj <- getFuture[Vector[ObjectInfo]](dao, queryDAO[ObjectInfo](new DataQueryExp(ObjectIdEqOp("_id",testcase.source,false),None))) ;
            destobj <- getFuture[Vector[ObjectInfo]](dao, queryDAO[ObjectInfo](new DataQueryExp(ObjectIdEqOp("_id",testcase.destination ,false),None)))
        ) yield UnitofWorkRequest(testplan.copy(testcases = List()) , testcase, sourceenv.last, destenv.last, sourceobj.last,destobj.last)
  }
 }

感谢您在使 1.0 版正常工作方面的任何帮助。

【问题讨论】:

    标签: scala future scala-2.10


    【解决方案1】:

    如果我的问题没有正确表达,请接受我的道歉,我看到很多反对意见,我试图实现的是将代码封装在一个具有 Future 返回值的函数中,另外还试图将我的头包裹在 Future 框架上,所以看起来我终于能够在承诺的帮助下实现我所期待的。这是最终代码,如果将来有人碰巧在同一条船上

      def prepare(testplan:TestPlan) : Future[UnitofWorkRequest] = 
      {
              val unitworkprom  = promise[UnitofWorkRequest]
        for (   testcase <- testplan.testcases;
                dao <- daoact
            ) 
        {
            val f1 = getFuture[Vector[EnvironmentInfo]](dao, queryDAO[EnvironmentInfo](new DataQueryExp(ObjectIdEqOp("_id",testcase.sourceenv,false),None)));
            val f2 = getFuture[Vector[EnvironmentInfo]](dao, queryDAO[EnvironmentInfo](new DataQueryExp(ObjectIdEqOp("_id",testcase.destenv ,false),None)));
            val f3 = getFuture[Vector[ObjectInfo]](dao, queryDAO[ObjectInfo](new DataQueryExp(ObjectIdEqOp("_id",testcase.source,false),None))) ;
            val f4 = getFuture[Vector[ObjectInfo]](dao, queryDAO[ObjectInfo](new DataQueryExp(ObjectIdEqOp("_id",testcase.destination ,false),None)))
            for ( sourceenv <- f1; destenv <- f2; sourceobj <- f3 ; destobj <- f4)
            {
               unitworkprom.success(UnitofWorkRequest(testplan.copy(testcases = List()) , testcase, sourceenv.last, destenv.last, sourceobj.last,destobj.last))
            }
        }
        unitworkprom.future
      }
    

    所以在promise的帮助下,函数会立即返回空future,但是里面的代码为了理解,有4个future被并行调用会得到结果,然后使用promise成功函数设置最终会的值触发回调。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-08
      • 2021-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      相关资源
      最近更新 更多