2

Question:

  • Does deleting an NSManagedObject have to be done with in context.perform / context.performAndWait block ?
  • Or is it safe to delete the object outside the block ?

Code:

func delete(something: NSManagedObject, context: NSManagedObjectContext) {

    context.performAndWait { //Is context.perform / context.performAndWait required to delete an object ?

        context.delete(something)
    }
}

My thoughts:

  • Since this code was being called from different threads (both background / main ) it was better to use context.perform / context.performAndWait.
  • The context might have been created with a specific concurrent type (main / private queue).
  • The context's concurrent type would need to match that of the thread (main / background) in which the code was being executed.
  • The block would ensure it runs ok even if a thread with a different mismatched thread type is executing it.
user1046037
  • 14,608
  • 12
  • 79
  • 117

2 Answers2

0

As my personal experience, use performAndWait, because it will wait until operation done. Anyway, both method will run on it's own thread.(context's thread).

From Documentation:

perform(:) and performAndWait(:) ensure the block operations are executed on the queue specified for the context. The perform(:) method returns immediately and the context executes the block methods on its own thread. With the performAndWait(:) method, the context still executes the block methods on its own thread, but the method doesn’t return until the block is executed.

Mani
  • 17,226
  • 13
  • 73
  • 97
  • The question is not about whether to use `perform` or `performAndWait`. The question is should I use it at all or is it ok to delete without the block. I understand both would run on their respective threads, but the context might have been created with a specific concurrent type (main / private queue). My thought was context's concurrent type would need to match that of the thread (main / background). – user1046037 Apr 30 '18 at 06:47
  • 1
    I think you didn't understand. Ok let me explain. If you call `perform(:)` or `performAndWait(:)`, these method will place the execution block on the context's queue. Here no matter, where you calling from. If you call it from main/ background thread, that's not mean, the operation will place accordingly. It's a matter, context was created with what type. You no need to think about thread safe. It depends only on context's type. – Mani Apr 30 '18 at 06:51
  • I've seen your updated question. Third and fourth point is wrong understanding. – Mani Apr 30 '18 at 06:53
0
  1. In apple documentation it is being said about „...andWait” it works assyncronious. However „...andWait” should be used to catch the errors inside of perform block...

    Moc.performBlock{ for jsonObject in jsonArray { let your = actions } do { try moc.save()

    moc.performBlockAndWait { do { try moc.save() } catch { fatalError(„Failure to save context: (error)”) } } }...

  2. Better to do it inside in case you have different values / unused values. In most cases ARC (memory management) should fix it.

You should also read here:

Core Data background context best practice

J A S K I E R
  • 1,181
  • 3
  • 14
  • 31