此代码应该将我的数据保存到TestViewController.m文件中的核心数据:
//Property
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
//Methods
- (void)saveUser:(NSString *) username withEmail:(NSString *) email withName:(NSString *) name
{
//1
AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
//2
self.managedObjectContext = appDelegate.managedObjectContext;
User *coreDataUser = [NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:self.managedObjectContext];
coreDataUser.username = username;
coreDataUser.email = email;
coreDataUser.name = name;
NSError *error;
if (![self.managedObjectContext save:&error])
{
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
}
//In my save user method I have stuff that is done in another thread but then this is called:
// Add in any UIKit code here on main queue
dispatch_async(dispatch_get_main_queue(), ^{
if (isUnique == YES)
{
[self saveUser:[emailStr lowercaseString] withEmail:emailStr withName:nameStr];
我的代码总是崩溃:
ser *coreDataUser = [NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:self.managedObjectContext];
主要是信号SIGABRT。
我正在关注本教程:http://www.codigator.com/tutorials/ios-core-data-tutorial-with-example/
在ios 7中运行。
我错过了什么?
我现在正在跟踪appDelegate.m代码,但它看起来像这样:
// 1
- (NSManagedObjectContext *) managedObjectContext {
if (_managedObjectContext != nil) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
_ managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator: coordinator];
}
return _managedObjectContext;
}
//2
- (NSManagedObjectModel *)managedObjectModel {
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
_managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
return _managedObjectModel;
}
//3
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil)
{
return _persistentStoreCoordinator;
}
NSString *test = [self applicationDocumentsDirectory];
NSString *test2 = test;
NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"HappyPeople.sqlite"]];
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
i f (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error])
{
/*Error for store creation should be handled in here*/
}
return _persistentStoreCoordinator;
}
- (NSString *)applicationDocumentsDirectory
{
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}
似乎创建了sqllite db文件。
我得到的例外是:
NSException * name:@"NSInternalInconsistencyException" reason:@"+entityForName: could not locate an entity named 'Users' in this model." 0x0895ee20
答案 0 :(得分:0)
我知道这个问题有点老了......
这是我更改对象模型的架构并尝试使用旧架构的文件时获得的异常。磁盘上的sqllite数据库可能与您的应用程序架构不匹配。
我一直在使用带有UIManagedDocument的CoreData,所以它有点不同。您可以选择在打开时自动迁移。
此外,在评论的早期,人们询问ManagedObjectContext是否为零。我相信在调用插入之前应该检查这个。我们确定所有核心数据设置都在主队列中吗?打开可能需要一些时间。 (再次使用UIManagedDocument明确地在另一个线程上。)