使用 MagicalRecord 保存实体 - 还尝试保存不同的实体?

Posted

技术标签:

【中文标题】使用 MagicalRecord 保存实体 - 还尝试保存不同的实体?【英文标题】:Save an entity with MagicalRecord - trying to save a different entity also? 【发布时间】:2014-02-19 16:37:37 【问题描述】:

我的应用已设置为从一个实体(位置(学校)列表)获取事物列表,并将它们放入我视图中的选择器中。

在视图中,用户选择位置并自动生成里程(预设在我们从中获取位置的同一实体中)。

当他们点击“保存条目”时,我希望它保存到 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 不是很熟悉,因为我还在学习所有这些。一方面,我觉得我已经摆脱了困境,但像这样的愚蠢事情让我觉得我倒退了 3 步。

感谢所有帮助。

【问题讨论】:

【参考方案1】:

我在我的方法中创建了一个实体来获取里程 - 一旦我摆脱了这个错误就消失了。

数据仍然没有正确保存,但这完全是另一个问题。

我注释掉了创建新“空白”实体的部分,只是将数组设置为 nil 并处理如果我的代码中其他地方为 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****//

【讨论】:

以上是关于使用 MagicalRecord 保存实体 - 还尝试保存不同的实体?的主要内容,如果未能解决你的问题,请参考以下文章

MagicalRecord 不保存对多关系实体

CoreData(MagicalRecord) 获取保存的对象

MagicalRecord:删除后保存上下文太长

MagicalRecord:现在创建,(可能)稍后保存

如何在 MagicalRecord saveWith Block 之外创建核心数据实体

MagicalRecord 调用多个保存方法