NSManagedObjectModel 合并模型FromBundles 错误
Posted
技术标签:
【中文标题】NSManagedObjectModel 合并模型FromBundles 错误【英文标题】:NSManagedObjectModel mergedModelFromBundles error 【发布时间】:2012-02-12 06:53:09 【问题描述】:我正在使用 Core Data,并且在我的应用程序一开始就将数据导入数据库时遇到了很多麻烦。 下面是我从我遵循的教程中获取的一些代码。 下面概述了我获得 SIGABRT 的要点。 任何建议或帮助表示赞赏 谢谢
//THIS FUNCTION IS CALLED AFTER MY APP DID FINISHING LOADING IN THE
// IN THE APP DELEGATE
-(void)loadData
NSManagedObjectContext *context = [self managedObjectContext];
NewModel *newModel = (NewModel *)[NSEntityDescription insertNewObjectForEntityForName:@"NewModel" inManagedObjectContext:context];
//ADD MORE DATA TO ENTITY
/**
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];
else
NSLog(@"Error");
return managedObjectContext;
/**
Returns the managed object model for the application.
If the model doesn't already exist, it is created by merging all of the models found in the application bundle.
*/
- (NSManagedObjectModel *)managedObjectModel
if (managedObjectModel != nil)
return managedObjectModel;
/**********************************************************/
// SIGABRT HAPPENS IN THE NEXT LINE OF CODE
/**********************************************************/
managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
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;
NSString *storePath = [ [self applicationDocumentsDirectory] stringByAppendingPathComponent:@"NewModel.db"];
NSURL *storeUrl = [NSURL fileURLWithPath:storePath];
// Put down default db if it doesn't already exist
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:storePath])
NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"LeagueModel" ofType:@"sqlite"];
if (defaultStorePath)
[fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
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;
/**
Returns the path to the application's Documents directory.
*/
- (NSString *)applicationDocumentsDirectory
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
【问题讨论】:
【参考方案1】:如果你查看苹果的文档:
mergedModelFromBundles:
返回通过合并在给定包中找到的所有模型创建的模型。
+ (NSManagedObjectModel *)mergedModelFromBundles:(NSArray *)bundles
参数
***bundles***
要搜索的 NSBundle 实例数组。如果指定 nil,则搜索主包。
***Return Value***
通过合并捆绑包中的所有模型创建的模型。
你在需要传递数组的地方传递 nil,这是崩溃的原因。
【讨论】:
> 如果指定 nil,则搜索主包。以上是关于NSManagedObjectModel 合并模型FromBundles 错误的主要内容,如果未能解决你的问题,请参考以下文章
尝试使用前向类“NSManagedObjectModel”作为 Swift 类模型的超类
NSManagedObjectModel 用 mergeModelFromBundles 初始化:nil 在 SenTestCase 中找不到模型
任何模型中都没有 NSEntityDescriptions 声明 NSManagedObject 子类 你加载你的 NSManagedObjectModel 了吗?
RestKit 2.0 删除 RKManagedObjectStore 但保留 NSManagedObjectModel