我正在将核心数据集成到我的应用程序中,我已经有了一个sqllite数据库文件。我应该创建一个新的,以使其更容易,或者我应该使用现有的。 对于很多问题我很抱歉,谢谢你提前!!
另外,我该如何创建一个新的?
我将以下方法实现到appdelegate(没有错误),但是我不知道在文本字段中放什么
这是什么? “myCoreData”是用.xcdatamodeld结尾创建的核心数据db的名称吗?如果是这样,妈妈是什么?NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"myCoreData" withExtension:@"momd"];
这是什么?
这是创建数据库,还是我应该创建它并将信息放在这里?这存储在哪里?
NSURL *storeURL = [[self applicationDocumentsDirectoryModified] URLByAppendingPathComponent:@"coreDataDB.sqlite"];
这是实施
- (void)saveContext
{
NSError *error = nil;
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil) {
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
}
#pragma Core Data stack
// Returns the managed object context for the application.
// If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
- (NSManagedObjectContext *)managedObjectContext
{
if (_managedObjectContext != nil) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return _managedObjectContext;
}
// Returns the managed object model for the application.
// If the model doesn't already exist, it is created from the application's model.
- (NSManagedObjectModel *)managedObjectModel
{
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"myCoreData" withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}
// Returns the persistent store coordinator for the application.
// If the coordinator doesn't already exist, it is created and the application's store added to it.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectoryModified] URLByAppendingPathComponent:@"coreDataDB.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;
}
答案 0 :(得分:1)
没有使用魔法记录,结束了从xcode中的新项目添加样板代码。启动一个simgle视图应用程序,单击coredata,转到adddelegate,底部是你需要的大多数方法。
为了方便起见,我用coredata所需的所有方法创建了一个传播者。