Iphone 核心数据中自动递增的对象ID?
我正在使用几种具有多种关系的Iphone 核心数据中自动递增的对象ID?,iphone,core-data,xcode4,Iphone,Core Data,Xcode4,我正在使用几种具有多种关系的NSManagedObject类型。如何让核心数据为我自动填充对象ID?我正在寻找类似SQL中的索引键的东西,以便给定对象的任何两个实例都不允许具有相同的ID 编辑: 我希望我的所有“帐户”对象上都有唯一的ID。我只是在'countForFetchRequest'中添加了一个对象,但我意识到,当删除倒数第二个对象然后添加一个对象时,最后两个对象现在具有相同的ID 如何确保给定值对于我的“帐户”NSManagedObject的所有实例都具有唯一值 EDIT2: 我需要有
NSManagedObject
类型。如何让核心数据为我自动填充对象ID?我正在寻找类似SQL中的索引键的东西,以便给定对象的任何两个实例都不允许具有相同的ID
编辑:
我希望我的所有“帐户”对象上都有唯一的ID。我只是在'countForFetchRequest'中添加了一个对象,但我意识到,当删除倒数第二个对象然后添加一个对象时,最后两个对象现在具有相同的ID
如何确保给定值对于我的“帐户”NSManagedObject的所有实例都具有唯一值
EDIT2:
我需要有一个单独的ID用于排序。所有
NSManagedObjects
自动具有唯一的NSManagedObjectID
。没有自定义自动递增属性的概念,但自己编写一个当然很容易。我解决这个问题的方法是使用核心数据聚合。实际上,我最终自己分配了ID
从本质上说,我查询核心数据以获取我的实体的所有实体ID,然后遍历它们。如果我发现一个ID高于当前临时ID,我会使临时ID高于聚合ID。完成后,我自动拥有一个高于列表中最高的ID。我看到的唯一缺陷是缺少ID(我相信也有一个简单的修复方法)
根据您的EDIT2和Edit3,以下答案将对您有所帮助。。假设您的id字段为NSNumber,id为unsignedInt 1) 获取对应实体的所有记录
NSError *error = nil;
NSArray *array = [self fetchAllFileEntity:&error];
2) 查找属于该结果的最大数目
NSNumber *maxValue = nil;
if (array)
maxValue = [array valueForKeyPath:@"@max.uniqueId.unsignedIntegerValue"];
else
maxValue = [NSNumber numberWithUnsignedInteger:0];
3) 将maxValue+1指定给新实体
entity.uniqueId = [NSNumber numberWithUnsignedInteger:maxValue.unsignedIntegerValue+1];
我已经为上述问题提出了这个解决方案,希望它能对某些人有所帮助
AppDelegate *appdelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appdelegate managedObjectContext];
NSError *error = nil;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *chatHist = [NSEntityDescription
entityForName:@"ChatHistory" inManagedObjectContext:context];
[fetchRequest setEntity:chatHist];
int chatIdNumber = 0;
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
if ([fetchedObjects count] > 0) {
ChatHistory *chatHistObj = [fetchedObjects objectAtIndex:[fetchedObjects count]-1];
chatIdNumber = [chatHistObj.chatId intValue];
}
chatIdNumber = chatIdNumber+1;
ChatHistory *chat_History = [NSEntityDescription insertNewObjectForEntityForName:@"ChatHistory" inManagedObjectContext:context];
chat_History.chatId = [NSString stringWithFormat:@"%d",chatIdNumber];
我可以使用NSManagedObjectID从核心数据存储中获取特定项吗?是的,请看,我实际上最终使用聚合编写了自己的自动增量。如果你想的话,请看我的答案+我不会用“容易”这个词“痛苦”更为恰当。可以理解,这不是核心数据的工作。但仍然…您不需要获取所有行-如果您使用Magic record entity.entityID=@([[entity findFirstOrderedByAttribute:@“entityID”升序:NO]entityID].intValue+1),则只需使用按id谓词降序排序即可;当我删除一些行(具有最大值的行)时会发生什么?用ex解释:假设您有5行的意思,@max count是5。现在删除第5行意味着,最大计数为4。所以,当您尝试添加新数据时,会自动将5分配给新数据,也就是说,表中的所有行(如果存在)都应该具有唯一的值(本地db)。我面临着相同的问题,任何一个都可以提供适当的解决方案。
AppDelegate *appdelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appdelegate managedObjectContext];
NSError *error = nil;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *chatHist = [NSEntityDescription
entityForName:@"ChatHistory" inManagedObjectContext:context];
[fetchRequest setEntity:chatHist];
int chatIdNumber = 0;
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
if ([fetchedObjects count] > 0) {
ChatHistory *chatHistObj = [fetchedObjects objectAtIndex:[fetchedObjects count]-1];
chatIdNumber = [chatHistObj.chatId intValue];
}
chatIdNumber = chatIdNumber+1;
ChatHistory *chat_History = [NSEntityDescription insertNewObjectForEntityForName:@"ChatHistory" inManagedObjectContext:context];
chat_History.chatId = [NSString stringWithFormat:@"%d",chatIdNumber];