2012-02-26 3 views
-1

다른 사람이이 문제를 발견했는지 궁금합니다.coredata가 xcode 4.3에서 작동하지 않습니까?

나는 이전 xcode 버전에서 훌륭하게 작동했던이 코드 조각을 가지고 있습니다.

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { 

    if (persistentStoreCoordinator != nil) { 
     return persistentStoreCoordinator; 
    } 

    NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"mydb.sqlite"]; 
    /* 
    Set up the store. 
    For the sake of illustration, provide a pre-populated default store. 
    */ 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    // If the expected store doesn't exist, copy the default store. 
    if (![fileManager fileExistsAtPath:storePath]) { 
     NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"mydb" ofType:@"sqlite"]; 
     if (defaultStorePath) { 
      [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL]; 
     } 
    } 

    NSURL *storeUrl = [NSURL fileURLWithPath:storePath]; 

    [self addSkipBackupAttributeToItemAtURL:storeUrl]; 

    NSError *error; 
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]]; 
    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) { 
     /* 
     Replace this implementation with code to handle the error appropriately. 

     abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button. 

     Typical reasons for an error here include: 
     * The persistent store is not accessible 
     * The schema for the persistent store is incompatible with current managed object model 
     Check the error message to determine what the actual problem was. 
     */ 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     abort(); 
    }  

    return persistentStoreCoordinator; 
} 

는 나는이에서 다음과 같은 기대 :

  1. 빈 "MYDB는"처음부터 만들 수 내 번들에 더 "mydb.sqlite"가없는 경우.
  2. 내 기본 번들에 "mydb.sqlite"가 있으면 지정된 디렉토리에 복사 될 것으로 기대됩니다.
  3. "mydb.sqlite"가 내 xcdatamodel과 호환되지 않으면 앱이 중단되어야합니다.

그러나 이전에 이미 생성 된 db에서만 작동합니다. 예를 들어 "mydb.sqlite"라는 임의의 db를 내 번들에 넣고 원래의 파일을 삭제하려고하면

응용 프로그램이 다운되지 않습니다! 빈 db가 작성되고 새 db가 무시됩니다. 이것은 내 코드를 위반하는 것처럼 완전히 잘못되었습니다.

또한 원래 데이터베이스를 다시 추가하면 아무 일도 발생하지 않으며 응용 프로그램은 빈 db를 만듭니다.

(그래 나는 시뮬레이션 응용 프로그램을 삭제, 내 프로젝트를 청소하고, 변경이 발생하기 전에, 심지어 빌드 폴더를 삭제합니까!)

어떤 아이디어가?

+0

무엇이 오류입니까? 그것을 게시 할 수 있습니까? – Raptor

+0

오류가 없습니다. 그것은 단지 내 코드에 따라야하는 방식으로 작동하지 않는 관찰이다 –

답변

0

질문/의견에 대한 답변은 간단합니다. 나는 4.2.1에 나의 엑스 코드를 복원 한

다운 그레이드

4.2.1에 전부입니다 예상대로 이제 (타임 머신 하나님 께 감사를드립니다)합니다.

나는 오늘 애플에게 버그 리포트를 제출할 것이다.

0

명시된 것처럼 코드와 기대치간에 차이가 있습니다.

여기 당신의 기대입니다 :

  1. 내 번들에는 "mydb.sqlite"가없는 경우 처음부터 생성 할 빈 "MYDB".
  2. "mydb.sqlite"가 내 기본 번들에 존재하면 지정한 디렉토리에 복사 될 것으로 예상됩니다.
  3. "mydb.sqlite"가 내 xcdatamodel과 호환되지 않으면 앱이 중단되어야합니다. 여기

는 코드가 무엇을 : 그것은에있는 경우 문서 디렉토리

  • mydb.sqlite이 존재하지 않는 경우에 mydb.sqlite에 대한

    1. 봐가, (주 번들에서 복사 주요 번들)
    2. 는 ~/문서/mydb.sqlite에서

    난 당신이 겪고있는 생각의 주요 문제는 3 단계 01의 인 새로운 영구 저장소 만들기은 제공된 URL에 새 상점을 만듭니다. 대신 (현재 MainBundle에서 복사 된 파일이 있는지 확인하기 위해) 파일의 존재를 다시 쿼리해야하며 존재하는 경우 새 영구 저장소를 만드는 대신 [persistentStoreCoordinator persistentStoreForURL:storeURL]을 사용하십시오.

    일단이 문제를 해결하면 다른 문제를보다 쉽게 ​​추적 할 수 있습니다.코드에 더 많은 NSLog를 추가하면 코드가 예상 실행 경로를 따르는 지 확인할 수 있습니다. 예 : 생성 한 각각의 새로운 개체를 기록하십시오. 따라서 어떤 개체가 nil인지 쉽게 확인할 수 있습니다. 번들 db를 복사 할 때 NULL 대신 NSError 포인터를 추가 한 다음 오류가 아닌 경우 오류를 기록하십시오.

  • +0

    예. 죄송합니다. 당신이 말하는 바를 진술 해주세요. 나는 내 질문에 명확하게하지 않았다. 그러나 이것은 작동하지 않습니다. –

    0

    기본 db가 실제로 번들에 포함되어 있는지 확인하십시오. 즉, 프로젝트에 포함 된 것으로 확인되고 파일 복사 빌드 단계에 있는지 확인하십시오. 프로젝트 구조에 복사 한 프로젝트와 SQLite 파일을 다시 정리하여 프로젝트에 포함시키지 않았습니다. 이는 사용자가보고있는 동작을 유발합니다.

    관련 문제