Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala Future onComplete回调未立即执行_Scala_Future - Fatal编程技术网

Scala Future onComplete回调未立即执行

Scala Future onComplete回调未立即执行,scala,future,Scala,Future,我有一个未来[Future[Set[String]]。我将其平面映射以返回一个未来[Set[String]]。我等待无穷大(我知道这很糟糕)然后检查Future是否完整。它返回true。但是,当我获取Future返回的值并尝试将其添加到集合中时,它不会添加该值。首先打印回调末尾的语句。当我从方法返回集合时,尽管Future返回值,但该集合始终为空。我添加了注释以解释事件发生的顺序。如果我做错了,请告诉我!谢谢 val googleXfpSegments = Set[String]() val c

我有一个未来[Future[Set[String]]。我将其平面映射以返回一个未来[Set[String]]。我等待无穷大(我知道这很糟糕)然后检查Future是否完整。它返回true。但是,当我获取Future返回的值并尝试将其添加到集合中时,它不会添加该值。首先打印回调末尾的语句。当我从方法返回集合时,尽管Future返回值,但该集合始终为空。我添加了注释以解释事件发生的顺序。如果我做错了,请告诉我!谢谢

val googleXfpSegments = Set[String]()
val customCriteriaFuture: Future[List[Long]] = getCustomCriteriaValueIds(lineItemPage.getResults())
// getCustomCriteriaValues returns a Future[Set[String]]
val criteriaValueList = customCriteriaFuture.flatMap(criteriaIdList => getCustomCriteriaValues(criteriaIdList.toSet))
// I wait for infinite time
Await.result(criteriaValueList, scala.concurrent.duration.Duration.Inf)
log.info("Future Completed = " + criteriaValueList.isCompleted) // This returns true
criteriaValueList onComplete {
    case Success(segmentNames) => {
        // This statement gets printed after the "TEST" value gets printed
        log.info("SegmentNameList = " + segmentNames.mkString(","))
        googleXfpSegments ++= segmentNames
        // This prints nothing which means the segmentNames is not getting added
        log.info("Google Segments = " + googleXfpSegments.mkString(",")) 
    }
    case Failure(error) => log.error("Error occurred ", error)
}
googleXfpSegments += "TEST"
// This prints TEST
log.info("Google Segments = " + googleXfpSegments.mkString(",")) 
日志输出如下所示:

[info] [INFO] [2014-12-09 17:25:01,147] [service.GoogleXFPService] Future Completed = true 
[info] [INFO] [2014-12-09 17:25:01,148] [service.GoogleXFPService] Google Segments = TEST 
[info] [INFO] [2014-12-09 17:25:01,148] [service.GoogleXFPService] SegmentNameList = vt.cm,vt.cp,ex.qy,ex.ahe,cm.gannett_trav

您需要将
wait.result(criteriaValueList,scala.concurrent.duration.duration.Inf)
的输出分配给一个值。当前您正在等待它完成,对输出不做任何操作,然后将一个onComplete回调附加到一个已经完成的将来,然后在回调运行之前程序终止

val segmentNames = Await.result(criteriaValueList, scala.concurrent.duration.Duration.Inf)
log.info("Google Segments = " + segmentNames.mkString(",")) 

标题中提到的
onComplete
回调在哪里?@m-z我编辑了这篇文章。无论我使用onComplete还是foreach,它都不会先执行Success块。
googleXfpSegments
这是一个可变的
集吗?@SoumyaSimanta不,这是一个不可变的集。@Anand-我不清楚如何重新分配to'val googleXfpSegments'?非常感谢您在这方面给我的启发!它确实有效!