核心数据 executeFetchRequest 因 exc_bad_access 而失败
Posted
技术标签:
【中文标题】核心数据 executeFetchRequest 因 exc_bad_access 而失败【英文标题】:Core data executeFetchRequest fail with exc_bad_access 【发布时间】:2013-09-30 07:33:42 【问题描述】:据我了解,这是一个内存问题,特别是因为我使用不同的计时器从多个地方多次调用该方法。
下面抛出异常的代码:
- (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];
甚至不要进入“如果”。
【问题讨论】:
这可能会有所帮助:***.com/questions/6660851/… 我看到了,这是很老的帖子,因为代码不是 ARC,而我的代码是 arc。 向我们展示 managedObjectContext 初始化代码。你使用什么类型的并发类型? #define appDelegate ((AppDelegate*)[UIApplication sharedApplication].delegate) self.managedObjectContext = appDelegate.managedObjectContext; 【参考方案1】:试试这个,
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];
【讨论】:
嗨。我正在创建我的 fetch 对象,如下所示: NSFetchRequest *request = [NSFetchRequest new];并像这样设置实体:[request setEntity:entity];我的代码在使用 EXC_BAD_ACCESS 执行获取请求时崩溃。知道为什么吗?以上是关于核心数据 executeFetchRequest 因 exc_bad_access 而失败的主要内容,如果未能解决你的问题,请参考以下文章
核心数据 executeFetchRequest 返回 NSManagedObject
核心数据 executeFetchRequest 因 exc_bad_access 而失败
核心数据 fetchRequest 给出 executeFetchRequest:error: <null> is not a valid NSFetchRequest
NSPredicate,executeFetchRequest 崩溃