5

We occasionally get crash reports from our users (we're using a crash reporter package that sends crashes to our server) with the following error:

"Illegal attempt to establish a relationship '...' between objects in different contexts"

(Different reports may have different relationship, it's not the same on all reports).

This seems easy enough, except that:

  1. We only have ONE NSManagedObject in our code - the singleton from the App delegate.

  2. We don't use any threads except for the main thread.

  3. ALL objects are allocated using the context using initWithEntity:insertIntoManagedObjectContext: (none of them is allocated with a simple init].

  4. The error NEVER happened in our testing (thousands of runs).

So basically we're stuck.

Any idea will be appreciated.

Amiram Stark
  • 2,208
  • 22
  • 32
  • I am having this exact same issue in my app - one managedObjectContext, no issues in testing, crash reports from users with this message - I would really, really appreciate if you could tell me what you found out to solve this issue – SAHM Nov 28 '13 at 13:13
  • Hi, any solution to this? I have the same problem. – Bartosz Bialecki Jun 10 '16 at 10:38

1 Answers1

-1

Whenever I'm working with Core Data in the main thread, I will put an NSAssert in any methods dealing with the context just to make extra sure I know everything is happening in the main thread. Something like this:

NSAssert([NSThread isMainThread], @"This method is not in the main thread");

try using that just to double/triple/quadruple check that things are happening where you think they are.

JAB
  • 3,165
  • 16
  • 29
  • Thanks, I'll try it, although as I said the crash never happened in our tests :( – Amiram Stark Aug 20 '12 at 20:38
  • more of a precaution than anything. I had an core data based app that showed an MBProgressHUD while doing some work with the context. Forgot that MBProgressHUD creates a separate thread, but never crashed during testing. Only found out when it went live and a handful of users reported crashes. So it can seem to work fine in testing while still working on separate threads. Hope it helps. – JAB Aug 20 '12 at 20:56
  • I know that there are some operations that take place in a dedicated thread (although I believe that MBProgressHUD runs in the main thread because it updates the UI), but the thing is - they don't access Core Data in my App, so they're probably not the reason. – Amiram Stark Aug 21 '12 at 08:05
  • I was operating under that assumption with the hud, then discovered the showWhileExecuting: method for MBProgressHUD creates a new thread. That's where my second thread was being created. – JAB Aug 21 '12 at 19:31