IPhone Programming - Saving Background Using Master Data

I am trying to save data to the main data in a background thread, as it takes quite a lot of time to save.

I did:

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

When everything works fine until the line in the insertRecord Method hits contextsave:&error . Program Signal: "SIGABRT"

Am I doing something wrong? it works fine when it is in the main thread, I just move the codes to another method and run it in the background and it no longer works.

+4
source share
2 answers

According to the Concurrency section with master data in the Master Data Programming Guide :

Recommended pattern for parallel programming using Core Data, stream restriction: each stream must have its own private managed object context.

and

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

It looks like you are passing a managed object into a background thread that is prohibited. I do not know if you are also trying to use the managed object context between threads.

This document describes a couple of workarounds for passing managed objects to other threads. You will need to implement one of them.

+6
source

The problem is that managed object contexts are not thread safe. If your -insertRecord: method uses a flow-controlled object context, you are asking for problems.

Cocoa Blog My girlfriend has an article, Core Data and Threads, without a headache in this thread and offers some strategies for saving in the background. The main idea is to make changes in the context that belongs to the background thread, and then merge the changes in the context of the main thread. This gives you a modern context that can be saved in the background while maintaining the current stream of the main stream.

+6
source

Source: https://habr.com/ru/post/1383365/


All Articles