3

注意,我已经检查了以下帖子:(堆栈溢出问题 1 堆栈溢出问题 2

我在我的应用程序中看到以下崩溃堆栈:

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)

这是一个 sigsegv,所以它不能通过 @try..@catch 捕获。这种情况很少发生,所以我很好奇这是否可能是删除和添加之间的评论中指出的竞争条件。

调用的代码是:

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

}

对此的任何指导表示热烈赞赏。要求澄清的评论请求将得到迅速回应。

4

0 回答 0