0

로그인 (사용자 ID, 비밀번호)과 정보 (제목, 정보)의 두 엔티티가 있습니다. 이제 그들 사이에는 1 대 1의 관계가 있습니다. 사용자에게 고유 한 데이터베이스에 정보를 추가해야합니다.1 대 1 관계 코어 데이터 iOS

Login *information = [NSEntityDescription insertNewObjectForEntityForName:@"Login" 
                 inManagedObjectContext:self.managedObjectContext]; 

    information.information.title = informationTitleTextView.text; 
    information.information.info_1 = information1textview.text; 
    information.information.info_2 = information2textview.text; 

    [self.managedObjectContext save:nil]; // write to database 

    [self.delegate savebuttontapped:self]; 

그러나 그것은 내가 뭘 잘못, Working.I 모르는 아니라 어떤 도움을 주시면 감사하겠습니다 :

내 코드는 여기 아래에있다?.

+2

'정보'로 로그인 인스턴스를 이름 짓는 것은 확실히 혼란 스럽습니다. 또한,이 코드가 실행되고 있다면 * nothing *이 아닌 * 분명히 * 어떤 일이 일어나고 있습니다. 그렇게하지 않으면 일어날 것으로 예상되는 것을 설명하면 도움이 될 것입니다. '-save :'가 반환 할 수있는 오류를 무시하지 않았다면 도움이 될 것입니다. NSError 포인터의 주소를 전달하는 것이 얼마나 어렵습니까? – Caleb

+0

값을 특성에 할당하고 싶습니다 ...하지만 NSLog를 수행하고 그것을 검사하는 것이 NULL을 표시하고 아무 것도 데이터베이스에 기록되지 않을 때 ... – user1253637

+0

인증 세부 정보를 저장하기 위해 키 체인을 사용해보십시오. 이것은 그것이 설계된 것입니다. – Jim

답변

1

Information의 인스턴스를 컨텍스트에 추가하지 않았습니다. 이 시도 : 어떤 시점에서

login.userId = theUserId; 
login.password = thePassword; 

: 당신이 그 속성에 따라 로그인 개체를 가져올려고하는 경우에

물론
Login *login = [NSEntityDescription insertNewObjectForEntityForName:@"Login" inManagedObjectContext:self.managedObjectContext]; 
Information *information = [NSEntityDescription insertNewObjectForEntityForName:@"Information" inManagedObjectContext:self.managedObjectContext]; 

login.information = information; 
login.information.title = informationTitleTextView.text; 
//...and so on... 

, 실제로 그 속성에 뭔가를 저장할 수 있습니다 앞으로는 기준에 맞는 Login 객체 만 가져 오려고 할 것입니다. 일단 당신이 그것을 가지고 있다면 아무 문제없이 관련 정보 객체를 얻을 수 있습니다 :

NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Login" 
    inManagedObjectContext:managedObjectContext]; 
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"userId like %@ AND password like %@", theUserId, thePassword]; 
[request setPredicate:predicate]; 
NSError *err = nil; 
NSArray *matchingLogins = [self.managedObjectContext executeFetchRequest:request error:&err]; 
int count = [matchingLogins count]; 
if (count != 1) { 
    NSLog(@"Houston, we have a problem."); 
} 
Login *login = [matchingLogins objectAtIndex:0]; 
Information *info = login.information; // Notice: no separate fetch needed 
+0

감사합니다 ... 내 문제를 해결했습니다. 같은 줄에 이제 information.title을 가져 와서 해당 로그인에 고유하게 표시하려고합니다. – user1253637

+0

// 셀을 구성하십시오 ... 로그인 * 로그인; login = [self.fetchedResultsController objectAtIndexPath : indexPath]; 정보 * 정보; 정보 = [self.fetchedResultsController objectAtIndexPath : indexPath]; login.information = 정보; cell.textLabel.text = login.information.title; 반환 셀; } – user1253637

+0

@ user1253637 Information 개체를 별도로 가져올 필요가 없습니다.이 개체는 이미 Login 개체와 연결되어 있으므로 로그인 할 때 이미 정보를 얻었습니다. 사용자 ID와 비밀번호와 일치하는 로그인 하나만 가져 오는 다른 방법에 대한 업데이트 된 대답을 참조하십시오. 모든 사용자를 표시하려는 경우 유용하지는 않지만 유용 할 수 있습니다. – Caleb