我一直在尝试在核心数据中添加对象。所以,我希望它不应该允许核心数据存储中的重复条目。怎么做? 这是我保存数据的代码。
-(IBAction)save:(id)sender
{
if([name.text isEqualToString:@""] && [address.text isEqualToString:@""] && [phone.text isEqualToString:@""])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Yo..!"
message:@"Data Not Saved"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
else
{
coreDataAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSManagedObject *newContact;
newContact = [NSEntityDescription
insertNewObjectForEntityForName:@"Contacts"
inManagedObjectContext:context];
[newContact setValue:name.text forKey:@"name"];
[newContact setValue:address.text forKey:@"address"];
[newContact setValue:phone.text forKey:@"phone"];
name.text = @"";
address.text = @"";
phone.text = @"";
NSError *error;
[context save:&error];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Yo..!"
message:@"Data Saved"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
NSLog(@"Object Saved\n");
}
}
答案 0 :(得分:6)
由于没有可用的内置方法,您需要获取结果并检查结果是否包含您不想复制的对象。
以下是代码段:
-(void)checkForDuplicates
{
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Students"
inManagedObjectContext:managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"students"
ascending:NO];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
[request setSortDescriptors:sortDescriptors];
[sortDescriptor release];
NSError *Fetcherror;
NSMutableArray *mutableFetchResults = [[managedObjectContext
executeFetchRequest:request error:&Fetcherror] mutableCopy];
if (!mutableFetchResults) {
// error handling code.
}
if ([[mutableFetchResults valueForKey:@"users"]
containsObject:name.text]) {
//notify duplicates
return;
}
else
{
//write your code to add data
}
}
希望这可以帮到你!
答案 1 :(得分:2)
不,coredata没有内置于uniquing,因为它不是一个DB。
你必须确保程序逻辑的唯一性。
e.g。通常,人们对一个应该是唯一的条目进行提取,如果该提取有1个条目,则不要添加另一个条目,否则添加它!
==>这适用于串行CD访问,但在多线程环境中运行的多个上下文可能会很复杂
答案 2 :(得分:1)
在Core Data中,没有重复条目,至少就核心数据而言。不是从数据库的角度来看它。这是有道理的,因为Core Data不是数据库,它是一个对象图管理系统。
因此,为了防止重复,您需要首先进行搜索,然后如果搜索返回NULL
,则只保存,否则不执行任何操作。
This article为您提供可根据需要自定义的代码
答案 3 :(得分:0)
让我从Apple示例代码本身中提取最佳方法。请参考示例代码" ThreadedCoreData"了解更多信息。
https://developer.apple.com/library/content/samplecode/ThreadedCoreData/Introduction/Intro.html
// before adding the earthquake, first check if there's a duplicate in the backing store
NSError *error = nil;
Earthquake *earthquake = nil;
for (earthquake in earthquakes) {
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"location = %@ AND date = %@", earthquake.location, earthquake.date];
NSArray *fetchedItems = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
if (fetchedItems.count == 0) {
// we found no duplicate earthquakes, so insert this new one
[self.managedObjectContext insertObject:earthquake];
}
}