2014-03-27 2 views
1

iOS의 코어 데이터 (SQLite) db에 새로 추가 된 필드에 인식 할 수없는 선택기가 나타납니다. Xcode의 Editor 메뉴를 사용하여 지시 된대로 새 모델 버전을 추가 한 다음 새 버전이 현재 버전인지 확인했습니다. 수정 된 테이블에 대한 .h 및 .m 파일이 업데이트되었지만, 이것을 손으로 만들었지 만 (이 파일을 생성하는 방법은?). 특별한 것은 없지만, 단지 String 타입 필드.경량 코어 데이터 마이그레이션 후 인식 할 수없는 선택 자 발생

문제는 데이터베이스 개체를 참조하려고하는 시간 코드가 실행 되어도 경량 마이그레이션이 수행되지 않는 것 같습니다. newFieldName 액세스 할 트링하여 준다 :

-[MyEntity newFieldName]: unrecognized selector sent to instance 0x852b510 
2014-03-27 13:26:21.734 ASSIST for iPad[41682:c07] *** Terminating app due to 
uncaught exception 'NSInvalidArgumentException', reason: '-[MyEntity newFieldName]: 
unrecognized selector sent to instance 0x852b510' 

상기 오류를 생성 코드 라인 아래 for 루프에서 유일한 라인이다 바와 같이

DataStoreCoreData *dStore = [[DataStoreCoreData alloc] initWithDataItemDescription:did]; 

for (MyEntity *myEnt in [dStore objects]) 
    NSString *name = [myEnt newFieldName];  

, I는 SQLite는 검사 때 갖는 DB를 새로운 필드가 없으므로 오류가 발생하면 의미가 있습니다. 그래서 나는 또한 마이그레이션을 수행해야하는 코드의 실행을 밟았으며 정상적으로 작동하는 것처럼 보입니다. 성공. 다음과 같이 표시됩니다.

NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"ASSIST.sqlite"]]; 

// handle db upgrade 
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: 
         [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 
         [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; 

NSError *error = nil; 
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]]; 

if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) 
    NSLog(@"\r\n Fail. [error localizedDescription]: %@", [error localizedDescription]); 
else 
    NSLog(@"\r\n Success"); 

이것은 6 번째 db 버전 업그레이드입니다. 모두 비정상적인 문제없이 가벼운 마이그레이션이 이루어졌습니다. 위의 코드가 새로운 스키마를 반영하도록 SQLite db를 강제 적용하면 안됩니까? 어떻게해야합니까, 아니면 여기에 또 다른 문제가 있습니까?

답변

0

나는 결국 대답을 발견했다. 질문에 제공 한 코드는 데이터베이스를 최소한으로 변경할 때 수정해야하는 코드의 일부일뿐입니다 (간단한 마이그레이션). 또한 개체 모델을 만들 때 새 버전을 지정해야합니다. 아래 코드에서 "MyNewVersion"을 확인하십시오. 이 매개 변수를 새로 작성한 다음 현재 모델 버전으로 반영하도록 업데이트해야합니다.

NSString *path = [[NSBundle mainBundle] pathForResource:@"MyNewVersion" ofType:@"mom" inDirectory:@"ASSIST.momd"]; 
NSURL *momURL = [NSURL fileURLWithPath:path]; 
managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL];