我有这个核心数据模型。我有一个学生,学生有父母(NSSet父母),父母有一些属性,也有属性addressHome和addressEmployer(地址也是对象所以他们有两个简单的对象 - addressHome和addressEmployer - 不是NSSet)。现在,我需要改变父母。我使用此代码更改父项属性,但工作非常奇怪。有时只保存母亲和父亲的一般数据,有时也是母亲的两个地址,但绝不是父亲的任何地址。我不理解它...我尝试调试,textFields填充正确,所以没有问题。你能告诉我吗?这是添加新学生的简单代码。
// add new student
if(!self.selectedStudentID){
Student *student = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:cdh.context];
// student hasnt parents --> create new
if(student.parents.count == 0){
Parent *mother = [NSEntityDescription insertNewObjectForEntityForName:@"Parent" inManagedObjectContext:cdh.context];
mother.type = @"mother";
[student addParentsObject:mother];
Parent *father = [NSEntityDescription insertNewObjectForEntityForName:@"Parent" inManagedObjectContext:cdh.context];
father.type = @"father";
[student addParentsObject:father];
}
for (Parent *parent in student.parents) {
// mother
if([parent.type isEqualToString:@"mother"]){
parent.name = motherVC.motherNameTextField.text;
parent.job = motherVC.motherJobTextField.text;
parent.phone = motherVC.motherPhoneHomeTextView.text;
parent.phoneWork = motherVC.motherPhoneWorkTextView.text;
// creating an AddressHome
if(!parent.addressHome){
Address *addressMotherHome = [NSEntityDescription insertNewObjectForEntityForName:@"Address" inManagedObjectContext:cdh.context];
parent.addressHome = addressMotherHome;
}
// assign values
parent.addressHome.city = motherVC.motherAddressCityTextField.text;
parent.addressHome.street = motherVC.motherAddressStreetTextField.text;
parent.addressHome.postcode = motherVC.motherAddressPostCodeTextField.text;
parent.addressHome.houseNumber = motherVC.motherAddressHouseNumberTextField.text;
// Creating an AddressEmployer
if(!parent.addressEmployer){
Address *addressMotherEmployer = [NSEntityDescription insertNewObjectForEntityForName:@"Address" inManagedObjectContext:cdh.context];
parent.addressEmployer = addressMotherEmployer;
}
// assign values
parent.addressEmployer.city = motherVC.motherEmployerAddressCityTextField.text;
parent.addressEmployer.street = motherVC.motherEmployerAddressStreetTextField.text;
parent.addressEmployer.postcode = motherVC.motherEmployerAddressPostCodeTextField.text;
parent.addressEmployer.houseNumber = motherVC.motherEmployerAddressHouseNumberTextField.text;
[cdh saveContext];
}
// father
else if([parent.type isEqualToString:@"father"]){
parent.name = fatherVC.fatherNameTextField.text;
parent.job = fatherVC.fatherJobTextField.text;
parent.phone = fatherVC.fatherPhoneHomeTextView.text;
parent.phoneWork = fatherVC.fatherPhoneWorkTextView.text;
// creating addressHome
if(!parent.addressHome){
Address *addressFatherHome = [NSEntityDescription insertNewObjectForEntityForName:@"Address" inManagedObjectContext:cdh.context];
parent.addressHome = addressFatherHome;
}
// assign values
parent.addressHome.city = fatherVC.fatherAddressCityTextField.text;
parent.addressHome.street = fatherVC.fatherAddressStreetTextField.text;
parent.addressHome.postcode = fatherVC.fatherAddressPostCodeTextField.text;
parent.addressHome.houseNumber = fatherVC.fatherAddressHouseNumberTextField.text;
// creating address-employer
if(!parent.addressEmployer){
Address *addressFatherEmployer = [NSEntityDescription insertNewObjectForEntityForName:@"Address" inManagedObjectContext:cdh.context];
parent.addressEmployer = addressFatherEmployer;
}
// assign values
parent.addressEmployer.city = fatherVC.fatherEmployerAddressCityTextField.text;
parent.addressEmployer.street = fatherVC.fatherEmployerAddressStreetTextField.text;
parent.addressEmployer.postcode = fatherVC.fatherEmployerAddressPostCodeTextField.text;
parent.addressEmployer.houseNumber = fatherVC.fatherEmployerAddressHouseNumberTextField.text;
[cdh saveContext];
}
}
// assign values for student
student.name = studentVC.studentNameTextField.text;
student.birthNumber = studentVC.studentBirthNumberTextField.text;
student.birthplace = studentVC.studentBirthPlaceTextField.text;
student.identityCard = studentVC.studentIdentityCardTextField.text;
student.nationality = studentVC.studentNationalityTextField.text;
student.legalRepresentative = studentVC.studentLegalRepresentativeTextField.text;
student.entranceToSchool = studentVC.studentEntranceToSchoolTextField.text;
student.otherSchool = studentVC.studentOtherSchoolTextField.text;
student.phone = studentVC.studentPhoneTextField.text;
student.email = studentVC.studentEmailTextField.text;
student.healthInsurer = studentVC.studentHealthInsurerTextField.text;
student.codeOfHealthInsurer = studentVC.studentCodeOfHealthInsurerTextField.text;
student.healthProblem = studentVC.studentHealthProblemTextView.text;
student.releaseFromGym = studentVC.studentReleaseFromGymTextView.text;
[cdh saveContext];
// end of new student
}