尝试将记录添加到数据库时出现此错误:

2012-02-12 20:15:18.187 Flavma[3197:707] CoreData: error: Serious application error.
An exception was caught from the delegate of NSFetchedResultsController during a call to -
controllerDidChangeContent:.  *** -[__NSArrayI objectAtIndex:]:
index 1 beyond bounds [0 .. 0] with userInfo (null)


我尝试过添加所有方法,但是我目前正在使用的是此类别:

#import "Patient+Create.h"

@implementation Patient (Create)

+ (Patient *)patientWithLastName:(NSString *)lastName inManagedObjectContext:(NSManagedObjectContext *)context
{
    Patient *patient = nil;
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Patient"];
    request.predicate = [NSPredicate predicateWithFormat:@"lastName = %@", lastName];
    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"lastName" ascending:YES];
    request.sortDescriptors = [NSArray arrayWithObject:sortDescriptor];

    NSError *error = nil;
    NSArray *patients = [context executeFetchRequest:request error:&error];

    if (!patients || ([patients count] > 1)) {
        //handle error
    } else if (![patients count]) {
        //create a new one
        patient = [NSEntityDescription insertNewObjectForEntityForName:@"Patient" inManagedObjectContext:context];
        patient.lastName = lastName;
    } else {
        patient = [patients lastObject];
    }
    return patient;
}

@end


首次创建数据时(如果我从设备中删除了应用程序),我便可以向数据库中添加数据,如下所示:

- (void) fetchPatientDataIntoDocument:(UIManagedDocument *)document
{
    dispatch_queue_t fetchQ = dispatch_queue_create("Patient fetcher", NULL);
    dispatch_async(fetchQ, ^{
        [document.managedObjectContext performBlock:^{
            [Patient patientWithLastName:@"Johnson" inManagedObjectContext:self.patientDatabase.managedObjectContext];
        }];
    });
    dispatch_release(fetchQ);
}


但是在那之后,我仍然遇到相同的错误。有任何想法吗?

最佳答案

#import "Patient+Create.h"

@implementation Patient (Create)
+ (Patient *)patientWithLastName:(NSString *)lastName inManagedObjectContext:(NSManagedObjectContext *)context
{
    Patient *patient = nil;
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Patient"];
    request.predicate = [NSPredicate predicateWithFormat:@"lastName = %@", lastName];
    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"lastName" ascending:YES];
    request.sortDescriptors = [NSArray arrayWithObject:sortDescriptor];

    NSError *error = nil;
    NSArray *patients = [context executeFetchRequest:request error:&error];

    if (!patients || [patients count]<=0) {
        //create a new one
        patient = [NSEntityDescription insertNewObjectForEntityForName:@"Patient" inManagedObjectContext:context];
        patient.lastName = lastName;
    [context performSelectorOnMainThread:@selector(save:) withObject:nil waitUntilDone:YES];
    } else {
        patient = [patients lastObject];
    }
    return patient;
}

@end


只需将新创建的对象保存在主线程上即可。
这样做的原因是,您在辅助线程(GCD)上创建对象,这些更改将不会生效,除非您将上下文保存在主线程上

[context performSelectorOnMainThread:@selector(save:) withObject:nil waitUntilDone:YES];

10-08 07:45
查看更多