Skip to content
This repository

Add method to clear thread-local context #401

Closed
wants to merge 2 commits into from

2 participants

Art Gillespie Tony Arnold
Art Gillespie

When running unit tests that are asynchronous/multi-threaded, I find that oftentimes a background thread will have an NSManagedObjectContext instance (created by MR_contextForCurrentThread) from a previous set up, even after calling [MagicalRecord cleanUp]

When this happens, I get the dreaded "Object's persistent store is not reachable from this NSManagedObjectContext's coordinator" error. Removing the MOC's from each thread's threadDictionary solves the problem.

Tony Arnold
Owner

Superseded by #542 (same changes, sans the merge). I need to write a couple of tests before merging into develop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
1  MagicalRecord/Categories/NSManagedObjectContext/NSManagedObjectContext+MagicalThreading.h
@@ -12,5 +12,6 @@
12 12
13 13 + (NSManagedObjectContext *) MR_contextForCurrentThread;
14 14 + (void) MR_resetContextForCurrentThread;
  15 ++ (void) MR_clearContextForCurrentThread;
15 16
16 17 @end
4 MagicalRecord/Categories/NSManagedObjectContext/NSManagedObjectContext+MagicalThreading.m
@@ -38,4 +38,8 @@ + (NSManagedObjectContext *) MR_contextForCurrentThread;
38 38 }
39 39 }
40 40
  41 ++ (void) MR_clearContextForCurrentThread {
  42 + [[[NSThread currentThread] threadDictionary] removeObjectForKey:kMagicalRecordManagedObjectContextKey];
  43 +}
  44 +
41 45 @end

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.