请告诉我:如果我在我的iPhone应用程序中使用Core Data,我基本上有两个文件。 mydatamodel.xcdatamodel文件,然后我需要一个.sqlite文件。 Apple提供了此代码段:
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator != nil) {
return persistentStoreCoordinator;
}
NSString *appDirPath = [self applicationDocumentsDirectory];
NSString *storeFileName = @"mystore.sqlite";
NSURL *storeUrl = [NSURL fileURLWithPath:[appDirPath stringByAppendingPathComponent:storeFileName]];
NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) {
NSLog(@"Error: %@, %@", error, [error userInfo]);
abort();
}
return persistentStoreCoordinator;
}
如果文件不可用,是否会创建该文件?
[persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]
我的应用不需要初始数据,因为它会在用户启动应用时下载。
答案 0 :(得分:7)
是的,Core Data将在您的应用代表首次启动应用程序之后创建SQLite数据库。
答案 1 :(得分:2)
是。 Apple模板提供的锅炉板核心数据堆栈代码将创建数据库文件(如果它尚不存在)。
答案 2 :(得分:1)
是的,该方法在给定位置添加指定类型的新持久存储,并返回新存储。 Here is the documentation