Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core data executeFetchRequest fail with exc_bad_access

From my understanding, this is a memory issue, especially since I call the method from several places several times with different timers.

Code below that throw the exception:

- (NSMutableArray*)getAllTraps
{
    @synchronized(self)
    {
        self.fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Trap"];
        NSError *error = nil;
        NSArray *results = [self.managedObjectContext executeFetchRequest:self.fetchRequest error:&error];

        if (!results)
        {
            NSLog(@"Error fetching traps: %@", error.localizedDescription);
            NSLog(@"Reason: %@", error.localizedFailureReason);
            NSLog(@"Suggestion: %@", error.localizedRecoverySuggestion);
            abort();
        }


        if (error != nil)
        {
            // Handle error
            NSLog(@"Error getting all traps");
        }
        else
        {
            // Handle success
            NSLog(@"Success getting all traps");
        }


        NSMutableArray *arrayOfAllTraps = [[NSMutableArray alloc] init];
        for (int i = 0; i < results.count; i++)
        {
            Trap *singleTrap = results[i];
            NSMutableDictionary *singleDict = [[NSMutableDictionary alloc] init];

            if (singleTrap.trapID.integerValue > 0)
            {
                singleDict[ID] = singleTrap.trapID;
                singleDict[ALARMDISTANCE] = singleTrap.alarmDistance;
                singleDict[ISACTIVE] = singleTrap.isActive;
                singleDict[LAT] = singleTrap.lat;
                singleDict[LON] = singleTrap.lon;
                singleDict[POLYGONS] = singleTrap.polys;


                // NSLog(@"Trap ID: %@, Trap Description: %@", singleTrap.trapID, singleTrap.trapDescription);
                singleDict[DESCRIPTION] = singleTrap.trapDescription;


                singleDict[ROADNUMBER] = singleTrap.roadNumber;
                singleDict[TYPE] = singleTrap.type;
                singleDict[DEGREES] = singleTrap.degrees;
                singleDict[DIRECTION] = singleTrap.direction;


                if (singleTrap.poly0 == nil)
                {
                    singleDict[POLYGON_A] = @"";
                }
                else
                {
                    singleDict[POLYGON_A] = singleTrap.poly0;
                }

                // Make sure not to set NULL value #1
                if (singleTrap.poly1 == nil)
                {
                    singleDict[POLYGON_B] = @"";
                }
                else
                {
                    singleDict[POLYGON_B] = singleTrap.poly1;
                }

                if (singleTrap.poly2 == nil)
                {
                    singleDict[POLYGON_C] = @"";
                }
                else
                {
                    singleDict[POLYGON_C] = singleTrap.poly2;
                }


                // Make sure not to set NULL value #2
                if (singleTrap.polygonAzimut1 == nil)
                {
                    singleDict[POLYGON_A_AZIMUTH] = @"";
                }
                else
                {
                    singleDict[POLYGON_A_AZIMUTH] = singleTrap.polygonAzimut1;
                }

                if (singleTrap.polygonAzimut2 == nil)
                {
                    singleDict[POLYGON_B_AZIMUTH] = @"";
                }
                else
                {
                    singleDict[POLYGON_B_AZIMUTH] = singleTrap.polygonAzimut2;
                }

                if (singleTrap.polygonAzimut3 == nil)
                {
                    singleDict[POLYGON_C_AZIMUTH] = @"";
                }
                else
                {
                    singleDict[POLYGON_C_AZIMUTH] = singleTrap.polygonAzimut3;
                }

                [arrayOfAllTraps addObject:singleDict];
            }
        }

        return arrayOfAllTraps;
    }
}

The fail come right after:

NSArray *results = [self.managedObjectContext executeFetchRequest:self.fetchRequest error:&error];

And not even go inside 'if'.

like image 681
Idan Moshe Avatar asked Nov 01 '22 13:11

Idan Moshe


1 Answers

Try this,

NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
NSEntityDescription *entityDescription = [NSEntityDescription

                                                  entityForName:@"Trap"  inManagedObjectContext:self.managedObjectContext];

[fetch setEntity:entityDescription];

 NSError * error = nil;
 NSArray *results = [self.managedObjectContext executeFetchRequest:fetch error:&error];
like image 59
karthika Avatar answered Nov 15 '22 05:11

karthika