I have a series of nested completion blocks in the code provided below. This is because I need to make separate network requests in the background to abstract data to be used in the next method, which provides another completion block, and so on. Is there any way around this? Any tip is much appreciated!
func fetchNearbyUsers(forCurrentUser user: User, completionHandler: usersCompletionHandler?) {
self.fetchAllUsers(completionHandler: { (users: [User]) in
ChatProvider.sharedInstance.fetchAllChatrooms(completionHandler: { (chatrooms: [Chatroom]) in
self.validateNewUsers(currentUser: user, users: users, chatrooms: chatrooms, completionHandler: { (validUsers: [User]) in
guard validUsers.isEmpty == false else {
completionHandler?([])
return
}
completionHandler?(validUsers)
})
})
})
}