저는 Apple의 GCD를 처음 접했고 DispatchGroup
에 문제가 있습니다. 그래서 게시물이 완전히 초기화 된 후지도에 게시물을 첨부하려고합니다. post.init
안에는 URLSession
이 있으며 URL에서 UIImage
을 다운로드합니다. 그러나 다운로드가 완료되기 전에도 group.notify
이 (가) 종료됩니다. 나는 그 이유가 무엇인지 알 수 없다. 아래는 코드입니다. 어떤 조언이나 도움을 주셔서 감사합니다! 감사.작업이 완료되기 전에 파견 그룹이 알립니다.
// ViewController.swift
let group = DispatchGroup
...
...
group.enter()
DispatchQueue.global(qos: .userInitiated).async {
post = Post(values: post)
self.posts[postId] = post
group.leave()
}
group.notify(queue: DispatchQueue.main, execute: {
print("notify:: \(post?.picture)") // This prints out nil, when it shouldn't.
self.addPostToMap(post!, at: location!)
})
// Post.swift
class Post {
var picture: UIImage?
var thumbnail: UIImage?
init(values: [String: Any]) {
...
URLSession.shared.dataTask(with: url!, completionHandler: {(data, response, error) in
DispatchQueue.main.async {
self.picture = UIImage(data: data!)
self.thumbnail = Util.resizeImage(image: self.picture!, targetSize: CGSize(width: 50, height: 50))
}
}).resume()
}
}
답장을 보내 주셔서 감사합니다. 그러나 didFinish에서 변수'post' 및'location'에 액세스 할 수 없으므로 알림을받는 것이 문제가됩니다. 이것에 대한 제안? –
사실 나는 그것을 만들었다! 위임 메서드를 통해 변수를 전달했습니다. 감사! –