0

我有一個2選項卡應用程序。在第一個中,我創建了「Sample」和「SampleList」實體的對象。每個sampleList包含一個ID和一組樣本。每個樣品都包含日期和溫度屬性。CoreData:應用程序在刪除創建的最後一個實例時崩潰

在第二個選項卡,我在的tableView顯示我的數據。我爲了刪除SampleLists實施

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

方法。在我的xcdatamodel中,SampleList和Sample之間關係的刪除規則是Cascade

我的問題是,當我嘗試刪除SampleList我剛剛創建的應用程序崩潰,我收到一個EXC_BAD_ACCESS信號。如果我重新啓動它,那麼我可以毫無問題地刪除「舊」sampleList。

早些時候,我有以下問題:我無法顯示,因爲我公司推出的應用程序,我創建了sampleLists,因爲它太崩潰。我也收到了EXC_BAD_ACCESS信號。實際上,該集合的最後一個樣本的日期似乎是nil。如果我沒有發佈NSDate來設置樣本的日期,我不再有這個問題了...

如果有人能幫我找出什麼可能會導致我的麻煩,那將是太棒了! !

這裏是我用來創建新實例的方法:

SampleList *newSampleList = (SampleList *)[NSEntityDescription insertNewObjectForEntityForName:@"SampleList" inManagedObjectContext:managedObjectContext]; 
[newSampleList setPatchID:patchID]; 
NSMutableSet *newSampleSet = [[NSMutableSet alloc] init]; 
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 

for (int i = 0; i < [byteArray count]; i=i+4, sampleCount++) { 
    NSDateComponents *comps = [[NSDateComponents alloc] init]; 
    [comps setYear:year]; 
    [comps setMonth:month]; 
    [comps setDay:day]; 
    [comps setHour:hours]; 
    [comps setMinute:minutes]; 
    NSDate *sampleDate = [gregorian dateFromComponents:comps]; 

    Sample *newSample = (Sample *)[NSEntityDescription insertNewObjectForEntityForName:@"Sample" inManagedObjectContext:managedObjectContext]; 

    [newSample setSampleDate:sampleDate]; 
    [newSample setSampleTemperature:[NSNumber numberWithInt:temperature]]; 

    [newSampleSet addObject:newSample]; 
    [comps release]; 
    //[sampleDate release]; 
} 

[newSampleList setSampleSet:newSampleSet]; 
// [newSampleSet release]; 

NSError *error; 
if (![managedObjectContext save:&error]) { 
    NSLog(@"Could not Save the context !!"); 
} 

[gregorian release]; 

編輯: 我發現我錯了。 我在做一個comparaison每個sampleDate這樣的:

NSDate *maxDate = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate:0]; 
(...) 
for (int i = 0; i < [byteArray count]; i=i+4, sampleCount++) { 
    (...) 
    if ([maxDate compare:sampleDate] == NSOrdredAscending){ 
     max = sampleDate; 
    } 

在哪裏我應該做的事情:

if ([maxDate compare:sampleDate] == NSOrdredAscending){ 
    [maxDate release]; 
    maxDate = [sampleDate retain]; 
} 

回答

1

根據用於NSEntityDescription文檔,

+ (id)insertNewObjectForEntityForName:(NSString *)entityName inManagedObjectContext:(NSManagedObjectContext *)context 

返回一個自動釋放的對象。所以,你不需要後釋放:

[newSampleList setSampleSet:newSampleSet]; 

newSampleList最終會被自動釋放,這是造成你看,當你重新啓動你的應用程序,因爲有時候得到一個EXC_BAD_ACCESS什麼。

蘋果,當你需要自己釋放的對象,當對象被自動釋放memory management documentation會給你一個最佳實踐。

+0

newSampleSet是通過alloc + init創建的,然後再由newSampleList保留,所以你需要做一個[newSampleSet release]來平衡alloc + init。當newSampleList被自動釋放時,它也會釋放newSampleSet。 – pxl 2010-06-01 09:49:59

+0

當然,我對內存管理做錯了什麼。我正在看你指出的文件。 我做錯了NSDate。事實上,我試圖刪除我的代碼(和xcdatamodel)中日期的所有內容,並且我的應用程序不再崩潰。我將深入研究這些日期和內存管理。 – leochab 2010-06-01 14:17:42

+0

所以內存管理文檔鏈接一直很有用!謝謝 – leochab 2010-06-02 07:57:34

0

我喜歡用的方法類似下面得到更翔實的核心數據錯誤報告:

- (void) detailedStoreError:(NSError *)error { 
    NSLog(@"Failed to save to data store: %@", [error localizedDescription]); 
    NSArray *_detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey]; 
    if (_detailedErrors != nil && [_detailedErrors count] > 0) { 
     for (NSError *_detailedError in _detailedErrors) { 
      NSLog(@" DetailedError: %@", [_detailedError userInfo]); 
     } 
    } 
    else { 
     NSLog(@" %@", [error userInfo]); 
    } 
} 

你可以按如下方式使用它:

NSError *error; 
if (![managedObjectContext save:&error]) { 
    [self detailedStoreError:error]; 
} 

更翔實的錯誤報告可以幫助您解決。

+0

感謝您的建議,但我在保存上下文時沒有任何錯誤。但現在,我會這樣做! – leochab 2010-06-01 08:32:30

相關問題