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;
}
이것이 작동하지 않는 이유는 말할 수 없지만 정확히 동일한 코드가 작동한다고 말할 수 있습니다. sqlite 데이터베이스가 Documents 폴더에 복사되어 있습니까? – MoFuRo
@ user1028028 안녕하세요, 좋은 대답이 있습니다. 이보 르꼬 (Ivo Leko)의 대답을 왜 받아들이지 않습니까? – shannoga