سؤال

I'm new to the great framework Magical Record.

When do I have to give a context to a MR method?

Example:

// with context
NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
CheckinTypes *checkinType = [CheckinTypes MR_createInContext:localContext];

// without context
CheckinTypes *checkinType1 = [CheckinTypes MR_createEntity];

What's the difference between those two possibilities?

هل كانت مفيدة؟

المحلول

The basic rule of thumb is:

  • when you are on a background thread, create a new context and perform your work on that context.

However, the real answer is more complex than that. Using Core Data in general requires managing your contexts for your particular scenario. One common scenario is a settings screen where settings are saved in Core Data. By using a new context, and making your changes in that context, you can then simply toss the context (ie. not save) it to remove any temporary changes the user has not committed to.

Also, please note that MR_contextForCurrentThread has been deprecated and will be removed in an upcoming release of MagicalRecord. Using this method will lead to occasional, hard to track down crashes.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top