3

A note, I've already checked the following posts:(Stack overflow issue 1 Stack overflow issue 2

I am seeing the following crash stack in my app:

2  libsystem_c.dylib 0x32bc87ec _sigtramp + 48
3  CoreData          0x361a2e70 -[NSSQLCore _populateRowForOp:withObject:] + 2716
4  CoreData          0x36194ca2 -[NSSQLCore recordUpdateForObject:] + 130
5  CoreData          0x3619defc -[NSSQLCore recordChangesInContext:] + 600
6  CoreData          0x3619bfae -[NSSQLCore saveChanges:] + 286
7  CoreData          0x360f425e -[NSSQLCore executeRequest:withContext:error:] + 946
8  CoreData          0x360f3336 -[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 1130
9  CoreData          0x3615b286 -[NSManagedObjectContext save:] + 522
10 MyCrashingApp     0x000aa040 -[DisplayModelMgr saveDisplayModel:forDate:] (DisplayModelMgr.m:182)

This is a sigsegv so, it's not catchable via a @try..@catch. This only occurs rarely, so I am curious if it may be a race condition where noted in the comments between the deletion and the addition.

The code for the call is:

- (void) saveDisplayModel:(DisplayModel *)model forDate:(NSDate *)date
{
if (model == nil || date == nil)
    return;

[log debug:[model description]];

[log debug:@"DMM: saveDisplayModel: %@ lastSyncDate: %@",date, [model lastSyncDate]];

NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"MyDisplayModel" inManagedObjectContext:self.context];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(requestDate == %@)", date];
[request setPredicate:predicate];
[request setFetchLimit:1];
[request setEntity:entity];

NSError *error = nil;
NSArray *mutableFetchResults = [self.context executeFetchRequest:request error:&error];

if (mutableFetchResults != nil) {
    [log debug:@"DMM: mutableFetchResults:count %i",[mutableFetchResults count]];
    NSDate *sDate = [[model.lastSyncDate copy] autorelease];
    NSDate *timestamp = [NSDate date];
    MyDisplayModel *coreDataModel = nil;
    if([mutableFetchResults count] > 0) {
        coreDataModel = [mutableFetchResults objectAtIndex:0];
        [coreDataModel setSyncDate:sDate];
        [coreDataModel setTimeStamp:timestamp];
        [coreDataModel setRequestDate:date];
        if(model.entries != nil && [model.entries count] > 0) {

            for(MyDisplayModelValue *existingVal in coreDataModel.values) {
                [self.context deleteObject:existingVal];
            }
        }
        [coreDataModel setValues:nil]; // race condition?
    } else {
        coreDataModel = [NSEntityDescription
                                        insertNewObjectForEntityForName:@"MyDisplayModel"
                                        inManagedObjectContext:self.context];
        [coreDataModel setSyncDate:sDate];
        [coreDataModel setTimeStamp:timestamp];
        [coreDataModel setRequestDate:date];
    }
    for(DisplayModelEntry *dmv in [model.entries allValues]) {
        MyDisplayModelValue *newDashModelVal = [NSEntityDescription
                                                  insertNewObjectForEntityForName:@"MyDisplayModelValue"
                                                  inManagedObjectContext:self.context];
        [newDashModelVal setType:[NSNumber numberWithInt:dmv.type]];
        [newDashModelVal setActual:[NSNumber numberWithInt:dmv.actual]];
        [newDashModelVal setSource:[NSNumber numberWithInt:dmv.source]];
        [newDashModelVal setTarget:[NSNumber numberWithInt:dmv.target]];
        [newDashModelVal setMymodel:coreDataModel];
        [[coreDataModel mutableSetValueForKey:@"values"] addObject:newDashModelVal]; // create new if missing?
    }
    NSError *error;
    if (![self.context save:&error]) {
        [log error:@"Error Saving Dashbaord Cache: %@",[error description]];
    }
    [self.context refreshObject:coreDataModel mergeChanges:NO];
}

}

Any guidance on this warmly appreciated. Comment requests for clarifications will be quickly responded to.

Community
  • 1
  • 1
Dru Freeman
  • 1,766
  • 3
  • 19
  • 41
  • Do you have multiple threads using core data? Have you tried with NSZombies enabled? – JosephH Oct 12 '12 at 17:35
  • There is an NSOperationQueue being used, but the core data handling is done after the operation ends back on the main thread. This crash is on the main thread. Also. This is coming after a BlueTooth function so we can't run the test in the simulator to get NSZombies to work. There's also a severe lack of repro to get this to happen, so triggering it has been impossible to do in debug. – Dru Freeman Oct 12 '12 at 17:58
  • NSZombies can be used on a device – JosephH Oct 12 '12 at 22:08
  • @JosephH I've had no luck with this, either in the runtime environment variables or in the runtime preferences. – Dru Freeman Oct 13 '12 at 02:03

0 Answers0