0

I'm trying to first call one function that saves data from firebase to Coredata, and when it's done executing call another function that takes that data from coredata and uses it. I'm using a dispatchGroup and this is my code:

 let dispatchGroup = DispatchGroup()

 dispatchGroup.enter()

 DispatchQueue.main.async {
    self.storeStatsDataFromFirebaseToCoreData()
    dispatchGroup.leave()
    }

  dispatchGroup.notify(queue: .main) {
     self.storePRData()
   }

Now the dispatchGroup works fine. The "storeStatsDateFromFireBaseToCoreData" is being called first, and then the "storePRData". But It seems that the items hasn't finished loading into coredata, so when the "storePRData" func is trying to fetch the items it can't.

Do you guys have any clue to what im missing here? Im a beginner in coding and im thankful for any help.

Greetings, Pontus

Pontus
  • 44
  • 13
  • What's the code of `storeStatsDataFromFirebaseToCoreData()`? It is async? With a `context.perform {}` to ensure the correct thread? A `DispatchGroup()` might be overkill. You might want to have a completion closure instead on `storeStatsDataFromFirebaseToCoreData()`. – Larme Jan 05 '22 at 18:01
  • @Larme I tried to have a completion closure for storeStatsDataFromFirebaseToCoreData(), and call the other function within it. But it had the same problem as with the dispatchGroup. The storeStatsDataFromFirebaseToCoreData() code basically just saves data to coredata. I don't know the difference between async and sync :P – Pontus Jan 05 '22 at 18:40
  • Again, could you show the code of `storeStatsDateFromFireBaseToCoreData`? And if you don't know the difference between sync & async, you should learn about it, it's an important concept. `DispatchGroup`, `enter()`, `leave()` & `notify()` are used when there is multiple asynchronous code, to know and act when all have been done. – Larme Jan 06 '22 at 07:54

0 Answers0