Question

The problem randomly occurs...

Crashing Location (which is a method in NSOperationQueue)

[self.requestOperationQueue addOperationWithBlock: ^{

    NSArray *titleList = [[NSMutableArray alloc] init];
    NSArray *allBooks = [[CoreDataManager sharedInstance] fetchBooks];

    for (Book *book in allBooks)
        [titleList addObject:book.title];   // program crashed here!! failed to fault the value of book.title
}];

I use managedObjectContentChild for NSEntityDescription. However, executeFetchRequest by managedObjectContent, which is the parent of managedObjectContentChild. Is that the potential problem?

I tried executeFetchRequest by managedObjectContentChild, however, it leads many more different issues.

However, I am binded to use managedObjectContentChild since program is running in multiple threads by create new CoreDataManager instance for individual thread. Program will run into deadlock without using children MOC.

Thanks in advance!

CoreDataManager.m

- (id)init
{
if ((self = [super init]) != nil)
{
    delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    // Writer (write data to Persistent Store Coordinator)
    writerManagedObjectContext = [delegate writerManagedObjectContext];

    // Parent (Fetched Result Controller)
    managedObjectContext = [delegate managedObjectContext];

    // Child (handling Object Context Saving for individual threads)
    managedObjectContextChild = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
    managedObjectContextChild.parentContext = managedObjectContext;

    bookEntity = [NSEntityDescription entityForName:[Book description]
                                    inManagedObjectContext:managedObjectContextChild];

    friendEntity = [NSEntityDescription entityForName:[Friend description] 
                                    inManagedObjectContext:managedObjectContextChild];
    }
    return self;   
}


- (NSArray *)fetchBooks
{
    // Todo: fix the problem of "CoreData: error: NULL _cd_rawData but the object is not being turned into a fault"

    NSArray *results = nil;

    if (key == nil)
        return results;

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:key ascending:ascending];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
[request setPredicate:predicate];
[request setSortDescriptors:sortDescriptors];

    NSError *error = NULL;

    //    results = [managedObjectContextChild executeFetchRequest:request error:&error];
    results = [managedObjectContext executeFetchRequest:request error:&error];

    if (error != NULL)
    NSLog(@"Error fetching - %@", error);

return results;
}

I tried to figure out the following discussion, but still have no clue how to do it... CoreData: error: NULL _cd_rawData but the object is not being turned into a fault http://www.cocoabuilder.com/archive/cocoa/311615-weird-core-data-crash.html

Was it helpful?

Solution

Here is the problem,

// use this one
results = [managedObjectContextChild executeFetchRequest:request error:&error];
// not this
// results = [managedObjectContext executeFetchRequest:request error:&error];

so I use managedObjectContextChild (child MOC) instead of managedObjectContext (parent MOC) in order to create distinct MOC for each individual threads. As a rules of concurrency of CoreData.

using managedObjectContext (parent MOC) will not cause the error of object not turning to fault and crashes the app every single time, but it's serious issue if the app happened to be using the same MOC (well, there is only one managedObjectContext in this case) at the exactly the same moment even from different threads.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top