多个.xcdatamodeld核心数据文件,如何保存到正确的一个?

时间:2014-02-25 14:13:24

标签: ios objective-c core-data

我有3个核心数据管理对象文件:

  1. CoreDataModel.xcdatamodeld
  2. CoreDataModel_Small.xcdatamodeld
  3. CoreDataModel_MyLocations.xcdatamodeld
  4. 在这样配置它们:

    AppDelegate.h:

    @property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
    @property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
    @property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
    
    /////// CoreDataModel_Small ///////
    @property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext_small;
    @property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel_small;
    @property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator_small;
    
    /////// CoreDataModel_MyLocations ///////
    @property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext_MyLocations;
    @property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel_MyLocations;
    @property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator_MyLocations;
    

    AppDelegate.m:

    - (NSManagedObjectContext *)managedObjectContext
    {
        if (_managedObjectContext != nil) {
            return _managedObjectContext;
        }
    
        NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
        if (coordinator != nil) {
            _managedObjectContext = [[NSManagedObjectContext alloc] init];
            [_managedObjectContext setPersistentStoreCoordinator:coordinator];
        }
        return _managedObjectContext;
    }
    
    - (NSManagedObjectModel *)managedObjectModel
    {
        if (_managedObjectModel != nil) {
            return _managedObjectModel;
        }
        NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"CoreDataModel" withExtension:@"momd"];
        _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
        return _managedObjectModel;
    }
    
    - (NSPersistentStoreCoordinator *)persistentStoreCoordinator
    {
        if (_persistentStoreCoordinator != nil) {
            return _persistentStoreCoordinator;
        }
    
        NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CoreDataModel.sqlite"];
        NSError *error = nil;
        _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    
        NSDictionary *options = @{NSMigratePersistentStoresAutomaticallyOption : @YES,
                                  NSInferMappingModelAutomaticallyOption : @YES,
                                  NSSQLiteManualVacuumOption : @YES};
    
        if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }
    
        return _persistentStoreCoordinator;
    }
    
    //////////// CoreDataModel_Small ////////////
    
    - (NSManagedObjectContext *)managedObjectContext_small
    {
        if (_managedObjectContext_small != nil) {
            return _managedObjectContext_small;
        }
    
        NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator_small];
        if (coordinator != nil) {
            _managedObjectContext_small = [[NSManagedObjectContext alloc] init];
            [_managedObjectContext_small setPersistentStoreCoordinator:coordinator];
        }
        return _managedObjectContext_small;
    }
    
    - (NSManagedObjectModel *)managedObjectModel_small
    {
        if (_managedObjectModel_small != nil) {
            return _managedObjectModel_small;
        }
        NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"CoreDataModel_Small" withExtension:@"momd"];
        _managedObjectModel_small = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
        return _managedObjectModel_small;
    }
    
    - (NSPersistentStoreCoordinator *)persistentStoreCoordinator_small
    {
        if (_persistentStoreCoordinator_small != nil) {
            return _persistentStoreCoordinator_small;
        }
    
        NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CoreDataSmall.sqlite"];
        NSError *error = nil;
        _persistentStoreCoordinator_small = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel_small]];
    
        NSDictionary *options = @{NSMigratePersistentStoresAutomaticallyOption : @YES,
                                  NSInferMappingModelAutomaticallyOption : @YES,
                                  NSSQLiteManualVacuumOption : @YES};
    
        if (![_persistentStoreCoordinator_small addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            // abort();
        }
    
        return _persistentStoreCoordinator;
    }
    
    //////////// CoreDataModel_MyLocations ////////////
    
    - (NSManagedObjectContext *)managedObjectContext_MyLocations
    {
        if (_managedObjectContext_MyLocations != nil) {
            return _managedObjectContext_MyLocations;
        }
    
        NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator_MyLocations];
        if (coordinator != nil) {
            _managedObjectContext_MyLocations = [[NSManagedObjectContext alloc] init];
            [_managedObjectContext_MyLocations setPersistentStoreCoordinator:coordinator];
        }
        return _managedObjectContext_MyLocations;
    }
    
    - (NSManagedObjectModel *)managedObjectModel_MyLocations
    {
        if (_managedObjectModel_MyLocations != nil) {
            return _managedObjectModel_MyLocations;
        }
        NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"CoreDataModel_MyLocations" withExtension:@"momd"];
        _managedObjectModel_MyLocations = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
        return _managedObjectModel_MyLocations;
    }
    
    - (NSPersistentStoreCoordinator *)persistentStoreCoordinator_MyLocations
    {
        if (_persistentStoreCoordinator_MyLocations != nil) {
            return _persistentStoreCoordinator_MyLocations;
        }
    
        NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CoreDataMyLocations.sqlite"];
        NSError *error = nil;
        _persistentStoreCoordinator_MyLocations = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel_MyLocations]];
    
        NSDictionary *options = @{NSMigratePersistentStoresAutomaticallyOption : @YES,
                                  NSInferMappingModelAutomaticallyOption : @YES,
                                  NSSQLiteManualVacuumOption : @YES};
    
        if (![_persistentStoreCoordinator_MyLocations addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            // abort();
        }
    
        return _persistentStoreCoordinator;
    }
    

    并调用正确的上下文:

    FOUNDATION_EXPORT NSManagedObjectContext *generateManagedObjectContext(CoreDataType type)
    {
        NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
    
        if (type == CoreDataTypeBig) {
            context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator;
        }
        else if (type == CoreDataTypeSmall) {
            context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator_small;
        }
        else if (type == CoreDataTypeMyLocations) {
            context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator_MyLocations;
        }
    
        if ([NSThread isMainThread])    { [context setMergePolicy:NSMergeByPropertyObjectTrumpMergePolicy]; }
        else                            { [context setMergePolicy:NSMergeByPropertyStoreTrumpMergePolicy]; }
    
        return context;
    }
    

    但是我的一个方法中有一个错误,当从特定实体中删除所有对象时,我之前没有遇到过这个问题,因为我在一个.xcdatamodeld文件中管理了所有对象,我决定将其分开他们:

      

    获取请求必须具有实体

    我认为其中一个问题是当我保存上下文(在AppDelegate.m文件中)时,该函数保持平常,我确信我需要以某种方式使其保存到正确的{{ 1}}。

    编辑1:

    产生错误的代码:

    NSManagedObjectContext

1 个答案:

答案 0 :(得分:0)

该错误告诉您的是,当您进行提取时,entity为零。很难确定为什么会发生这种情况。你似乎是:

  • 查找persistentStoreCoordinator_MyLocations(使用CoreDataModel_MyLocations.momd处的数据模型)
  • 在使用该持久性商店协调员的托管对象上下文中查找名称存储在kCORE_DATA_LOCATION_ENTITY中的实体。
  • 获取该实体

这意味着kCORE_DATA_LOCATION_ENTITY中的任何内容都与CoreDataModel_MyLocations.momd中的实体名称不匹配。这可能就像拼写错误一样简单。要检查的第一件事是实体名称完全匹配。

如果名称匹配,则下一步是对Core Data堆栈进行更深入的验证。应用包中是否存在CoreDataModel_MyLocations.momdpersistentStoreCoordinator_MyLocations是否已正确初始化?验证堆栈中没有任何内容nil或以其他方式丢失。

另外,有些人报告说这个问题是旧应用程序安装的副作用,导致一些框架混乱。确保从测试设备和模拟器中删除任何以前的应用程序副本,并进行干净的构建。