Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone programming - Background saving with core data

I am trying to save data into core data in the background thread, as it takes quite some time to save.

I did:

[self performSelectorInBackGround:@selector(insertRecord:) withObject:data];

When all works fine until the line in insertRecord Method hits contextsave:&error. Program received signal : "SIGABRT"

Am I doing anything wrong? it works ok when its in main thread, I just move the codes to another method and run it in background and it doesn't work anymore.

like image 782
kraitz Avatar asked Dec 17 '22 06:12

kraitz


1 Answers

According to the "Concurrency with Core Data" section of Core Data Programming Guide:

The pattern recommended for concurrent programming with Core Data is thread confinement: each thread must have its own entirely private managed object context.

and

Using thread confinement, you should not pass managed objects or managed object contexts between threads.

It looks like you're passing a managed object to the background thread, which is forbidden. I don't know if you're also trying to share your managed object context between threads.

That document describes a couple of workarounds for passing managed objects to other threads. You'll need to implement one of them.

like image 159
rob mayoff Avatar answered Dec 28 '22 09:12

rob mayoff