为什么这个代码提高“CoreData:error:(19)PRIMARY KEY必须是唯一的”错误? [英] Why is this code raising the "CoreData: error: (19) PRIMARY KEY must be unique" error?

查看:130
本文介绍了为什么这个代码提高“CoreData:error:(19)PRIMARY KEY必须是唯一的”错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码引发CoreData:错误:(19)PRIMARY KEY必须是唯一的错误。
实体只有当属性是 NSDate ,以及一个名为 tasks 的一对多关系。为什么会出现这个错误?如果已经存储了具有特定日期的 Day ,则会提取它,否则我将其插入。因此,对于每一天的对象,当属性时应该有不同的。我不知道这是否是主键虽然。如何解决这个问题?先谢谢你。

This code raises the "CoreData: error: (19) PRIMARY KEY must be unique" error. The Day entity has only a when attribute which is an NSDate, and a to-many relationship called tasks. Why this error? If a Day with a specific date is already stored, I fetch it, otherwise I insert it. So, for each day object, there should be a different when attribute. I am not sure if this is the primary key though. How to solve this ? Thank you in advance.

   NSMutableSet *occurrences = nil;

   occurrences = ... 
   NSMutableOrderedSet *newSet = [NSMutableOrderedSet orderedSetWithCapacity:[occurrences count]];

   for(NSDate *current in occurrences) {

        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

        // try to find a corresponding Day entity whose when attribute is equal to the current occurrence
        // if none is available, create it

        Day * day = [[self getDayForDate:current inManagedObjectContext:moc] retain];
        if(!day){
            day = (Day *) [NSEntityDescription insertNewObjectForEntityForName:@"Day" inManagedObjectContext:moc];
        }

        day.when = current;
        [day addTasksObject:aTask];

        [newSet addObject:day];

        [moc insertObject:day];
        [moc processPendingChanges];

        [day release];

        [pool release];            
    } 

- (Day *)getDayForDate:(NSDate *)aDate inManagedObjectContext:(NSManagedObjectContext *)moc
{        
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Day" inManagedObjectContext:moc];
    [request setEntity:entity];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(when == %@)", aDate];
    [request setPredicate:predicate];
    NSError *error = nil;
    NSArray *array = [moc executeFetchRequest:request error:&error];
    [request release];

    Day *theDay = nil;

    if(array && [array count] == 1){
        theDay = [array objectAtIndex:0];
    }

    return theDay;
}


推荐答案

如果你已经有了,就需要插入一个新的 Day (这是day不是nil的情况)。特别是我指的是 [moc insertObject:day]

I guess you don't need to insert a new Day if you already have it (this is the case when day is not nil). In particular I'm referring to [moc insertObject:day].

如果使用 insertNewObjectForEntityForName ,当您保存moc时,该方法为您插入对象。如果你需要修改它(你已经检索到一个非零的一天)修改它并保存。另外,当循环完成(仅仅是为了性能原因)时,我会做 processPendingChanges

If you use insertNewObjectForEntityForName, that method inserts the object for you when you save the moc. If you need to modify it (you have retrieved a non-nil day) modify it and save. In additon, I will do processPendingChanges when the loop finishes (just for performance reasons).

希望有帮助。

这篇关于为什么这个代码提高“CoreData:error:(19)PRIMARY KEY必须是唯一的”错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