【问题标题】:implementing coredata into existing application, create or autowritten sqllite将核心数据实现到现有应用程序中,创建或未编写的 sqlite
【发布时间】:2012-12-11 23:06:23
【问题描述】:

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

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

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

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

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

【问题讨论】:

  • 只要使用魔法唱片。它很棒,可以为您处理样板代码。这是一个示例:yannickloriot.com/2012/03/…
  • 看起来很有趣,但是在使用这个之前我想学习这个核心数据的东西
  • 如果您只是弄湿了脚,那当然可以。但是,如果您在生产应用程序中使用线程,我强烈推荐 MR。它让你的生活更简单

标签: ios core-data core-data-migration


【解决方案1】:

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

为了方便起见,我创建了一个通信器,其中包含我对 coredata 所需的所有方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-04
    • 1970-01-01
    • 2014-04-16
    • 1970-01-01
    • 2010-12-21
    • 1970-01-01
    • 2014-04-11
    • 1970-01-01
    相关资源
    最近更新 更多