CoreStore icon indicating copy to clipboard operation
CoreStore copied to clipboard

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

Open matrosovDev opened this issue 5 years ago • 1 comments

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()

matrosovDev avatar May 04 '19 18:05 matrosovDev

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

JohnEstropia avatar May 06 '19 12:05 JohnEstropia