物体没有变成故障

时间:2013-08-06 22:17:49

标签: ios core-data nsmanagedobjectcontext fault

问题随机发生......

崩溃位置(这是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
}];

我对NSEntityDescription使用managedObjectContentChild。 但是,由managedObjectContent执行executeFetchRequest,这是managedObjectContentChild的父级。 这是潜在的问题吗?

我尝试了managedObjectContentChild的executeFetchRequest,然而,它引发了更多不同的问题。

但是,我绑定使用managedObjectContentChild,因为程序通过为单个线程创建新的CoreDataManager实例在多个线程中运行。程序将在不使用子MOC的情况下陷入死锁。

提前致谢!

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;
}

我试图找出以下讨论,但仍然不知道如何做到这一点...... 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

1 个答案:

答案 0 :(得分:0)

这是问题,

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

所以我使用managedObjectContextChild(子MOC)代替managedObjectContext(父MOC),以便为每个单独的线程创建不同的MOC。作为CoreData的并发规则。

使用managedObjectContext(父MOC)不会导致对象的错误没有转向错误并且每次都会崩溃应用程序,但如果应用程序恰好使用相同的MOC(那么,只有一个managedObjectContext)这是严重的问题在这种情况下)甚至从不同的线程完全相同的时刻。