我的应用程序已设置好,以便从一个实体(地点列表(学校))中提取一系列内容,并将其放入我视图中的选择器中。
在视图中,用户选择位置并自动生成里程(在我们获取位置的同一实体内预设)。
当他们点击“保存条目”时,我希望它保存到coredata中的另一个实体,我相信我已将其设置为正确执行此操作,但它会启动此错误:
2014-02-19 10:20:34.545 54 Miles[19263:70b] Selection 1: Aldrin
2014-02-19 10:20:34.546 54 Miles[19263:70b] Selection 2: Addams
2014-02-19 10:20:34.548 54 Miles[19263:70b] Miles: 1.8
2014-02-19 10:20:34.550 54 Miles[19263:70b] DrivenDate: 2014-02-19 16:15:31 +0000
2014-02-19 10:20:34.550 54 Miles[19263:70b] Date: 2014-02-19 16:20:34 +0000
2014-02-19 10:20:34.561 54 Miles[19263:70b] CoreData: error: (NSFetchedResultsController) object <MetaMiles: 0x8a8abb0> (entity: MetaMiles; id: 0x8a34d30 <x-coredata:///MetaMiles/t8DB6BCC9-78DF-4699-A86A-538E7ABD85292> ; data: {
"beg_school" = nil;
"end_school" = nil;
miles = 0;
}) returned nil value for section name key path 'beg_school'. Object will be placed in unnamed section
2014-02-19 10:20:34.563 54 Miles[19263:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSSetM addObject:]: object cannot be nil'
我将它设置为回应应该保存到实体中的每个事物(仅用于测试,以确保它抓取适当的数据)并根据日志 - 我可以看到它正在获取适当的数据。但它的第二部分似乎也试图保存MetaMiles记录。我不希望它这样做。 MetaMiles实体只是我从中获取数据的实体 - 它永远不需要对其进行任何保存。如何仅指定保存UserMiles实体而不保存MetaMiles实体(我从最初获取信息)?
我的按钮代码如下所示:
//Button to track miles
- (IBAction)trackMilesButton:(id)sender {
//When button is clicked - save the trip to the User_Miles database!
UserMiles *newMilesEntry = [UserMiles MR_createEntity];
//Configure entry data
[newMilesEntry setEnd_school:[_schoolArray1 objectAtIndex:[_tripPicker selectedRowInComponent:0]]];
NSLog(@"Selection 1: %@",[_schoolArray1 objectAtIndex:[_tripPicker selectedRowInComponent:0]]);
[newMilesEntry setBeg_school:[_schoolArray1 objectAtIndex:[_tripPicker selectedRowInComponent:1]]];
NSLog(@"Selection 2: %@",[_schoolArray1 objectAtIndex:[_tripPicker selectedRowInComponent:1]]);
NSArray *currentMiles = [self getMileage];
MetaMiles *tripMiles = [currentMiles objectAtIndex:0];
[newMilesEntry setMiles:tripMiles.miles];
NSLog(@"Miles: %@",tripMiles.miles);
[newMilesEntry setDriven_date:self.datePicker.date];
NSLog(@"DrivenDate: %@", self.datePicker.date);
[newMilesEntry setEntry_date:[NSDate date]];
NSLog(@"Date: %@", [NSDate date]);
//Save the data & reload View
[NSManagedObjectContext MR_rootSavingContext];
[self viewDidLoad];
}
我对CoreData并不熟悉,因为我还在学习所有这些。一方面我觉得我已经摆脱了它,但是这样的愚蠢的事情让我觉得我向后退三步。
所有帮助表示赞赏。
答案 0 :(得分:0)
我正在我的方法中创建一个实体来获取里程 - 一旦我摆脱了错误就消失了。
数据仍未妥善保存,但完全是另一个问题。
我注释掉了创建新“空白”实体的部分,只是将数组设置为nil并处理了如果在我的代码中其他地方为零的话。
以下是那些好奇的方法:
//****GET MILEAGE METHOD****//
- (NSArray *)getMileage {
//Update the Mileage indicator to display miles between currently selected values
NSString *begSchool = [_schoolArray1 objectAtIndex:[_tripPicker selectedRowInComponent:0]];
NSString *endSchool = [_schoolArray1 objectAtIndex:[_tripPicker selectedRowInComponent:1]];
NSPredicate *milesFilter = [NSPredicate predicateWithFormat:@"beg_school=%@ AND end_school=%@", begSchool, endSchool];
NSArray *resultMiles = [MetaMiles MR_findAllWithPredicate:milesFilter];
if (!resultMiles || ![resultMiles count]) {
//The first load - this displays appropriately in log letting us know there is currently nothing there
NSLog(@"Empty Array");
//MetaMiles *emptyInstance = [MetaMiles MR_createEntity];
//resultMiles = [[NSArray alloc]initWithObjects:emptyInstance, nil];
resultMiles = nil;
} else {
MetaMiles *tripMiles = [resultMiles objectAtIndex:0];
//This confirms our result miles come out appropriately - they look like this: ("2.1")
NSLog(@"Our result miles are: %@", tripMiles.miles);
}
return resultMiles;
}
//****END GET MILEAGE****//