Synchronize nested async task - ios

I have nested async tasks. The function below following this flow: loadEpisodes(load a list of episodes) -> use a array from completion to loop through each episode one and load comments ( one more async task ) for specific episode.
The problem is: comletion(fullyEpisodes) excuted before finishing comments load task. I tried to use Dispatch Group (second code block) but it's not working.
func loadComments(comletion: #escaping ([Episode]) -> Void){
loadEpisodes(completion: {
episodes in
var fullyEpisodes = [Episode]()
for episode in episodes {
WebService().load(resource: episode.comment, comletion: {
comments in
if let comments = comments {
let _episode = Episode(id: episode.id, title: episode.title, comments: comments)
fullyEpisodes.append(_episode)
print("done")
}
})
}
comletion(fullyEpisodes)
})
}
Implemented Dispatch Group:
func loadComments(comletion: #escaping ([Episode]) -> Void){
loadEpisodes(completion: {
episodes in
var fullyEpisodes = [Episode]()
let group = DispatchGroup()
for episode in episodes {
group.enter()
WebService().load(resource: episode.comment, comletion: {
comments in
if let comments = comments {
let _episode = Episode(id: episode.id, title: episode.title, comments: comments)
fullyEpisodes.append(_episode)
print("done")
}
})
group.leave()
}
group.wait()
group.notify(queue: .main, execute: {
comletion(fullyEpisodes)
})
})
}
When I try to replace the comment load request by "print("something")" (not a new async task), Dispatch group is working.

In your second example, (a) move group.leave() into the load() completion handler closure; and (b) remove the group.wait() altogether.
func loadComments(comletion: #escaping ([Episode]) -> Void){
loadEpisodes(completion: {
episodes in
var fullyEpisodes = [Episode]()
let group = DispatchGroup()
for episode in episodes {
group.enter()
WebService().load(resource: episode.comment, comletion: {
comments in
if let comments = comments {
let _episode = Episode(id: episode.id, title: episode.title, comments: comments)
fullyEpisodes.append(_episode)
print("done")
}
group.leave()
})
// group.leave()
}
//group.wait()
group.notify(queue: .main, execute: {
comletion(fullyEpisodes)
})
})
}
or, cleaning that up a bit with trailing closure syntax and fixing completion spelling:
func loadComments(completion: #escaping ([Episode]) -> Void) {
loadEpisodes { episodes in
var fullyEpisodes = [Episode]()
let group = DispatchGroup()
for episode in episodes {
group.enter()
WebService().load(resource: episode.comment) { comments in
if let comments = comments {
let _episode = Episode(id: episode.id, title: episode.title, comments: comments)
fullyEpisodes.append(_episode)
}
group.leave()
}
}
group.notify(queue: .main) {
completion(fullyEpisodes)
}
}
}

Related

DispatchGroup with async functions in a Firebase Realtime DB observe method in Swift

