3

I know that NSManagedObjectContext is not thread safe but I want to execute my fetches and updates in the background. So I have questions on if I can do this.

I have one singleton that accesses the core data stuff so I wanted to create a serial dispatch_queue_t can execute all the requests in serial. Is this okay cause I am not sure if all the requests will be executed in the same thread but there will not be any concurrent operations since it is serial.

Marcus S. Zarra
  • 46,571
  • 9
  • 101
  • 182
dadougster
  • 642
  • 5
  • 9
  • 2
    [Core Data **is** thread safe](http://stackoverflow.com/a/21941891/10673). The `NSManagedObjectContext` is just thread contained. The terminology is important. – Marcus S. Zarra Mar 07 '14 at 19:26

1 Answers1

10

There is no need in creating serial dispatch_queue_t just trust NSManagedObjectContext to handle it, just use performBlock: & performBlockAndWait:

    [context performBlock:^{
        // do fetches or updates here and do not worry about threads,
        // just make sure your context has a proper concurrencyType
    }];
dariaa
  • 6,285
  • 4
  • 42
  • 58
  • To be clear, @dadougster is correct, you shouldn't pin an `NSManagedObjectContext` to a serial queue because the serial queue doesn't guarantee thread confinent. – Heath Borders Mar 08 '14 at 04:16