我有3个核心数据管理对象文件:
在这样配置它们:
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
答案 0 :(得分:0)
该错误告诉您的是,当您进行提取时,entity
为零。很难确定为什么会发生这种情况。你似乎是:
persistentStoreCoordinator_MyLocations
(使用CoreDataModel_MyLocations.momd
处的数据模型)kCORE_DATA_LOCATION_ENTITY
中的实体。这意味着kCORE_DATA_LOCATION_ENTITY
中的任何内容都与CoreDataModel_MyLocations.momd
中的实体名称不匹配。这可能就像拼写错误一样简单。要检查的第一件事是实体名称完全匹配。
如果名称匹配,则下一步是对Core Data堆栈进行更深入的验证。应用包中是否存在CoreDataModel_MyLocations.momd
? persistentStoreCoordinator_MyLocations
是否已正确初始化?验证堆栈中没有任何内容nil
或以其他方式丢失。
另外,有些人报告说这个问题是旧应用程序安装的副作用,导致一些框架混乱。确保从测试设备和模拟器中删除任何以前的应用程序副本,并进行干净的构建。