【问题标题】:Core data relationship with prepopulated sqlite与预填充 sqlite 的核心数据关系
【发布时间】:2015-03-20 01:34:33
【问题描述】:

首先,对不起。我英语说的不好。你好。我是iOS初学者。

目前我是这样开发的:

  1. 删除自动生成的核心数据 sqlite 文件
  2. 使用 firefox sqlite manager 将我创建的 sqlite 文件复制到已删除的 sqlite 文件路径
    (使用 Z_PK、Z_ENT、Z_OPT 的 sqlite)

两个问题:

  1. 如何使用 firefox sqlite manager 创建实体关系。
  2. 如何预先创建详细的核心数据选项,如关系删除规则、sqlite 管理器上的属性或其他任何内容。

【问题讨论】:

    标签: ios sqlite core-data


    【解决方案1】:

    嗯,基本上您需要获取一个由您的项目使用您的架构创建的空数据库,然后对其进行操作以保存您想要的数据。更多信息here

    链接中未描述的替代方法是使用 Core Data 本身(以及用于读取其他数据库的 SQLite 库)将数据从您的数据库复制到 Core Data 数据库中。

    【讨论】:

    • 即sqlite索引查询=核心数据实体关系表达式?
    • 是的,一对一的关系只是指向另一个表中对应对象的ID。我不确定一对多。
    【解决方案2】:
    - (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
    }
    
    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CoreDataBooks.CDBStore"];
    
    /*
     Set up the store.
     For the sake of illustration, provide a pre-populated default store.
     */
    NSFileManager *fileManager = [NSFileManager defaultManager];
    // If the expected store doesn’t exist, copy the default store.
    if (![fileManager fileExistsAtPath:[storeURL path]]) {
        NSURL *defaultStoreURL = [[NSBundle mainBundle] URLForResource:@"CoreDataBooks" withExtension:@"CDBStore"];
        if (defaultStoreURL) {
            [fileManager copyItemAtURL:defaultStoreURL toURL:storeURL error:NULL];
        }
    }
    
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
    
    NSError *error;
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&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. 
    
         Typical reasons for an error here include:
         * The persistent store is not accessible;
         * The schema for the persistent store is incompatible with current managed object model.
         Check the error message to determine what the actual problem was.
    
    
         If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.
    
         If you encounter schema incompatibility errors during development, you can reduce their frequency by:
         * Simply deleting the existing store:
         [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]
    
         * Performing automatic lightweight migration by passing the following dictionary as the options parameter:
         @{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES}
    
         Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.
    
         */
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
    
    return _persistentStoreCoordinator;
    

    代码取自 Apple docs Core Data Books Link 您需要有一个预先填充的数据数据库,该数据库已通过 Core Data 填充。此函数在应用首次启动时将预填充的数据库复制到主数据库存储位置。


    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-10
      • 2011-04-01
      • 1970-01-01
      • 2011-09-29
      • 2015-07-04
      • 1970-01-01
      • 2011-12-17
      相关资源
      最近更新 更多