views:

18

answers:

0

I am experiencing a strange crash that is related to setting properties (typically bools) for a NSManagedObject.

The method that this code is being run on is on a background thread. It is being called from a [NSURLConnection connectionDidFinishLoading] delegate method.

Here is the code.

{
// Create a new MOC because we are working on a different thread
NSManagedObjectContext *tempMOC = [[NSManagedObjectContext alloc] init];
tempMOC.undoManager = nil;
[tempMOC setMergePolicy:NSMergeByPropertyObjectTrumpMergePolicy];
tempMOC.persistentStoreCoordinator = self.managedObjectContext.persistentStoreCoordinator;

/**
Here I have code to add new objects from the download to the persistent store.
From testing I have confirmed that this area is not the problem
**/

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"RidgeVolumeScan" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"radar == %@", radar];
[request setPredicate:predicate];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timestamp" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptors release];
[sortDescriptor release];

NSError *error;
NSArray *scans = [managedObjectContext executeFetchRequest:request error:&error];
[request release];

for (RidgeVolumeScan *scan in scans)
{       
            // I will show this method below (this seems to be the method that is failing)
    [scan setAvailability:[timestampArray containsObject:[dateFormatter stringFromDate:scan.timestamp]] forProduct:currentProduct];

    BOOL delete = YES;

    for (NSString *product in [NSArray arrayWithObjects:@"N0R", @"N0Z", @"NCR", @"N0V", @"N0S", @"N1P", @"NTP", @"NVL", @"NET", nil])
    {
        if ([scan availabilityForProduct:product] && ([scans indexOfObject:scan] < 20))
            delete = NO;
    }

    if (delete)
        [tempMOC deleteObject:scan];
}

NSError *error;
if (![tempMOC save:&error])
       NSLog(@"%@", error);

}

- (void)setAvailability:(BOOL)availability forProduct:(NSString *)product
{
    if ([product isEqual:@"N0R"])
        self.N0Ravailable = [NSNumber numberWithBool:availability];
    if ([product isEqual:@"N0Z"])
        self.N0Zavailable = [NSNumber numberWithBool:availability];
    if ([product isEqual:@"NCR"])
        self.NCRavailable = [NSNumber numberWithBool:availability];
    if ([product isEqual:@"N0V"])
        self.N0Vavailable = [NSNumber numberWithBool:availability];
    if ([product isEqual:@"N0S"])
        self.N0Savailable = [NSNumber numberWithBool:availability];
    if ([product isEqual:@"N1P"])
        self.N1Pavailable = [NSNumber numberWithBool:availability];
    if ([product isEqual:@"NTP"])
        self.NTPavailable = [NSNumber numberWithBool:availability];
    if ([product isEqual:@"NVL"])
        self.NVLavailable = [NSNumber numberWithBool:availability];
    if ([product isEqual:@"NET"])
        self.NETavailable = [NSNumber numberWithBool:availability];
}

I am getting a EXC_BAD_ACCESS on the save call. It crashes before the error is printed out to the screen. I get a similar crash in one other area in the program (in a place where I am setting another bool value for a NSManagedObject), but this is where it happens most frequently (I would say 20% of the time I run this particular code).

If I comment out the setAvailability:forProduct: method call then the save occurs every time, but that is an important part of the program so that is not really a fix. Any ideas? Thanks.