var max = 0
var tag = null
val tags: Future[List[Tag]] = Tags.find(Json.obj()).toList
for{
tag <- tags
tagsOk <- Requests.find(Json.obj("tags.tag" -> tag.category + " " + tag.name)).count
if(tagsOk > max) {
max = tagsOk
tag = tag.category + " " + tag.name //string tag
}
}
What's wrong??--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
def max = Action {
var max: Int = 0
var tagFound: Tag = null
//obtain all the tags in the db.
val futureTags: Future[List[Tag]] = Tags.all.toList
val futureComputation = futureTags map{ (tags: List[Tag]) =>
tags map {
(tag: Tag) =>
//create the tag String
val tagName = tag.category + ":" + tag.attr
//search in the db the documents where tags.tag == tag.
val futureRequests : Future[List[recommendationsystem.models.Request]]= Requests.find(Json.obj("tags.tag" -> tagName)).toList
futureRequests map { (requests: List[recommendationsystem.models.Request]) =>
//get the numbers of documents matching the tag
val number: Int = requests.size
if(number > max) {
max = number
tagFound = tag
}
}
}
}
futureComputation onComplete {
case Success(_) => println("callback completed")
case Failure(e) => e.printStackTrace
}
println("after complete")
val jsonObject = if(max > 0) Json.obj("tag" -> tagFound, "occurencies" -> max) else Json.obj("tag" -> "NoOne", "occurencies" -> 0)
Ok(jsonObject)
}
I can see that the "after complete" is printed before the "callback completed". Why happened it?? how can i solve my new problem?
My code is now the following:
...
futureComputation onComplete {
case Success(_) => println("callback completed")
case Failure(e) => e.printStackTrace
}
println("after complete")
I can see that the "after complete" is printed before the "callback completed". Why happened it?? how can i solve my new problem?