0
下面是我的更新方法无法更新核心数据对象正确
-(void)updateData:(NSString *)doctorName hospitalName:(NSString *)hospitalName emailAdd:(NSString *)emailAdd phoneNum:(NSString *)phoneNum mobileNum:(NSString *)mobileNum
{
AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"DoctorInfo" inManagedObjectContext:delegate.persistentContainer.viewContext];
NSFetchRequest *request = [NSFetchRequest new];
[request setEntity:entityDesc];
NSString *query = doctorName;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(doctorName = %@)", query];
[request setPredicate:predicate];
NSError *error;
NSAsynchronousFetchResult *storeResult = [delegate.persistentContainer.viewContext executeRequest:request error:&error];
NSArray *result = storeResult.finalResult;
DoctorInfo *firstResult = [result firstObject];
firstResult.doctorName = doctorName;
firstResult.hospitalName = hospitalName;
firstResult.emailAdd = emailAdd;
firstResult.phoneNumber = phoneNum;
firstResult.mobileNumber = mobileNum;
if (![delegate.persistentContainer.viewContext save:&error]) {
NSLog(@"Couldn't edit: %@", error);
}
}
我能够更新除了doctorName
所有的变量。我认为这可能是由于这行代码:
NSString *query = doctorName;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(doctorName = %@)", query];
[request setPredicate:predicate];
我应该如何修改这个方法,这样我就可以更新doctorName呢?
你要什么给医生名称更改为?您在代码中唯一的值是您用于搜索记录的值 – Paulw11