How to use Core Data synchronously? How to use Core Data synchronously? json json

How to use Core Data synchronously?


Typically Core Data transactions should always be performed on the object's Managed Object Context thread. For this reason you will see the performBlock and performBlockAndWait calls in NSManagedObjectContext.

Since you are using the main thread you are technically synchronous assuming you are making those update calls on the main thread. If you are not then I would suggest wrapping your synch call into a performBlockAndWait call.

That being said, you should leverage Apple's Documentation on the subject as they explain how you can implement multithreaded core data. You should always perform your server related updates on a background thread.

If you want to implement a removeAll feature you will need to manually fetch all the objects you want to remove and call context.deleteObject(managedObject). Alternatively if you want something more powerful that should enforce cascade deletion, you can set this in your model editor when you select the relationship. The following Delete Rules are available:

  • Nullify
  • Cascade
  • No Action
  • Deny

Finally, you might find this post useful in explaining some of the commonly used Core Data stack setups and the various performance of each.

Welcome to iOS and good luck:)

EDIT

As an aside you might find Ray Wenderlich provides some great Core Data Tutorials