NSManagedObjectContext executeFetchRequest返回erratic对象,导致EXC_BAD_ACCESS,SIGABRT等 [英] NSManagedObjectContext executeFetchRequest returns erratic objects, causes EXC_BAD_ACCESS, SIGABRT, etc
问题描述
我的猜测是我在这里缺少一些核心数据的基本理解,但这里:
My guess is I'm missing some Core Data fundamental understanding here, but here goes:
我有几个获取请求在我的应用程序处理检索不同的东西。在某些情况下,代码运行正常,返回请求的对象。
I have several fetch requests in my app to handle retrieval of different things. In some cases the code runs fine, returning the requested objects.
在某些情况下,它返回似乎是已经释放的对象后来试图引用返回的结果给出EXC_BAD_ACCESS)。由于我在代码中设置了各种断点和日志语句,因此它也会在代码中的其他位置得到偶尔的SIGABRT或EXC_BAD_ACCESS。
In some cases, it returns what appear to be already-released objects (e.g. just a few lines of code later trying to reference the returned result gives EXC_BAD_ACCESS). As I set various breakpoints and log statements in the code and step through it will also get the occasional SIGABRT or EXC_BAD_ACCESS in other locations in the code.
在每种情况下,它似乎是我参考获取请求的结果。
In every case, it appears to be when I go to reference the result of a fetch request.
以下是一个此类提取请求的示例:
Here's a sample of one such fetch request:
// Who am I?
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *signedInPersonId = [defaults stringForKey:@"signedInPersonId"];
// Return (if any) the Request object with given UUID
RequestStrings *r = [[RequestStrings alloc] init];
NSEntityDescription *description = [NSEntityDescription entityForName:r.table_Request inManagedObjectContext:moc];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(UUID == %@) && (dataOwnerId == %@)", UUID, signedInPersonId];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:description];
[request setPredicate:predicate];
NSError *error = nil;
NSArray *requests = [moc executeFetchRequest:request error:&error];
Request *returnRequest = nil;
if (requests != nil) {
if ([requests count] > 0) {
NSLog(@"getRequestWithId - requests array: %@, first: %@", requests, [requests objectAtIndex:0]);
returnRequest = [requests objectAtIndex:0];
}
else {
returnRequest = nil;
}
}
[r release];
[request release];
return returnRequest;
这里有一些更多的信息
在某些情况下,同样的代码将返回所需的对象,或者抛出一个异常,说明 [NSCFNumber length]
是一个无法识别的选择器。不确定同一个实体说明+撷取要求如何在一种情况下传回数组,在另一种情况下传回数字。
P.S. Here's some more info
In some instances the same code will return the desired objects, or throw an exception stating that [NSCFNumber length]
is an unrecognized selector. Not sure how the same entity description + fetch request could return an array in one case and a Number in the other.
推荐答案
基本上这样做:
// (1) Create an array of stuff
NSArray *myArray = [NSarray arrayWithObjects:a, b, c, nil];
// (2) Take the first one off
id myObject = [myArray objectAtIndex:0];
// (3) Release everything
[myArray release];
// (4) Return myObject
return myObject;
您只是使用CoreData执行步骤(1)。
You're just using CoreData to do step (1).
步骤(1)返回一个对象数组。保留这些对象的唯一的东西是他们在*中的数组。如果您释放数组(步骤3),您将释放其中的所有对象。你为什么期望myObject仍然存在的步骤(4)?
Step (1) returns an array of objects. The only thing retaining these objects is the array that they're in*. If you release the array (step 3), you will release all the objects that are inside it. Why do you expect myObject to still exist by step (4)?
试试这个:
// Make sure that we keep a retain of returnRequest
returnRequest = [[[requests objectAtIndex:0] retain] autorelease];
做出了一个假设,使我的回答更简单。在现实世界中,你不知道什么是保留你的对象 - 这是由框架和你的代码。但是,保留任何您期望保留的内容是一个好习惯。
*For the pedantic out there : I've made an assumption that makes my answer simpler. In the real world you don't know what's retaining your objects - that's up to the framework as well as your code. However, it's good practice to retain anything you expect to keep around.
这篇关于NSManagedObjectContext executeFetchRequest返回erratic对象,导致EXC_BAD_ACCESS,SIGABRT等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!