RDBMS (관계형 데이터베이스)에 대한 생각을 멈추고 관리 대상 개체 모델에 대해 생각해보십시오. CoreData는 객체와 그 연관을 관리합니다. Athlete 객체를 Event 객체에 연결할 수 있습니다 (예 : 단편 대 복수형, 즉 운동 선수 및 이벤트 대 운동 선수 및 이벤트를 제안하고 일대일 관계 및 복수 대다 관계를 위해 단수를 사용하는 방법으로). 그것은 내가 선호하는 일이지만, 나는 자신을 Athletes (객체)가 아닌 Athlete (객체)라고 부릅니다. 모든 것을 더 읽기 쉽고 직관적으로 만듭니다.
@interface Athlete : NSManagedObject
@property (nonatomic, retain) NSString * name;
//... a bunch more attributes
@property (nonatomic, retain) Event *event; // use singluar for relationship name too
//...
@end
@interface Event : NSManagedObject
@property (nonatomic, retain) NSString * eventName;
//... a bunch more attributes
@property (nonatomic, retain) NSSet *athletes; // use plural for relationship name
//...
@end
@implementation MyViewController
//... some method
// fetch the athletes (possibly present in table view or other mechanism for selection)
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Athlete"];
NSArray *athletes = [managedObjectContext executeFetchRequest:fetchRequest error:nil];
// select the althletes (primary & opponent - hardcoded for example)
//...
NSArray *selectedAthletes = [NSArray arrayWithObjects: athletes[0], athletes[1], nil];
// create an event
Event *event = [NSEntityDescription
insertNewObjectForEntityForName:@"Event"
inManagedObjectContext:context];
// add the athletes
[event addAthletes:[NSSet setWithArray:selectedAthletes]];
//...
@end
지금 당신은이 운동 선수와의 이벤트가 : 이것은 당신이 관계를 설명하는 방법을 주어진처럼
가정 당신의 실체가 보인다. 당신이 상대와 도전자을 구분하고 싶었다면 당신은 만들 수 2 - 하나의 상대와 도전자와 같은 것을 통해 선수에 이벤트를 연결로 (선수로 이벤트에서) 관계 :
@interface Event : NSManagedObject
@property (nonatomic, retain) NSString * eventName;
//... a bunch more attributes
@property (nonatomic, retain) Athlete *opponent; // use singluar for relationship name
@property (nonatomic, retain) Athlete *challenger;
//...
@end
//...
event.opponent = athletes[0];
event.challenger = athletes[1];
//...
난 당신이 무슨 뜻인지 확실하지. 예를 들어 무엇이 잘못되었는지를 명확히 할 수 있습니까? 즉 당신이 한 일, 예상했던 일, 실제로 일어난 일. 그렇게하면 우리는 당신이 무엇이 도움이되는지 정확히 알고 있습니다. :) – Fogmeister
글쎄 내 앱이 하나의보기에서 운동 선수를 만들고 "활성"상태로 선택한 다음 다른보기에서 내가 최근 선택한 각 운동 선수 밑에서 여러 개의 이벤트를 만들 수있는 tableview 컨트롤러가있는 위치를 설정합니다. 기본적으로 두 텍스트 필드에서 텍스트를 가져 와서 테이블에 만들 수 있도록 해당 문자열을 내 엔터티에 추가하려고합니다. 관계를 사용하고 하나의 엔티티에 배수 객체를 추가하고이를 다른 객체의 한 객체에 연결하는 방법에 대해 전체 프로그램 측면에서 혼란스러워합니다. 비록 데이터 모델 다이어그램을 잘 이해합니다. 말이 돼? –
핵심 데이터 모델의 시각적 구조를 추가하십시오. 그리고이 자습서 시리즈를 살펴 보았습니다 : http://www.raywenderlich.com/934/core-data-tutorial-for-ios-getting-started? –