coreData现有验证
coreData existing validation
所以我正在使用 CoreData 填写申请表
首先,我使用唯一名称和一些属性创建 "Shop"。
在应用程序中,您可以编辑 "Shop",我正在尝试通过 "shopName" 进行验证,以避免创建另一个具有相同名称的 "Shop"。
我正在使用这个:
-(BOOL)uniqueEntityExistsWithEnityName {
BOOL returnValue = NO;
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Shop"];
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"shopName = [cd] %@", _shopName.text];
NSSortDescriptor *shop = [[NSSortDescriptor alloc] initWithKey:@"shopName" ascending:YES];
[request setSortDescriptors: @[shop]];
[request setPredicate:predicate];
NSError *error = nil;
NSArray *matches = [self.managedObjectContext executeFetchRequest:request error:&error];
NSLog(@"request = %@",predicate);
if (!matches) {
NSLog(@"Error: Couldn't execute fetch request %@", error);
}
else if([matches count] > 1) {
NSString *existShop = [NSString stringWithFormat:@"Could Be Only One %@ Shop", _shopName.text];
UIAlertView *exist = [[UIAlertView alloc]initWithTitle:@"Shop Exists in Your Records"
message:existShop
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[exist show];
NSLog(@"Error: Have more than %lu records",
(unsigned long)[matches count]);
returnValue = YES;
}
else {
NSLog(@"%lu object in record", (unsigned long)[matches count]);
[self oldShopDelete];
[self checkShopPhoneNumber];
editShop.shopName = _shopName.text;
editShop.shopPhoneNumber = _shopPhoneNumber.text;
editShop.shopSecondPhoneNumber = _shopSecondPhoneNumber.text;
editShop.shopEmail = _shopEmail.text;
editShop.shopNote = _shopNoteView.text;
[super saveAndDissmiss];
returnValue = YES;
}
return returnValue;
}
使用该代码,您仍然有机会保存另外一个具有相同名称的已编辑 "Shop"。
但问题是 - 我无法在这之后制作 [matches count] = 1
我将没有机会编辑那个商店
也许还有另一种方法可以进行这种验证?
仅在实际首次设置或编辑名称时才检查名称冲突。
您还可以将当前商店传递给谓词以确保AND SELF != %@
不会与正在编辑但名称未更改的现有商店匹配。
所以我正在使用 CoreData 填写申请表 首先,我使用唯一名称和一些属性创建 "Shop"。 在应用程序中,您可以编辑 "Shop",我正在尝试通过 "shopName" 进行验证,以避免创建另一个具有相同名称的 "Shop"。 我正在使用这个:
-(BOOL)uniqueEntityExistsWithEnityName {
BOOL returnValue = NO;
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Shop"];
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"shopName = [cd] %@", _shopName.text];
NSSortDescriptor *shop = [[NSSortDescriptor alloc] initWithKey:@"shopName" ascending:YES];
[request setSortDescriptors: @[shop]];
[request setPredicate:predicate];
NSError *error = nil;
NSArray *matches = [self.managedObjectContext executeFetchRequest:request error:&error];
NSLog(@"request = %@",predicate);
if (!matches) {
NSLog(@"Error: Couldn't execute fetch request %@", error);
}
else if([matches count] > 1) {
NSString *existShop = [NSString stringWithFormat:@"Could Be Only One %@ Shop", _shopName.text];
UIAlertView *exist = [[UIAlertView alloc]initWithTitle:@"Shop Exists in Your Records"
message:existShop
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[exist show];
NSLog(@"Error: Have more than %lu records",
(unsigned long)[matches count]);
returnValue = YES;
}
else {
NSLog(@"%lu object in record", (unsigned long)[matches count]);
[self oldShopDelete];
[self checkShopPhoneNumber];
editShop.shopName = _shopName.text;
editShop.shopPhoneNumber = _shopPhoneNumber.text;
editShop.shopSecondPhoneNumber = _shopSecondPhoneNumber.text;
editShop.shopEmail = _shopEmail.text;
editShop.shopNote = _shopNoteView.text;
[super saveAndDissmiss];
returnValue = YES;
}
return returnValue;
}
使用该代码,您仍然有机会保存另外一个具有相同名称的已编辑 "Shop"。
但问题是 - 我无法在这之后制作 [matches count] = 1
我将没有机会编辑那个商店
也许还有另一种方法可以进行这种验证?
仅在实际首次设置或编辑名称时才检查名称冲突。
您还可以将当前商店传递给谓词以确保AND SELF != %@
不会与正在编辑但名称未更改的现有商店匹配。