可以包括存储文件(sqlite的分贝的大部分时间)在您的应用程序。 然后在你的应用程序代理编辑persistentStoreCoordinator吸气merhod:
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator_ != nil) {
return persistentStoreCoordinator_;
}
NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"CoreDataStore.sqlite"];
// Check if the store exists in.
if (![[NSFileManager defaultManager] fileExistsAtPath:storePath]) {
// copy the payload to the store location.
NSString *bundleStore = [[NSBundle mainBundle] pathForResource:@"YourPayload" ofType:@"sqlite"];
NSError *error = nil;
[[NSFileManager defaultManager] copyItemAtPath:bundleStore toPath:storePath error:&error];
if (error){
NSLog(@"Error copying payload: %@", error);
}
}
NSError *error = nil;
NSURL *storeURL = [NSURL fileURLWithPath:storePath];
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_;
}
谢谢你的这段代码。你不觉得设备/核心数据/复制数据库之间可能存在兼容性问题吗? – iwalktheline 2011-04-12 13:47:41
不,我主要是使用模拟器创建数据库,然后在设备上使用它,这是没有问题的。 – rckoenes 2011-04-12 14:01:23
在你的回答的第二句中,** persantstore **是什么? (打字错误?) – David 2013-08-02 19:20:03