2012-03-06 3 views
7

앱 업그레이드 릴리스에서 개체 모델을 상당히 변경했습니다. 엔티티 추가/제거, 새로운 속성 및 관계. 실제로 중요한 핵심 데이터 마이그레이션이 추가되는 것처럼 보입니다. 데이터는 주로 오프라인 브라우징 환경을 향상시키는 캐시 역할을하기 때문에 이 시점에서 실제로는 이 필요합니다. 이전 제가 방금 날아가서 다시 만들어지면 훨씬 더 간단해질 것입니다. 내가 코어 데이터를 영구적으로 삭제하는 대신 마이그레이션 (RestKit 사용)

  • 에 일반적인 전략은이 주제에 건너 한 다양한 글을 바탕으로

    모델이

  • 은 삭제합니다 (managedObjectContext의 초기화하는 동안 예외를 잡기에 의해) 변경되었음을 감지 최신 스키마가 새로운 모델
와 영구 저장소 를 다시 초기화와 영구 저장소 (iOS의 우리의 경우 sqlite가 파일에)
  • 는 objectModel를 다시 초기화

    이는 objectModel를

    - (NSManagedObjectModel *)managedObjectModel { 
    
    if (managedObjectModel != nil) { 
        return managedObjectModel; 
    } 
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"<model name>" ofType:@"momd"]; 
    NSURL *momURL = [NSURL fileURLWithPath:path]; 
    managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL]; 
    
    return managedObjectModel; 
    } 
    

    과 objectModel를 재 작성을 다시 초기화하고 그러나

    objectManager = [RKObjectManager objectManagerWithBaseURL: 
            [NSString stringWithFormat:@"http://%@/v3", 
             [[NSBundle mainBundle] objectForInfoDictionaryKey:@"APIDomain"]]];  
    NSManagedObjectModel *objectModel = [self managedObjectModel]; 
    objectManager.objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:storeName usingSeedDatabaseName:nil managedObjectModel:objectModel delegate:nil]; 
    

    에 저장하는 코드입니다, 나는 다음과 같은 오류 얻을 :

    Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'UTCity''

    내가 같은 느낌을 앱을 다시 시작하면 새 상점을 성공적으로 만들었으므로 거의 닫힙니다.

    -pf

  • +0

    이 모든 행운을 빕니다. – CharlieMezak

    답변

    6

    나는 당신이 RKManagedObjectStoreDelegate에서 방법을 구현하여 설명하고 일을 할 수 있었던 것 같아요. 이 메소드는 지속적 저장소 작성이 실패 할 때 호출됩니다. 이 메서드가 호출 될 때 영구 저장소를 삭제하기 만하면됩니다. RestKit은 이것으로부터 회복 된 것 같습니다. 다음 번에 새로운 빈 저장소를 만들었다 고 가정합니다. 대리자 개체를 받아들이는 방법 중 하나를 통해 RKManagedObjectStore의 인스턴스를 초기화해야합니다 있도록

    - (void)managedObjectStore:(RKManagedObjectStore *)objectStore didFailToCreatePersistentStoreCoordinatorWithError:(NSError *)error { 
        [objectStore deletePersistentStore]; 
    } 
    

    RKManagedObjectStore 시도는, 초기화시 영구 저장소를 만들 수 있습니다. 방금 애플 리케이션 대리인을 전달했습니다.

    지금까지는 작동하는 것으로 보입니다. 계속 발전하면서 우리는 그것이 계속 그렇게 할 것인지를 볼 것입니다.

    +0

    '- [RKManagedObjectStore resetPersistentStores :]'는 RestKit 0.20.x에서 동일한 것 같습니다. –

    +1

    이 대리자 기능 managedObjectStore : didFailToCreatePersistentStoreCoordinatorWithError : 0.20에서 사라진 것 같습니다. 대체품에 대해 알고 있습니까? – mosca1337

    3

    마이그레이션이 실패 할 때 영구 저장소를 모두 삭제하는 솔루션입니다.

    // Core Data Persistent Store 
        NSError *error; 
        NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"Data.sqlite"]; 
        NSPersistentStore __unused *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath 
                           fromSeedDatabaseAtPath:nil 
                            withConfiguration:nil 
                              options:@{NSInferMappingModelAutomaticallyOption: @YES, NSMigratePersistentStoresAutomaticallyOption: @YES} 
                               error:&error]; 
    
        // Reset the persistant store when the data model changes 
        if (error) { 
    
         [[NSFileManager defaultManager] removeItemAtPath:storePath 
                    error:nil]; 
    
         NSPersistentStore __unused *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath 
                            fromSeedDatabaseAtPath:nil 
                             withConfiguration:nil 
                               options:nil 
                                error:nil]; 
        } 
    
    관련 문제