Hi @Claude31,
well, you can tell me if it is a good idea :) I am open for every good solution. Just to give you more background information.
To a given comment object, I load all the replies. Those replies are stored in the commentReplyArray array.
For each comment reply, I have to execute the async tasks to fetch the information, put it to result array and return it.
Yes, the replies should be displayed in a table view to a given comment. I want to add additional cells below the cell with the original comment.
Maybe I can share the whole function header as well:
func loadReplies(commentItem: CommentItem, isSearchBarFocused:Bool, filteredComments:[CommentItem], dataSource:[CommentItem]) {
var dataSource = dataSource
// Get index of the comment
let index:Int
if (isSearchBarFocused) {
index = filteredComments.firstIndex(of: commentItem)!
}
else {
index = dataSource.firstIndex(of: commentItem)!
}
var ips: [IndexPath] = []
var results: [Int: CommentItem] = [:]
self.dataAccessService.fetchRepliesByCommentId(completionHandler: { (commentReplyArray) in
let group = DispatchGroup()
for var i in 0..<commentReplyArray.count {
let commentReplyObject = commentReplyArray[i]
let commentItem = CommentItem()
group.enter()
self.dataAccessService.fetchUserById(completionHandler: { (userObject) in
commentItem.userObject = userObject
results[i] = commentItem
defer { group.leave() }
}, uid: commentReplyObject.userId)
group.enter()
self.dataAccessService.fetchDownloadURLOfProfileImage(organizerId: commentReplyObject.userId) { (contentURL) in
commentItem.userObject.contentURL = contentURL
defer { group.leave() }
}
group.notify(queue: .main) {
commentItem.commentObject = commentReplyObject
let array = commentReplyArray.compactMap { _ in results[i] }
print(array.count)
}
}
}, commentId: commentItem.commentObject.id)
}
This time, I tried to move the let group = DispatchGroup() into the loop