我正在通过核心数据将新对象插入数据库。在我插入值之前,有什么方法可以检查数据库中是否有任何重复项?
for (int i =0;i<[categoryArray count];i++)
{
Category * cat = [categoryArray objectAtIndex:i];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"ICategory" inManagedObjectContext:managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
ICategory *catt = (ICategory *)[NSEntityDescription insertNewObjectForEntityForName:@"ICategory" inManagedObjectContext:managedObjectContext];
[catt setName:cat.name];
[catt setID:cat.ID];
[catt setPhoto:cat.photo];
[catt setSapphireID:event.ID];
NSLog(@"cattttt have %@", catt);
}
每次我运行应用程序时,它都会再次重新插入值。我想检查其中是否有任何新类别,如果没有,那么我将仅添加该新类别。