I'm developing an iOS chat app that uses Firebase Realtime Database for storing messages. I have a function that is called when the home chat screen is loaded. This function loads the recipient name, last message, timestamp and a profile pic.
I've used DispatchGroup to sync all the calls. At first, I thought that it worked, but when I send a new message (update the DB in any way) the app crashes. I believe it is because the observe closure is being called again, and there is an imbalance between the enter/leave calls.
I can't think of a way to make it work with DispatchGroup. Is there a way to fix this? Or is there a better option than DispatchGroup?
This is the main function with the firebase observer:
func getAllChatsForCurrentUser(completion: #escaping (_ chats: [Chat], _ error: Error?) -> Void) {
var chats = [Chat]()
let group = DispatchGroup()
let currentUserUID = Auth.auth().currentUser!.uid
let chatRef = Database.database().reference(withPath: "chats")
group.enter()
chatRef.observe(.value) { (snapshot) in
var childrenArray = [String]()
let children = snapshot.children
while let rest = children.nextObject() as? DataSnapshot {
childrenArray.append(rest.key) //1
}
for child in childrenArray {
if child.contains(currentUserUID) { //2
let otherUserUID = child.replacingOccurrences(of: currentUserUID, with: "")
group.enter()
self.getChatInfo(uid: otherUserUID, chatID: child) { (chat, err) in
chats.append(chat)
group.leave()
}
}
}
group.leave()
}
group.notify(queue: .main) {
completion(chats, nil)
}
}
1 - For the chat name I use a combination of 2 uid's. So here I have an array of all chats.
2 - If the chat name contains the current users uid - I'm working with it. The recipients uid in the other part of the string.
getChatInfo function below:
func getChatInfo(uid: String, chatID: String, completion: #escaping (_ chat: Chat, _ error: Error?) -> Void) {
let miniGroup = DispatchGroup()
var newChat = Chat()
newChat.otherUserUid = uid
miniGroup.enter()
self.getUserProfileFromUID(uid: uid) { (user, error) in
newChat.name = user.name
newChat.profilePic = user.photoURL
miniGroup.leave()
}
miniGroup.enter()
self.getLastMessageAndTimeForChat(chatID: chatID) { (message, time, error) in
newChat.lastMessage = message
newChat.lastMessageTime = time
miniGroup.leave()
}
miniGroup.notify(queue: .main) {
completion(newChat, nil)
}
}
I know that this is probably a bad way of structuring the data and calling the functions. At least I've been told so, without reasoning. Been stuck with this problem for nearly a week now, any info would be greatly appreciated.
UPDATE 1
Tried wrapping the leave() calls in defer {}, and tried playing around with NSOperations instead of DispatchGroup. Still no luck.
So I figured it out by using a completion handler with a begin handler.
getChatsWithBeginAndComplete(beginHandler: {
self.group.enter()
self.group.notify(queue: .main) {
print("done")
self.tableView.reloadData()
}
}) {
self.group.leave()
}
And the function:
func getChatsWithBeginAndComplete(beginHandler: #escaping () -> (), completionHandler: #escaping () -> ()) {
allChatsHandle = allChatsRef.observe(.value) { (snapshot) in
let bigGroup = DispatchGroup()
beginHandler()
var childrenArray = [String]()
let children = snapshot.children
while let rest = children.nextObject() as? DataSnapshot {
childrenArray.append(rest.key)
}
for chatID in childrenArray {
if chatID.contains(currentUserUID) {
bigGroup.enter()
let funcGroup = DispatchGroup()
//Do more async stuff in the funcGroup
funcGroup.notify(queue: .main) {
self.chats.append(chat)
bigGroup.leave()
}
}
}
bigGroup.notify(queue: .main) {
completionHandler()
}
}
}
So here all the group.enter and group.leave calls are balanced, because they are called from the completion/begin handlers, or from inside the firebase observer.
I don't think that it's the best way to handle this problem, but it's definitely one way. If somebody knows a better solution - please let me know.

Delay of delete data into Firestore array field values through loop SWIFT

I am trying to delete all the cart items through a for loop like this:
func removeRelatedProductFromUserCart(selectArrayNodeIds: [String], completion: #escaping(Error?)->()) {
let semaphore = DispatchSemaphore(value: 0)
let serialQueue = DispatchQueue(label: "com.queue.Serial")
serialQueue.async {
for nodeId in selectArrayNodeIds {
FirebaseManager.shared.removeProductFromUsersWishOrCartList(isForWishList: false, item: nodeId) { (error) in
completion(error)
semaphore.signal()
}
semaphore.wait()
}
}
}
And Firebasemanager:
func removeProductFromUsersWishOrCartList(isForWishList: Bool, item: String?, completion: #escaping (Error?) -> ()) {
let uid = UserDefaults.standard.string(forKey: "uid")!
if isForWishList == true {
Firestore.firestore().collection("user").document(uid).updateData([
"wishList": FieldValue.arrayRemove([item!])
]) { (error) in
completion(error)
}
} else {
Firestore.firestore().collection("user").document(uid).updateData([
"cart": FieldValue.arrayRemove([item!])
]) { (error) in
completion(error)
}
}
}
And then when I am trying to fetch the cart items from the same field, I am getting the updated list of cart items after a delay. I observed the delete process opening firebase console and what I have found is that the delete happens in console with a delay. But the error response (error == nill) doesn't wait till then in removeRelatedProductFromUserCart. So how can I wait till delete all cart items on Firestore and then load them? Thanks a lot in advance.
1- Firebase runs in a background thread so no need for
let serialQueue = DispatchQueue(label: "com.queue.Serial")
2- You need a DispatchGroup
func removeRelatedProductFromUserCart(selectArrayNodeIds: [String], completion: #escaping(Bool)->()) {
let g = DispatchGroup()
for nodeId in selectArrayNodeIds {
g.enter()
FirebaseManager.shared.removeProductFromUsersWishOrCartList(isForWishList: false, item: nodeId) { (error) in
q.leave()
}
}
g.notify(queue:.main) {
completion(true)
}
}

How can I run 2 asynchrones code in one function and escape them?

