我有一個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];
}
newSampleSet是通過alloc + init創建的,然後再由newSampleList保留,所以你需要做一個[newSampleSet release]來平衡alloc + init。當newSampleList被自動釋放時,它也會釋放newSampleSet。 – pxl 2010-06-01 09:49:59
當然,我對內存管理做錯了什麼。我正在看你指出的文件。 我做錯了NSDate。事實上,我試圖刪除我的代碼(和xcdatamodel)中日期的所有內容,並且我的應用程序不再崩潰。我將深入研究這些日期和內存管理。 – leochab 2010-06-01 14:17:42
所以內存管理文檔鏈接一直很有用!謝謝 – leochab 2010-06-02 07:57:34