核心数据executeFetchRequest失败,带有exc_bad_access

时间:2013-09-30 07:33:43

标签: ios objective-c core-data nsfetchrequest

根据我的理解,这是一个内存问题,特别是因为我使用不同的计时器从多个地方多次调用该方法。

下面的代码抛出异常:

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

失败之后:

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

甚至没有进入'if'。

1 个答案:

答案 0 :(得分:2)

试试这个,

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