Using DispatchGroup leads to issue with invocation CoreStore success and failure closures. #270

Open
opened 2025-12-29 18:25:10 +01:00 by adam · 1 comment
Owner

Originally created by @matrosovDev on GitHub (May 4, 2019).

I faced with the issue that I never get code called group.leave() line. Looks like when we use DispatchGroup() waiting for all async calls but it does not call CoreStore success and failure blocks then.

let group = DispatchGroup()
        
        group.enter()
        
        
        if let workoutsDictionaries = serverResponse["other"] as? [[String: Any]] {
            
            CoreStore.perform(
                asynchronous: { (transaction) -> [WorkoutEntity]? in
                    
                    let workouts = self.offlineModeService.mergedWorkouts(from: workoutsDictionaries, transaction: transaction)
                    
                    return workouts
                    
            },
                success: { (transactionWorkouts) in
                    
                    guard let unwrappedTransactionWorkouts = transactionWorkouts else {
                        return
                    }
                    
                    let workouts = CoreStore.fetchExisting(unwrappedTransactionWorkouts)
                    
                    group.leave()
                    
                    //completion(.data(workouts))
            },
                failure: { (error) in
                    //completion(.error(StackedError.coreDataObjectSavingFailed(error: error)))
                    
                    group.leave()
            }
            )
            
        }
        
        group.wait()
Originally created by @matrosovDev on GitHub (May 4, 2019). I faced with the issue that I never get code called group.leave() line. Looks like when we use DispatchGroup() waiting for all async calls but it does not call CoreStore success and failure blocks then. ``` let group = DispatchGroup() group.enter() if let workoutsDictionaries = serverResponse["other"] as? [[String: Any]] { CoreStore.perform( asynchronous: { (transaction) -> [WorkoutEntity]? in let workouts = self.offlineModeService.mergedWorkouts(from: workoutsDictionaries, transaction: transaction) return workouts }, success: { (transactionWorkouts) in guard let unwrappedTransactionWorkouts = transactionWorkouts else { return } let workouts = CoreStore.fetchExisting(unwrappedTransactionWorkouts) group.leave() //completion(.data(workouts)) }, failure: { (error) in //completion(.error(StackedError.coreDataObjectSavingFailed(error: error))) group.leave() } ) } group.wait() ```
adam added the question label 2025-12-29 18:25:10 +01:00
Author
Owner

@JohnEstropia commented on GitHub (May 6, 2019):

You are likely causing a deadlock as perform(asynchronous:) should never be waited on. Try to use perform(synchronous:) instead.

@JohnEstropia commented on GitHub (May 6, 2019): You are likely causing a deadlock as `perform(asynchronous:)` should never be waited on. Try to use `perform(synchronous:)` instead.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore-JohnEstropia#270