데이타베이스 | 코어데이타 | |
데이타베이스 자체 | 관리객체 컨텍스트(Managed Object Context) | 관리객체를 담는 그릇 |
테이블 구조(스키마) | 엔티티 (NSEntityDescription) | |
테이블 구조를 이루는 세부컬럼 정보 | 프로퍼티 (Property) | |
테이블 내의 하나의 레코드 | 관리객체 (Managed Object) | |
SQL문 (예: select * from) | 갬색 요청(Fetch Request) | |
SQL 조건문 (where age > 20) | 조건 서술(Predicate) | |
저장소 관리자 | 저장소 관리자 (Persistent Store Coordinator) | 컨텍스트 내용을 파일이나 DB에 저장하는 객체 |
//프로그램종료시 저장
-(void)applicationWillTerminate:(NSNotification *)notification{
coreDataAppDelegate *appDelegate = [UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSError *error;
for(int i=1;i<=3;i++)
{
NSString *fieldName = [NSString stringWithFormat:@"line%d",i];
UITextField *field = [self valueForKey:fieldName];
NSFetchRequest *request = [NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Line" inManagedObjectContext:context];
[request setEntity:entity];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"(lineNum = %d)", i];
[request setPredicate:pred];
NSManagedObject *theLine = nil;
NSArray *array = [context executeFetchRequest:request error:&error];
if([array count] > 0)
theLine = [array objectAtIndex:0];
else
theLine = [NSEntityDescription insertNewObjectForEntityForName:@"Line" inManagedObjectContext:context];
[theLine setValue:[NSNumber numberWithInt:i] forKey:@"lineNum"];
[theLine setValue:field.text forKey:@"lineText"];
[request release];
}
[context save:&error];
}
-(void)ViewDidLoad{
coreDataAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Line" inManagedObjectContext:context];
[request setEntioty:entity];
NSError *error;
NSArray *array = [context executeFetchRequest:request error:&error];
for(ManagedObject *oneObject in array)
{
NSNumber *lineNum = [oneObject valueForKey:@"lineNum"];
NSString *lineText = [oneObject valueForKey:@"lineText"];
NSString *field = [NSString stringWithFormat:@"line%d", lineNum];
UITextField *txtField = [self valueForKey:field];
txtField.text = lineText;
}
[request release];
UIApplication *app = [UIApplication sharedApplication];
[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:app];
}
'Object-C' 카테고리의 다른 글
XML (0) | 2010.10.09 |
---|---|
SQLITE (0) | 2010.10.08 |
archiving (0) | 2010.10.07 |
pickerView (0) | 2010.10.07 |
imagePicker (0) | 2010.10.07 |