質問

My app is setup so that it fetches a list of things from one entity (list of locations(schools)) and puts them into pickers inside my view.

In the view the user picks the locations and the mileage is automatically generated (preset inside the same entity that we get the locations from).

When they click "save entry", I want it to save to a different entity in coredata and I believe I have set it up to properly do that, but it kicks off this error:

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'

I have it set to echo each thing that should be getting saved into the entity (just for testing, to ensure it's grabbing the appropriate data) and as per the log - I can see that it's getting the appropriate data. But the the second part of it looks like it's trying to save a MetaMiles record as well. I do not want it to do this. The MetaMiles entity is only an entity where I am getting data from - it will never need any saving done to it. How do I specify only to save the UserMiles entity and NOT save the MetaMiles entity (that I fetch information from originally)?

My button code looks like this:

//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];
}

Not super familiar with CoreData as I'm still learning all of this. On one hand I feel like I'm getting the hang out of it, but then silly things like this make me think I'm taking 3 steps backwards.

All help appreciated.

役に立ちましたか?

解決

I was creating an entity in my method to get the miles - once I got rid of that the error went away.

The data is still not saving appropriately, but that's another question entirely.

I commented out the part to create the new 'blank' entity and just set the array to nil and handled what to do if that was nil elsewhere in my code.

Here is the method for those curious:

//****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****//
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top