作者:讨厌上学的-彭志超-_354 | 来源:互联网 | 2023-02-10 12:24
Ihavejuststartedusingcoredata.Iwanttosetupapre-populateddb.Ireadsomewherethatcore
I have just started using core data. I want to setup a pre-populated db. I read somewhere that core data creates a sqlite file when a core data app is run. I don't know where to look for it though.
我刚开始使用核心数据。我想设置一个预先填充的数据库。我在某处读到核心数据在运行核心数据应用程序时会创建一个sqlite文件。我不知道在哪里寻找它。
I followed the instructions over on this blog but did not find the sqlite file over the location specified directory /Users//Library/Application Support/iPhone Simulator/User/Application//Documents/
nor in the application directory.
我按照此博客上的说明进行操作,但没有在指定目录/ Users / <用户名> /库/应用程序支持/ iPhone模拟器/用户/应用程序/ <应用程序guid> / Documents / <数据库名称> 的位置找到sqlite文件。 sqlite>也不在应用程序目录中。
here is my code for persistentCoordinator.
这是我的persistentCoordinator代码。
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (__persistentStoreCoordinator != nil)
{
return __persistentStoreCoordinator;
}
NSString *storePath = [[[self applicationDocumentsDirectory] path] stringByAppendingPathComponent:@"coredata.sqlite"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:storePath]) {
NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"coredata" ofType:@"sqlite"];
if (defaultStorePath) {
[fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
}
}
NSURL *storeURL = [NSURL fileURLWithPath:storePath];
NSDictionary *optiOns= [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
NSError *error = nil;
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return __persistentStoreCoordinator;
}
4 个解决方案