2011-11-07 3 views
2

NSPersistentStoreCoordinator 방법이 iOS 5 인 방법으로 수정되었습니다. 미리 채워진 데이터베이스를 얻으려고합니다. 작동하지 않는 것 같지만 데이터가 없습니다. 존재하는 것 같다 ... 어떤 제안?iOS 5에서 핵심 데이터 미리 채우기

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator 
{ 
    if (__persistentStoreCoordinator != nil) 
    { 
     return __persistentStoreCoordinator; 
    } 

    NSURL *storePath = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"DataModel.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 absoluteString]]) { 
     NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"DataModel" ofType:@"sqlite"]; 
     if (defaultStorePath) { 
      [fileManager copyItemAtPath:defaultStorePath toPath:[storePath absoluteString] error:NULL]; 
     } 
    } 


    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"DataModel.sqlite"]; 

    NSError *error = nil; 
    __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; 
    if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) 
    { 

     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     abort(); 
    }  

    return __persistentStoreCoordinator; 
} 
+0

이것이 작동하지 않는 이유는 말할 수 없지만 정확히 동일한 코드가 작동한다고 말할 수 있습니다. sqlite 데이터베이스가 Documents 폴더에 복사되어 있습니까? – MoFuRo

+0

@ user1028028 안녕하세요, 좋은 대답이 있습니다. 이보 르꼬 (Ivo Leko)의 대답을 왜 받아들이지 않습니까? – shannoga

답변

2

잘못된 절대 문자열을 나타내는 applicationDocumentsDirectory 메서드에 문제가 있습니다. 그것은 틀린 것은 아니지만 NSFileManager 클래스에 적합하지 않습니다.

해결책은 간단합니다. 내가 applicationDocumentsDirectory2라고 부르는 다른 방법을 써야합니다.

그래서 첫째, AppDelegate에의 파일 헤더로 이동 및 방법 선언 : 이제

- (NSString *)applicationDocumentsDirectory2; 

을의하는 .m 파일로 이동하는 방법 정의 :

- (NSString *)applicationDocumentsDirectory2 { 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil; 
    return basePath; 
} 

마지막으로 persistentStoreCoordinator 메소드를

으로 변경하십시오.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator 
{ 
    if (__persistentStoreCoordinator != nil) 
    { 
     return __persistentStoreCoordinator; 
    } 
    NSError *error = nil; 

    NSString *storePath = [[self applicationDocumentsDirectory2] stringByAppendingPathComponent: @"DataModel.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:@"DataModel" ofType:@"sqlite"]; 
     if (defaultStorePath) { 
      if(![fileManager copyItemAtPath:defaultStorePath toPath:storePath error:&error]) { 
       NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 


      } 
     } 
    } 


    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"DataModel.sqlite"]; 

    __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; 
    if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) 
    { 

     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     abort(); 
    }  

    return __persistentStoreCoordinator; 

} 

그게 전부 야! :)

관련 문제