【发布时间】:2011-12-23 21:15:26
【问题描述】:
NSPersistentStoreCoordinator 方法似乎有一些修改是 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 文件夹中?
-
@user1028028 嘿,你得到了很好的答案。你为什么不批准 Ivo Leko 的回答?
标签: objective-c core-data ios5