제 문제가 조금 복잡해 보일 수도 있습니다. 그러나 나는 자신을 잘 표현하려고 노력할 것입니다.비동기 작업이 스칼라에서 완료 될 때까지 어떻게 대기합니까?
데이터로 채워진 Map[String, List[String]]
을 반환하려는이 방법이 있습니다.
def myFunction():Map[String, List[String]] = {
val userMap = Map[String, String](("123456", "ASDBYYBAYGS456789"),
("54321", "HGFDSA5432"))
//the result map to return when all data is collected and added
val resultMap:Future[Map[String, List[String]]]
//when this map is finished (filled) this map is set to resultMap
val progressMap = Map[String, List[String]]()
for(user <- userMap){
//facebook graph API call to get posts.
val responsePost = WS.url("async get to facebook url").get()
responsePosts.flatMap { response =>
val jsonBody = response.json
val dataList = List[String]()
for(i <-0 until 5){
//parse the json-data to strings
val messages = (jsonBody.\("statuses").\("data")(i).\("message"))
val likesArray = (jsonBody.\("statuses").\("data")(i).\\("data")).flatMap(_.as[List[JsObject]])
val likes = likesArray.length
//Put post with likes in temporary list
dataList ::= ("Post: " + message.toString + " Likes: " + likes.toString)
}
//facebook graph API call to get friends.
val responseFriends = WS.url("async get to facebook url").get()
responseFriends.map { response =>
val jsonBody = response.json
val friendCount = jsonBody.\("data")(0).\("friend_count").toString
//add "Friends: xxx" to the dataList and add the new row to resultMap containig a list with post and friends.
dataList ::= ("Friends: " + friendCount)
progressMap += user._1 -> dataList
//check if all users has been updated
if(progressMap.size == userMap.size){
resultMap = progressMap
}
}
}
}
//return the resultMap.
return resultMap
}
}
내 코드가 최적의 구문으로 작성되지 않았을 수 있습니다.
하지만 내가 원하는 것은 resultMap을 데이터와 함께 반환하는 것입니다. 내 문제는 "get to facebook url"
이 비동기 적으로 완료 되었기 때문에이 resultMap이 비어있게 반환된다는 것입니다. 나는 이것을 빈 곳으로 만들고 싶지 않습니다.
이 코드는 내 방법은 지금까지 내 솔루션입니다. 분명히 작동하지 않지만, 내가하려고하는 것을 볼 수 있기를 바랍니다. 잘 모르는 경우에도 생각대로 대답 해 주시면 올바른 방향으로 나를 놓을 수 있습니다.
val 인 경우 어떻게 값을 dataList에 추가 할 수 있습니까? –