iphone - 获取时核心数据中的重复记录

标签 iphone objective-c cocoa cocoa-touch core-data

各位,

我的 iPhone 项目中有一些涉及 Core Data 的谜题。在两个单独的 View Controller 实例中的 viewDidLoad 方法期间,我调用以下方法:

- (NSFetchedResultsController *)getStudentResults:(NSString *)classRoom forWeekStarting:(NSDate *)startDate andEnding:(NSDate *)endDate {
    AttendanceAppDelegate *appDelegate = (AttendanceAppDelegate *)[[UIApplication sharedApplication] delegate];

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:[appDelegate managedObjectContext]];

    int secondsInAWeek = 60 * 60 * 24 * 7;
    NSDate *today = [[NSDate alloc] init];
    NSDate *nextWeek = [[NSDate alloc] initWithTimeIntervalSinceNow:secondsInAWeek];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(student.classRoom like %@) AND (dateTime >= %@) AND (dateTime <= %@)", classRoom, startDate, endDate];
    [request setPredicate:predicate];
    [request setEntity:entityDescription];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"dateTime" ascending:YES];
    NSArray *descriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [request setSortDescriptors:descriptors];
    [descriptors release];
    [sortDescriptor release];
    [nextWeek release];
    [today release];


    NSFetchedResultsController *fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:[appDelegate managedObjectContext] sectionNameKeyPath:nil cacheName:@""];
    NSError *error; 
    if (![fetchedResultsController performFetch:&error])
        NSLog(@"Error performing fetch on fetchedResultsController: %@", [error localizedDescription]);

    if (fetchedResultsController == nil || [[fetchedResultsController fetchedObjects] count] == 0) { 
        Student *student = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:[appDelegate managedObjectContext]];
        NSData *classRoomStudentData = [[NSData alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"STUDENT_ATTENDANCE" ofType:@"XML"]];
        [student setClassRoom:@"default"];
        [student buildWithStudentData:classRoomStudentData startingWithXPathNode:@"//attendance"];

        NSError *error; 
        if (![[appDelegate managedObjectContext] save:&error]) { 
            NSLog(@"Error saving the managedObjectContext: %@", [error localizedDescription]);
        }

        if (![fetchedResultsController performFetch:&error])
            NSLog(@"Error performing fetch on fetchedResultsController: %@", [error localizedDescription]);
    }
    return fetchedResultsController;
}

每次运行应用程序时,Student 对象的数量都会随着原始实体的数量而增加(每个原始实体都会重复一次)。因此,如果我从 4 开始,下次我会得到 8,然后是 12,然后是 16,等等。我似乎无法弄清楚为什么,因为解析 XML 的代码仅在获取没有返回对象时才会被调用。

如有任何帮助,我们将不胜感激,谢谢。

-- 迈克尔

最佳答案

您不仅在获取未返回对象时插入新对象:查看您的代码

if (fetchedResultsController == nil || [[fetchedResultsController fetchedObjects] count] == 0)

当您无法正确初始化 NSfetchedResultsController 时,您也可以插入它们。因此,如果每次添加新对象时都无法正确初始化它。

建议初始化 NSfetchedResultsController,如 Apple 提供的所有示例代码所示。基本上,您首先验证 fetchedResultsController 是否为零。如果没有,您只需返回该变量即可。否则,您将对其进行初始化。

请参阅我对此问题的回答作为示例:

How to properly configure NSFetchedResultsController

关于iphone - 获取时核心数据中的重复记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1452371/

相关文章:

iphone - SIGABRT 启动

iphone如何指定Class数据类型必须采用某种协议(protocol)

objective-c - "Expected a Type"编译器应该知道的协议(protocol)错误

objective-c - 如何在 cocoa 应用程序中对单选按钮(来自 Storyboard)进行分组?

objective-c - 当用户右键单击 Mac 上 Cocoa 应用程序的停靠图标时,如何创建菜单?

iphone - 移动平台之间的可移植性

ios - 如何从 iPhone 专用应用程序过渡到 iPhone 和 iPad 应用程序?

iphone - GCD 获取队列名称/标签

iphone - 异步 NSURLConnection 抛出 EXC_BAD_ACCESS

c++ - Objective-C++内存问题