modo-studio/SugarRecord

Dispatching db operation on background thread deadlock

nazywamsiepawel opened this issue · 5 comments

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?

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

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.

@nazywamsiepawel any chance for it to appear on Github?

@nazywamsiepawel and @amayatsky I've started drafting how the method would be like. Could you look into it and give me feedback? #325

Pushed a new version with the changes @amayatsky