我收到此错误:
核心数据数据存储丢失获取错误:'NSInvalidArgumentException',原因:在实体 <NSSQLEntity FlowData id=1> 中找不到 keypath flowData.flowAmount
尝试将核心数据存储中的数据加载到表格视图中时。
在更改数据模型并将其还原后,从模拟器中删除应用程序后,下次运行应用程序时,将重新创建 sqlite 数据库。
我能够插入数据库。这个转储显示了我刚刚输入的记录:
INSERT INTO "ZFLOWDATA"
VALUES(1,1,1,NULL,8.0,376884478.384176,376884471.83175,8.0);
但是,当我尝试从数据存储中读取时,出现上述错误。
这是我尝试加载数据时的相关代码:
- (void)viewDidLoad
{
[super viewDidLoad];
NSError *error;
if (![[self fetchedResultsController] performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1); // Fail
}
self.title = @"Flow Data Items";
}
和
- (NSFetchedResultsController *)fetchedResultsController {
jhsAppDelegate *appDelegate = (jhsAppDelegate *)[[UIApplication sharedApplication]delegate];
managedObjectContext = [appDelegate managedObjectContext];
if (fetchedResultsController != nil) {
return fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"FlowData" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"FlowData.flowAmount"
ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
[fetchRequest setFetchBatchSize:20];
NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext sectionNameKeyPath:nil
cacheName:@"Root"];
self.fetchedResultsController = theFetchedResultsController;
self.fetchedResultsController.delegate = self;
return fetchedResultsController;
}
这是我的数据模型:
最后,这是我的实体数据模型类代码:
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@interface FlowData : NSManagedObject
@property (nonatomic, retain) NSNumber * flowAmount;
@property (nonatomic, retain) NSDate * flowEnteredDateTime;
@property (nonatomic, retain) NSDate * flowOccurrenceDateTime;
@property (nonatomic, retain) NSNumber * flowScheduledFrequencyMinutes;
@property (nonatomic, retain) NSNumber * flowUrgency;
@end
这一定与我如何尝试读取数据有关,因为我能够在另一个视图控制器上插入一行,因为我相信FlowData.flowAmount
实体上确实存在该行,因为它被插入到实体中。
有任何想法吗?.h 文件中列出属性的顺序是否必须与实际数据模型中的顺序相匹配?我的数据类型匹配吗?