How can I run 2 asynchrones code in one function and escape them?How can I develop for iPhone using a Windows development machine?How can I disable the UITableView selection?How can I make a UITextField move up when the keyboard is present - on starting to edit?What are Unwind segues for and how do you use them?Update or reload UITableView after completion of delete action on detail viewSwift Error - Use of undeclared type 'cell' - Collection ViewHow to load data retrieved from Firestore in UIPickerView?Pausing while downloading Firestore dataHow can my app open Siri shortcuts recording phrase viewHow to execute a function until Firestore complete downloading data by Swift?

How can I, as DM, avoid the Conga Line of Death occurring when implementing some form of flanking rule?

"Oh no!" in Latin

Travelling in US for more than 90 days

Why doesn't Gödel's incompleteness theorem apply to false statements?

How to test the sharpness of a knife?

If the Dominion rule using their Jem'Hadar troops, why is their life expectancy so low?

Why is participating in the European Parliamentary elections used as a threat?

Why would five hundred and five same as one?

How do you say "Trust your struggle." in French?

How to get directions in deep space?

Showing mass murder in a kid's book

Why does the frost depth increase when the surface temperature warms up?

New Order #2: Turn My Way

Pre-Employment Background Check With Consent For Future Checks

Sort with assumptions

Toggle window scroll bar

Do native speakers use "ultima" and "proxima" frequently in spoken English?

Is there any common country to visit for persons holding UK and Schengen visas?

Taking my research idea outside my paid job

In the event of Brexit being postponed beyond the EU elections, will UK voters in EU countries be eligible to participate?

What is this high flying aircraft over Pennsylvania?

Center page as a whole without centering each element individually

Put the phone down / Put down the phone

C++ lambda syntax



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


How can I develop for iPhone using a Windows development machine?How can I disable the UITableView selection?How can I make a UITextField move up when the keyboard is present - on starting to edit?What are Unwind segues for and how do you use them?Update or reload UITableView after completion of delete action on detail viewSwift Error - Use of undeclared type 'cell' - Collection ViewHow to load data retrieved from Firestore in UIPickerView?Pausing while downloading Firestore dataHow can my app open Siri shortcuts recording phrase viewHow to execute a function until Firestore complete downloading data by Swift?













0















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)










share|improve this question
























  • Just put completion(true) inside the downloadImage block?

    – Tj3n
    Mar 7 at 2:35











  • you need yo implement it with dispatch group ... read multi threading in depth.

    – Abu Ul Hassan
    Mar 7 at 6:28











  • Tj3n: Then it's called on every loop which I don't want

    – SwiftNewling
    Mar 7 at 6:37












  • Abu UI Hassan: can you give me a quick simple example how to use these the right way because I tried dispatch group before. Thanks in advance

    – SwiftNewling
    Mar 7 at 6:39















0















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)










share|improve this question
























  • Just put completion(true) inside the downloadImage block?

    – Tj3n
    Mar 7 at 2:35











  • you need yo implement it with dispatch group ... read multi threading in depth.

    – Abu Ul Hassan
    Mar 7 at 6:28











  • Tj3n: Then it's called on every loop which I don't want

    – SwiftNewling
    Mar 7 at 6:37












  • Abu UI Hassan: can you give me a quick simple example how to use these the right way because I tried dispatch group before. Thanks in advance

    – SwiftNewling
    Mar 7 at 6:39













0












0








0








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)










share|improve this question
















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)







ios google-cloud-firestore swift4.2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 5:56









Paulw11

69.8k1088104




69.8k1088104










asked Mar 7 at 0:57









SwiftNewlingSwiftNewling

30315




30315












  • Just put completion(true) inside the downloadImage block?

    – Tj3n
    Mar 7 at 2:35











  • you need yo implement it with dispatch group ... read multi threading in depth.

    – Abu Ul Hassan
    Mar 7 at 6:28











  • Tj3n: Then it's called on every loop which I don't want

    – SwiftNewling
    Mar 7 at 6:37












  • Abu UI Hassan: can you give me a quick simple example how to use these the right way because I tried dispatch group before. Thanks in advance

    – SwiftNewling
    Mar 7 at 6:39

















  • Just put completion(true) inside the downloadImage block?

    – Tj3n
    Mar 7 at 2:35











  • you need yo implement it with dispatch group ... read multi threading in depth.

    – Abu Ul Hassan
    Mar 7 at 6:28











  • Tj3n: Then it's called on every loop which I don't want

    – SwiftNewling
    Mar 7 at 6:37












  • Abu UI Hassan: can you give me a quick simple example how to use these the right way because I tried dispatch group before. Thanks in advance

    – SwiftNewling
    Mar 7 at 6:39
















Just put completion(true) inside the downloadImage block?

– Tj3n
Mar 7 at 2:35





Just put completion(true) inside the downloadImage block?

– Tj3n
Mar 7 at 2:35













you need yo implement it with dispatch group ... read multi threading in depth.

– Abu Ul Hassan
Mar 7 at 6:28





you need yo implement it with dispatch group ... read multi threading in depth.

– Abu Ul Hassan
Mar 7 at 6:28













