2014-02-19 1 views
0

내 앱이 하나의 엔티티 (위치 목록 (학교) 목록)의 목록을 가져 와서 내보기의 선택기에 넣도록 설정되었습니다.MagicalRecord를 사용하여 엔티티 저장 - 다른 엔티티도 저장하려고합니까?

보기에서 사용자가 위치를 선택하면 마일리지가 자동으로 생성됩니다 (위치를 얻은 동일한 엔티티 내에서 미리 설정 됨).

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' 

: 그들은, 나는 그것이 coredata의 다른 개체에 저장할와 내가 제대로 그렇게하도록 설정 한 생각하지만,이 오류가 개막 클릭 '항목을 저장 "

엔티티에 저장해야하는 각 항목을 에코하도록 설정 했으므로 (적절한 데이터를 확보 할 수 있는지 테스트하기 위해) 로그별로 볼 수 있습니다. 적절한 데이터를 얻고 있음을 알 수 있습니다. 그러나 두 번째 부분은 MetaMiles 레코드를 저장하려고하는 것 같습니다. 나는 그렇게하기를 원하지 않는다. MetaMiles 엔티티는 데이터를 가져 오는 엔티티 일뿐입니다. 저장 작업이 필요 없습니다. UserMiles 엔티티 만 저장하고 MetaMiles 엔티티는 저장하지 않으려면 어떻게 지정해야합니까?

내 버튼의 코드는 다음과 같습니다 : 난 아직도이 모든 것을 배우고로 CoreData 슈퍼 잘 알고

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

하지 않습니다. 한 손으로 나는 그것에서 놀고있는 것을 얻고있는 것처럼 느낀다. 그러나 이것과 같은 어리석은 물건은 내가 3 개의 단계를 거꾸로 가져 가고있다라고 생각하게한다.

모두에게 감사드립니다.

답변

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****// 
: 여기

이러한 흥미로운 방법이며
관련 문제