iOS 8+ Core Data Faults When Resuming App From Background
<p>This issue only occurs in iOS 8 or higher. I have a core data managed object that I'm assigning to the property of a <code>UIViewController</code>. Whenever I leave the app and resume it from the background, the managed object faults. Whenever I attempt to access a property on the object, the fault does not fire and all of the data returns <code>nil</code>.</p>

<p>I set an observer for <code>UIApplicationWillEnterForegroundNotification</code> to examine the <code>selectedObject</code>, and at that point in the code execution the object has not faulted yet. It only faults following the app entering the foreground. Does anyone have any idea what could be happening here?</p>

<h2>Edit #1:</h2>

<p>Here's more of the relevant files. Note these have been simplified and variable names changed to protect the original code:</p>

<p><strong>myAppDelegate.h</strong></p>

<pre><code>@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
</code></pre>

<p><strong>myAppDelegate.m</strong></p>

<pre><code>@synthesize managedObjectContext = __managedObjectContext;

- (NSManagedObjectContext *)managedObjectContext
{
    if (__managedObjectContext != nil)
    {
        return __managedObjectContext;
    }

    // This code only gets hit the first time the app tries to access the context.
    // After that (including when the app resumes from the background), the one stored in __managedObjectContext is returned.
    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil)
    {
        __managedObjectContext = [[NSManagedObjectContext alloc] init];
        [__managedObjectContext setPersistentStoreCoordinator:coordinator];
    }
    return __managedObjectContext;
}
</code></pre>

<p><strong>DetailView.h</strong></p>

<pre><code>@property (retain, nonatomic) MyObject *selectedObject;
</code></pre>

<p><strong>DetailView.m</strong></p>

<pre><code>@synthesize selectedObject;

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(willEnterForeground)
                                                 name:UIApplicationWillEnterForegroundNotification
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didBecomeActive)
                                                 name:UIApplicationDidBecomeActiveNotification
                                               object:nil];

- (void)willEnterForeground
{
    NSLog(@"App will enter foreground."); // breakpoint here
}

- (void)didBecomeActive
{
    NSLog(@"App became active."); // breakpoint here
}

- (void)viewWillAppear:(BOOL)animated
{
    NSManagedObjectContext *context = ((myAppDelegate*)[[UIApplication sharedApplication] delegate]).managedObjectContext;
    NSError *error = nil;
    NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"MyObject" inManagedObjectContext:context];
    NSPredicate *predicate  = [NSPredicate predicateWithFormat:@"(selected == YES)", nil];
    [request setEntity:entity];
    [request setPredicate:predicate];
    selectedObject = (MyObject*)[[context executeFetchRequest:request error:&amp;error] objectAtIndex:0];
}

- (IBAction)buttonPressed:(id)sender
{
    NSString *link = [selectedObject link]; // breakpoint here
}
</code></pre>

<p>When I hit the breakpoints in <code>willEnterForeground</code> and <code>didBecomeActive</code> I check the <code>selectedObject</code>:</p>

<blockquote>
  <p>MyObject: 0x7fa801c80bc0 (entity: MyObject; id: 0xd000000020940002 x-coredata://9775DE4D-2312-4684-904B-613302AC2B19/MyObject/p2085 ; data: { link = "<a href="http://www.example.com" rel="nofollow">http://www.example.com</a>" }</p>
</blockquote>

<p>I also check <code>[selectedProperty managedObjectContext]</code> and the <code>[myAppDelegate managedObjectContext]</code>, both give me the following:</p>

<blockquote>
  <p>NSManagedObjectContext: 0x7fa7fbc96c90</p>
</blockquote>

<p>Now if I click the button that is bound to <code>buttonPressed:</code> and re-check everything, <code>[myAppDelegate managedObjectContext]</code> still gives me the same output above, but <code>[selectedObject managedObjectContext]</code> is <code>nil</code> and examining the object gives the following:</p>

<blockquote>
  <p>MyObject: 0x7fa801c80bc0 (entity: MyObject; id: 0xd000000020940002 x-coredata://9775DE4D-2312-4684-904B-613302AC2B19/MyObject/p2085 ; data: fault)</p>
</blockquote>

<p>And when <code>[selectedObject link]</code> is accessed it returns <code>nil</code>. To the best of my knowledge, none of my code is being run following the <code>willEnterForeground</code> and <code>didBecomeActive</code> methods when the app resumes from the background.</p>