2 つのタブのアプリケーションがあります。最初のものでは、"Sample" および "SampleList" エンティティのオブジェクトを作成しています。各 sampleList には、ID とサンプルのセットが含まれています。各サンプルには、日付と温度のプロパティが含まれています。
2 番目のタブでは、データを tableView に表示しています。私は実装しました
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
メソッドを使用して SampleList を削除します。私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];
編集: 間違いを見つけました。次のように、各 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];
}