Problem moving from old(er) version of MR to current versions... #115

Closed
awmwong opened this Issue Dec 19, 2011 · 6 comments

Projects

None yet

5 participants

awmwong commented Dec 19, 2011

The current version of my application used an older version of MR (don't know the exact revision, but it was the pre MR_ prefix days),

All over the application I have code that used to look like this:

NSManagedObjectContext* context = [NSManagedObjectContext context];
[context setNotifiesMainContextOnSave:YES];

that sits in a background operation. Upon updating to the current version of MR, I went through and updated everything to look like this...

NSManagedObjectContext* context = [NSManagedObjectContext MR_context];
[context MR_setNotifiesMainContextOnSave:YES];

but now I'm getting an

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Context already has a coordinator;  cannot replace.'

when the setter is being called.

What has changed?

FWIW, Building on iOS5 SDK without ARC.

EDIT1 I realize now that there is now a MR_contextThatNotifiesDefaultContextOnMainThread helper... is this the new preferred way of doing things?

EDIT2 Moving everything to MR_contextThatNotifiesDefaultContextOnMainThread has fixed the initial problem.. but now I have random crashes with EXC_BAD_ACCESS...

Perhaps some information on what my operations are doing:

  • Operations are created and added to a background operation queue
  • Each operation creates it's own NSManagedObjectContext and sets it to notify the main thread
  • Operation performs some ASIHTTP request and on completion of the request creates/modifies the corresponding entities.
  • Context Save; Operation is finished.

EDIT3 I went on a hunch this morning and took a gamble that the problem had something to do with ARC, so I went and took out
#define MR_USE_ARC __has_feature(objc_arc)
inside CoreData+MagicalRecord.h to force NO ARC operation...

This is probably not the way to be doing things, but it seems to have temporarily alleviated my problems...

EDIT 4 So it looks like MR_contextThatNotifiesDefaultContextOnMainThread helper doesn't seem to be working properly. The context it creates doesn't notify the main context at all. Workaround that I managed to get working is to do
NSManagedObjectContext *context = [NSManagedObjectContext MR_context]; //background context
[[NSManagedObjectContext MR_defaultContext] MR_observeContext:context];
only then I could get a proper merge to occur.

Strictly looking at the console log, if you call MR_save from a context returned by the helper, you never see the
Merging changes to *** DEFAULT *** context
happening.

Digging into the source reveals that depending on whether you are running in THREAD_ISOLATION_ENABLED or PRIVATE_QUEUES_ENABLED, you get some REALLY different things happening for the helpers/methods related to
having a context that notifies the main one...

awmwong commented Dec 20, 2011

I'm downgrading to 1.8 for now and seeing how far I can get the application to run with that...

I am seeing the same issue with the EXC_BAD_ACCESS

I posted a similar issue here: #141

Did either of you define an approach that works? Anthony, did you downgrade and get better results?

awmwong commented Feb 16, 2012

I haven't been working on the particular project for a while now, but at the time I believe we decided on a downgrade and things seemed to work out okay.

thanks, I'll put a downgrade on my list of things to try. I only require iOS 5, so I'm giving the iOS 5-only branch a try: https://github.com/magicalpanda/MagicalRecord/tree/iOS5-only

Kastet commented Mar 6, 2012

Same issue. What to do?

@tonyarnold tonyarnold closed this Dec 14, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment