作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用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];
}
最佳答案
如果您查看苹果的文档:
mergedModelFromBundles:
+ (NSManagedObjectModel *)mergedModelFromBundles:(NSArray *)bundles
***bundles***
***Return Value***
关于ios - NSManagedObjectModel mergedModelFromBundles错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9247031/
我正在尝试使用 CoreData 中的迁移功能。我遵循了苹果文档。我在以下方法中遇到问题: /** Returns the managed object model for the applicat
我有一个包含 15 个版本的核心数据模型。它有代码可以在发布时逐步从当前商店的版本迁移到最新版本。 关键是调用 NSDictionary* options = @{ NSMigratePers
我正在尝试在 SenTestCase 中使用 CoreData。问题是找不到 NSManagedObjectModel。我试图通过在应用程序包中搜索 mom 文件来使用 URL 对其进行初始化,但我找
我是一名优秀的程序员,十分优秀!