Tj3n: Then it's called on every loop which I don't want

– SwiftNewling
Mar 7 at 6:37






Tj3n: Then it's called on every loop which I don't want

– SwiftNewling
Mar 7 at 6:37














Abu UI Hassan: can you give me a quick simple example how to use these the right way because I tried dispatch group before. Thanks in advance

– SwiftNewling
Mar 7 at 6:39





Abu UI Hassan: can you give me a quick simple example how to use these the right way because I tried dispatch group before. Thanks in advance

– SwiftNewling
Mar 7 at 6:39












1 Answer
1






active

oldest

votes


















1














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)








share|improve this answer























  • Thank you it worked :) But i have a question: Doesn't it leave the dispatch group on every loop when it executes the downloadImage() function? If yes why it works then?

    – SwiftNewling
    Mar 7 at 15:06






  • 1





    Think asynchronous. downloadImage works asynchronously, the completion handler is called much later in time. Imagine DispatchGroup as a counter, enter increases the counter (quickly), leave decreases it (slower). When the counter reaches zero, the closure in notify is called.

    – vadian
    Mar 7 at 15:18











  • Aaaah makes sense. Thank you

    – SwiftNewling
    Mar 7 at 16:15










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55034480%2fhow-can-i-run-2-asynchrones-code-in-one-function-and-escape-them%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














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)








share|improve this answer























  • Thank you it worked :) But i have a question: Doesn't it leave the dispatch group on every loop when it executes the downloadImage() function? If yes why it works then?

    – SwiftNewling
    Mar 7 at 15:06






  • 1





    Think asynchronous. downloadImage works asynchronously, the completion handler is called much later in time. Imagine DispatchGroup as a counter, enter increases the counter (quickly), leave decreases it (slower). When the counter reaches zero, the closure in notify is called.

    – vadian
    Mar 7 at 15:18











  • Aaaah makes sense. Thank you

    – SwiftNewling
    Mar 7 at 16:15















1














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)








share|improve this answer























  • Thank you it worked :) But i have a question: Doesn't it leave the dispatch group on every loop when it executes the downloadImage() function? If yes why it works then?

    – SwiftNewling
    Mar 7 at 15:06






  • 1





    Think asynchronous. downloadImage works asynchronously, the completion handler is called much later in time. Imagine DispatchGroup as a counter, enter increases the counter (quickly), leave decreases it (slower). When the counter reaches zero, the closure in notify is called.

    – vadian
    Mar 7 at 15:18











  • Aaaah makes sense. Thank you

    – SwiftNewling
    Mar 7 at 16:15













1












1








1







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)








share|improve this answer













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)









share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 7 at 7:54









vadianvadian

153k17163189




153k17163189












  • Thank you it worked :) But i have a question: Doesn't it leave the dispatch group on every loop when it executes the downloadImage() function? If yes why it works then?

    – SwiftNewling
    Mar 7 at 15:06






  • 1





    Think asynchronous. downloadImage works asynchronously, the completion handler is called much later in time. Imagine DispatchGroup as a counter, enter increases the counter (quickly), leave decreases it (slower). When the counter reaches zero, the closure in notify is called.

    – vadian
    Mar 7 at 15:18











  • Aaaah makes sense. Thank you

    – SwiftNewling
    Mar 7 at 16:15

















  • Thank you it worked :) But i have a question: Doesn't it leave the dispatch group on every loop when it executes the downloadImage() function? If yes why it works then?

    – SwiftNewling
    Mar 7 at 15:06






  • 1





    Think asynchronous. downloadImage works asynchronously, the completion handler is called much later in time. Imagine DispatchGroup as a counter, enter increases the counter (quickly), leave decreases it (slower). When the counter reaches zero, the closure in notify is called.

    – vadian
    Mar 7 at 15:18











  • Aaaah makes sense. Thank you

    – SwiftNewling
    Mar 7 at 16:15
















Thank you it worked :) But i have a question: Doesn't it leave the dispatch group on every loop when it executes the downloadImage() function? If yes why it works then?

– SwiftNewling
Mar 7 at 15:06





Thank you it worked :) But i have a question: Doesn't it leave the dispatch group on every loop when it executes the downloadImage() function? If yes why it works then?

– SwiftNewling
Mar 7 at 15:06




1




1





Think asynchronous. downloadImage works asynchronously, the completion handler is called much later in time. Imagine DispatchGroup as a counter, enter increases the counter (quickly), leave decreases it (slower). When the counter reaches zero, the closure in notify is called.

– vadian
Mar 7 at 15:18





Think asynchronous. downloadImage works asynchronously, the completion handler is called much later in time. Imagine DispatchGroup as a counter, enter increases the counter (quickly), leave decreases it (slower). When the counter reaches zero, the closure in notify is called.

– vadian
Mar 7 at 15:18













Aaaah makes sense. Thank you

– SwiftNewling
Mar 7 at 16:15





Aaaah makes sense. Thank you

– SwiftNewling
Mar 7 at 16:15



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55034480%2fhow-can-i-run-2-asynchrones-code-in-one-function-and-escape-them%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

1928 у кіно

Захаров Федір Захарович

Ель Греко