0

我正在将核心数据集成到我的应用程序中,我已经有一个 sqllite DB 文件。我应该创建一个新的以使其更容易,还是应该使用现有的。很抱歉有很多问题,谢谢你!

另外,如何创建一个新的?

我在 appdelegate 中实现了以下方法(没有错误),但是我不知道在文本字段中输入什么

这是什么?“myCoreData”是以 .xc​​datamodeld 结尾创建的核心数据数据库的名称吗?如果是这样,妈妈是什么?

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;
}
4

1 回答 1

1

没有使用 Magical 记录,最终在 xcode 中添加了来自新项目的样板代码。启动一个simgle view应用程序,点击coredata,去adddelegate,在底部是你最需要的方法。

为了使它更容易,我创建了一个包含 coredata 所需的所有方法的通信器。

于 2012-12-27T23:44:10.943 回答