I want to run 2 pieces of asynchronous code in one function and escape them. I want first to download the Reciter information and then download with these information the images that is associated with the Reciter. I'm using Firestore. I tried to work with DispatchQueue and DispatchGroup but I couldn't figure something out. I hope someone can help me :)
func getReciters(completion: #escaping (Bool) -> Void) {
var reciters = [Reciter]()
self.BASE_URL.collection(REF_RECITERS).getDocuments { (snapchot, error) in
if let error = error {
debugPrint(error)
completion(false)
// TODO ADD UIALTERCONTROLLER MESSAGE
return
}
guard let snapchot = snapchot else { debugPrint("NO SNAPSHOT"); completion(false); return }
for reciter in snapchot.documents {
let data = reciter.data()
let reciterName = data[REF_RECITER_NAME] as? String ?? "ERROR"
let numberOfSurahs = data[REF_NUMBER_OF_SURAHS] as? Int ?? 0
// **HERE I WANT TO DOWNLOAD THE IMAGES**
self.downloadImage(forDocumentID: reciter.documentID, completion: { (image) in
let reciter = Reciter(name: reciterName, image: nil, surahCount: numberOfSurahs, documentID: reciter.documentID)
reciters.append(reciter)
})
}
}
UserDefaults.standard.saveReciters(reciters)
completion(true)
}
You need DispatchGroup.
In the scope of the function declare an instance of DispatchGroup.
In the loop before the asynchronous block call enter.
In the loop inside the completion handler of the asynchronous block call leave.
After the loop call notify, the closure will be executed after all asynchronous tasks have finished.
func getReciters(completion: #escaping (Bool) -> Void) {
var reciters = [Reciter]()
self.BASE_URL.collection(REF_RECITERS).getDocuments { (snapchot, error) in
if let error = error {
debugPrint(error)
completion(false)
// TODO ADD UIALTERCONTROLLER MESSAGE
return
}
guard let snapchot = snapchot else { debugPrint("NO SNAPSHOT"); completion(false); return }
let group = DispatchGroup()
for reciter in snapchot.documents {
let data = reciter.data()
let reciterName = data[REF_RECITER_NAME] as? String ?? "ERROR"
let numberOfSurahs = data[REF_NUMBER_OF_SURAHS] as? Int ?? 0
group.enter()
self.downloadImage(forDocumentID: reciter.documentID, completion: { (image) in
let reciter = Reciter(name: reciterName, image: nil, surahCount: numberOfSurahs, documentID: reciter.documentID)
reciters.append(reciter)
group.leave()
})
}
group.notify(queue: .main) {
UserDefaults.standard.saveReciters(reciters)
completion(true)
}
}
}

Dispatch group notify seems to get called too early

I am trying to use dispatch group as suggested here https://stackoverflow.com/a/35906703/406322
However, it seems like myGroup.notify is being called before all the iterations of the for loop is completed. What am I doing wrong?
let myGroup = DispatchGroup()
for channel in channels.subscribedChannels() {
myGroup.enter()
buildUser(channel) { (success, user) in
if success {
addUser(user)
}
print("Finished request \(user.id)")
myGroup.leave()
}
}
myGroup.notify(queue: .main) {
print("Finished all requests.")
}
The output is this:
Finished request 1
Finished all requests.
Finished request 2
Not sure, but isn't your print("Finished request \(user.id)") being called from a thread and therefore can be called after your print("Finished all requests.") since it's on a main priority queue ?
try replacing
print("Finished request \(user.id)")
by:
DispatchQueue.main.async {
print("Finished request \(user.id)")
}
Testing it out in a playground works fine:
import Foundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
class User {
var id: Int
init(id: Int) {
self.id = id
}
}
class Channel {
var user: User
init(user: User) {
self.user = user
}
}
var subscribedChannels: [Channel] = []
let user1 = User(id: 1)
let user2 = User(id: 2)
subscribedChannels.append(Channel(user: user1))
subscribedChannels.append(Channel(user: user2))
let myGroup = DispatchGroup()
let bgQueue = DispatchQueue.global(qos: .background)
func doSomething(channel: Channel, callback: #escaping (Bool, User) -> Void) {
print("called for \(channel.user.id)")
bgQueue.asyncAfter(deadline: .now() + 1) {
callback(true, channel.user)
}
}
for channel in subscribedChannels {
myGroup.enter()
doSomething(channel: channel) { (success, user) in
if success {
//
}
print("Finished request \(user.id)")
myGroup.leave()
}
}
myGroup.notify(queue: .main) {
print("Finished all requests.")
}
this prints
called for 1
called for 2
then 1 second later
Finished request 1
Finished request 2
Finished all requests.
I don't know your classes and methods so it's hard for me to know more

How to run synchronically two functions with async operations on iOS using Swift

