【问题标题】:Coredata - Using configurations to manage transient entitiesCoredata - 使用配置来管理瞬态实体
【发布时间】:2013-02-11 15:11:25
【问题描述】:

我正在尝试使用配置来管理核心数据中的瞬态实体。

  • 首先,我创建了 2 个名为“Persistent”和“Transient”的 coredata 配置。
  • 然后,我创建了实体并将它们与正确的配置相关联 取决于实体是否可以持久化。
  • 最后,使用persistentStoreCoordinator,我创建了一个Sqlite 类型的持久存储并将其链接到“Persistent”配置。我还创建了一个内存类型的持久存储并将其链接到“瞬态”配置。

测试:我启动了iphone模拟器,我的应用程序启动成功。我关闭模拟器并 在数据库文件上启动 sqlite3。我列出了表( .tables 命令),我可以看到已经为我的临时实体创建了一些表 => 所以,它不起作用。

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    NSLog(@"++++++++ persistentStoreCoordinator");

    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
    }

    NSURL *persistStoreURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"ProjectToDelete.sqlite"];

    NSMutableDictionary *sqliteOptions = [NSMutableDictionary dictionary];
    [sqliteOptions setObject:@"WAL" forKey:@"journal_mode"];

    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                             [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
                             sqliteOptions, NSSQLitePragmasOption,
                             nil];

    NSError *error = nil;
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:@"Persistent" URL:persistStoreURL options:options error:&error]) {

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSInMemoryStoreType configuration:@"Transient" URL:[NSURL URLWithString:@"memory://store"] options:nil error:&error]) {

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return _persistentStoreCoordinator;
}

【问题讨论】:

    标签: core-data memory sqlite entities transient


    【解决方案1】:

    我认为您正在混合不同的 CoreData 概念。

    CoreData 不是 ORM,而是管理对象及其关系的对象图。

    你可以在一个实体上拥有瞬态的属性,它不会被做成代表那个实体的表中的列,只在运行时生成,而对象在内存中。

    抽象实体永远不会被实例化,如果您有很多从一个超级对象继承的对象,通常会使用它们,即员工管理系统可能有一个抽象的个人实体,以及都继承自 Person 的 Employee、Manager、Director 和 Manager

    【讨论】:

    • 这个网页讨论了处理瞬态实体的不同方法:cimgf.com/2011/08/08/transient-entities-and-core-data,但我正在尝试使用 coredata 中的配置来实现。
    • 你不能单独使用配置来做到这一点。他谈到的瞬态实体的概念是在代码中管理的,他在 NSManagedObjectContext 中创建 NSManagedObjects,但从不保存它们。还有其他几种方法可以实现这一点,但它们都是在代码中完成的,在配置文件中没有
    • 我对其他方式很感兴趣,如果你能多谈谈的话。对你来说,最好的方法是什么?
    【解决方案2】:

    正如评论中提到的,尝试通过模型编辑器配置数据存储是行不通的。这些配置可以让您添加预定的提取,但它们不允许您配置数据的存储位置。这是使用任何这些方法的关键。如博文中所述,您需要添加一些代码才能正确使用这些技术。

    【讨论】:

      猜你喜欢
      • 2011-05-06
      • 2012-01-25
      • 2013-04-23
      • 2011-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-14
      相关资源
      最近更新 更多