modo-studio / SugarRecord

CoreData/Realm sweet wrapper written in Swift
MIT License
2.11k stars 223 forks source link

Dispatching db operation on background thread deadlock #311

Closed nazywamsiepawel closed 7 years ago

nazywamsiepawel commented 7 years ago

Hey all,

I'm coding up a sync mechanism that downloads data entities in big batches, then maps then and upserts to core data. I do not want to run it on the main queue. My current attempt looks like this :

DispatchQueue.global(qos: .background).async {
   do {
        try self.db.operation { (context, save) throws in
   /* Creating / Updating a lot of entities here */

            try save()
        }
     } catch {
                assertionFailure()
     } 
}

What I end up is a deadlock in performBlockAndWait. When dispatching it on the main queue, everything works correctly (but blocks the UI for a while). I run it on iOS 10.

  1. Is this a bug or expected behaviour and I'm doing something wrong here?
  2. If the latter, what is the best way to offload a big number of core data operations into a background thread using SugarRecord?
amayatsky commented 7 years ago

Have you abandoned this ship? I am running all my save operations on the main queue now, seems kinda sad to me.

nazywamsiepawel commented 7 years ago

hey @amayatsky, dispatching all save notifications on the main queue is sad indeed.

Couple months ago I have completely abandoned SugarRecord in favor of my own mini core data wrapper that I have written for one of my projects so I'm not struggling with this issue any more.

amayatsky commented 7 years ago

@nazywamsiepawel any chance for it to appear on Github?

pepicrft commented 7 years ago

@nazywamsiepawel and @amayatsky I've started drafting how the method would be like. Could you look into it and give me feedback? https://github.com/carambalabs/SugarRecord/issues/325

pepicrft commented 7 years ago

Pushed a new version with the changes @amayatsky