lets propose this scenario
a method with async network operations
func asyncMethodA() -> String?
{
result : String?
Alamofire.manager.request(.POST, "https://www.apiweb.com/apimethod", parameters: parameters, encoding:.JSON)
.response { (request, response, rawdata, error) in
if (response?.statusCode == 200)
{
//DO SOME HEAVY LIFTING
}
}
return result //string
}
another method with async network operations
func asyncMethodB() -> String?
{
result : String?
Alamofire.manager.request(.POST, "https://www.yetanotherapiweb.com/apimethod", parameters: parameters, encoding:.JSON)
.response { (request, response, rawdata, error) in
if (response?.statusCode == 200)
{
//DO SOME HEAVY LIFTING
}
}
return result //string
}
a method in which i shall call those methods A and B, to do some operations
func displayResult
{
1) let a = asyncMethodA()
2) let b = asyncMethodB()
3) println(a + b) //some chaotic stuff might happen :(
}
so the question is how i could make that (2) waits for (1) to run, and (3) waits for (2) and so on (that 1,2 and 3 run syncronised)?
(i know that one answer is to chain asyncMethodA and displayResult into asyncMethodB, but want to know if there is some other method)
thank you!.
func anAsyncMethod(resultHandler: (result: AnyObject) -> Void) {
...
}
func anotherAsyncMethod(resultHandler: (result: AnyObject) -> Void) {
...
}
let operationQueue = NSOperationQueue()
func performWithCompletionHandler(completion: (AnyObject?, AnyObject?) -> Void) {
var resultOfOperation1: AnyObject?
var resultOfOperation2: AnyObject?
let operation1 = NSBlockOperation {
let dispatchGroup = dispatch_group_create()
dispatch_group_enter(dispatchGroup)
self.anAsyncMethod {
result in
resultOfOperation1 = result
dispatch_group_leave(dispatchGroup)
}
// wait until anAsyncMethod is completed
dispatch_group_wait(dispatchGroup, DISPATCH_TIME_FOREVER)
}
let operation2 = NSBlockOperation {
let dispatchGroup = dispatch_group_create()
dispatch_group_enter(dispatchGroup)
self.anotherAsyncMethod {
result in
resultOfOperation2 = result
dispatch_group_leave(dispatchGroup)
}
// wait until anotherAsyncMethod is completed
dispatch_group_wait(dispatchGroup, DISPATCH_TIME_FOREVER)
}
let completionOperation = NSBlockOperation {
// send all results to completion callback
completion(resultOfOperation1, resultOfOperation2)
}
// configuring interoperation dependencies
operation2.addDependency(operation1)
completionOperation.addDependency(operation2)
operationQueue.addOperations([operation1, operation2, completionOperation], waitUntilFinished: false)
}
Thanks Yimin for the code above. I've updated it to the latest Swift syntax so just posting to be helpful:
func anAsyncMethod(resultHandler: (_ result: AnyObject) -> Void) {
...
}
func anotherAsyncMethod(resultHandler: (_ result: AnyObject) -> Void) {
...
}
func performWithCompletionHandler(completion: #escaping (AnyObject?, AnyObject?) -> Void) {
let operationQueue = OperationQueue()
var resultOfOperation1: AnyObject?
var resultOfOperation2: AnyObject?
let operation1 = BlockOperation {
let dispatchGroup = DispatchGroup()
dispatchGroup.enter()
self.anAsyncMethod {
result in
resultOfOperation1 = result
dispatchGroup.leave()
}
// wait until anAsyncMethod is completed
dispatchGroup.wait(timeout: DispatchTime.distantFuture)
}
let operation2 = BlockOperation {
let dispatchGroup = DispatchGroup()
dispatchGroup.enter()
self.anotherAsyncMethod {
result in
resultOfOperation2 = result
dispatchGroup.leave()
}
// wait until anotherAsyncMethod is completed
dispatchGroup.wait(timeout: DispatchTime.distantFuture)
}
let completionOperation = BlockOperation {
// send all results to completion callback
completion(resultOfOperation1, resultOfOperation2)
}
// configuring interoperation dependencies
operation2.addDependency(operation1)
completionOperation.addDependency(operation2)
operationQueue.addOperations([operation1, operation2, completionOperation], waitUntilFinished: false)
}
With the below, you can launch both async methods at the same time and do your heavy lifting after whichever one finishes last.
var methodAFinished = false
var methodBFinished = false
func asyncMethodA() -> String?
{
Alamofire.manager.request(.POST, "https://www.apiweb.com/apimethod", parameters: parameters, encoding:.JSON)
.response { (request, response, rawdata, error) in
if (response?.statusCode == 200) {
methodAFinished = true
doStuff()
}
}
return result //string
}
The guts of asyncMethodB would be methodBFinished = true; doStuff()
func doStuff() {
if methodAFinished && methodBFinished {
// do crazy stuff
}
}

Resources