executeFetchRequest:error:导致未捕获的异常'NSRangeException'

时间:2012-08-23 08:52:10

标签: objective-c ios

我在iOS4中遇到了Out of Bounds异常,当我在iOS5中运行我的应用程序时,我没有这样做。

当我调用executeFetchRequest:error:

时,我得到了异常

这是代码:

// Fetch
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"DisplayName = %@", inDisplayName];

    //iOS5 only
    //NSFetchRequest *req = [[NSFetchRequest alloc] initWithEntityName:inEntityName];

    NSFetchRequest *req = [[NSFetchRequest alloc] init];
    [req setEntity:[NSEntityDescription entityForName:inEntityName inManagedObjectContext:inContext]];

    [req setFetchLimit:1];
    [req setPropertiesToFetch:[NSArray arrayWithObject:@"DisplayName"]];
    [req setIncludesPropertyValues:NO];
    [req setIncludesSubentities:NO];
    [req setPredicate:pred];

    NSError *err = nil;
    NSArray * results = [inContext executeFetchRequest:req error:&err]; // here

    if ([results count]) {
        id result = [results objectAtIndex:0];
        [cache setObject:result forKey:cacheKey];        
        return result;
    }

    FreeAndNil(req);

这是日志:

2012-08-23 10:47:35.855 MyApp [2694:11603] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 4294967295 beyond bounds [0 .. 30]'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x01cb15a9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x02035313 objc_exception_throw + 44
    2   CoreFoundation                      0x01ca70a5 -[__NSArrayM objectAtIndex:] + 261
    3   CoreFoundation                      0x01bd674e CFArrayGetValueAtIndex + 190
    4   CoreData                            0x0193c304 -[NSSQLCore _prepareDictionaryResultsFromResultSet:usingFetchPlan:] + 2084
    5   CoreData                            0x018867ac -[NSSQLCore _newRowsForFetchPlan:selectedBy:withArgument:] + 2188
    6   CoreData                            0x0187d151 -[NSSQLCore newRowsForFetchPlan:] + 369
    7   CoreData                            0x0187c515 -[NSSQLCore objectsForFetchRequest:inContext:] + 357
    8   CoreData                            0x0187c0ce -[NSSQLCore executeRequest:withContext:error:] + 206
    9   CoreData                            0x0192bcdc -[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 1084
    10  CoreData                            0x01879267 -[NSManagedObjectContext executeFetchRequest:error:] + 359
    11  MyApp                               0x0002519a +[CMItem objectWithEntityName:displayNameHasValue:inContext:] + 666
    12  MyApp                               0x00024c50 +[CMItem objectWithEntityName:andProperty:hasValue:inContext:] + 208
    13  MyApp                               0x00051ba7 -[CMMyEntity awakeFromInsert] + 295
    14  CoreData                            0x018b0647 -[NSManagedObjectContext insertObject:] + 151
    15  CoreData                            0x0188c680 -[NSManagedObject initWithEntity:insertIntoManagedObjectContext:] + 336
    16  CoreData                            0x018b0a33 +[NSEntityDescription insertNewObjectForEntityForName:inManagedObjectContext:] + 131
    17  MyApp                               0x000909a4 -[CMEditViewControllerBase loadNewCollectible] + 212
    18  MyApp                               0x00091756 -[CMEditViewControllerBase saveButtonPushed:] + 614
    19  UIKit                               0x007204fd -[UIApplication sendAction:to:from:forEvent:] + 119
    20  UIKit                               0x00932cc3 -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 156
    21  UIKit                               0x007204fd -[UIApplication sendAction:to:from:forEvent:] + 119
    22  UIKit                               0x007b0799 -[UIControl sendAction:to:forEvent:] + 67
    23  UIKit                               0x007b2c2b -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
    24  UIKit                               0x007b17d8 -[UIControl touchesEnded:withEvent:] + 458
    25  UIKit                               0x00744ded -[UIWindow _sendTouchesForEvent:] + 567
    26  UIKit                               0x00725c37 -[UIApplication sendEvent:] + 447
    27  UIKit                               0x0072af2e _UIApplicationHandleEvent + 7576
    28  GraphicsServices                    0x0244a992 PurpleEventCallback + 1550
    29  CoreFoundation                      0x01c92944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
    30  CoreFoundation                      0x01bf2cf7 __CFRunLoopDoSource1 + 215
    31  CoreFoundation                      0x01beff83 __CFRunLoopRun + 979
    32  CoreFoundation                      0x01bef840 CFRunLoopRunSpecific + 208
    33  CoreFoundation                      0x01bef761 CFRunLoopRunInMode + 97
    34  GraphicsServices                    0x024491c4 GSEventRunModal + 217
    35  GraphicsServices                    0x02449289 GSEventRun + 115
    36  UIKit                               0x0072ec93 UIApplicationMain + 1160

感谢

更新

- (void)awakeFromInsert
{
    [super awakeFromInsert];
    [self setReadIt:[NSNumber numberWithBool:YES]];
    [self setCLZID:@"0"];
    CMLookUpItemComic *myRating = [CMItem objectWithEntityName:@"MyRating" andProperty:@"DisplayName" hasValue:@"0" inContext:[self managedObjectContext]];
    [self setMyRating:myRating];

    [self setUserValues:[NSEntityDescription insertNewObjectForEntityForName:@"UserValues" inManagedObjectContext:[self managedObjectContext]]];
}

超类:

- (void)awakeFromInsert
{
    [super awakeFromInsert];
    [self autoSetUniqueID];
}

- (void)autoSetUniqueID
{
    if (highestUniqueID == NSNotFound) {
        NSManagedObjectContext *context = [self managedObjectContext];
        NSEntityDescription *entity = [self entity];

        NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"UniqueID"
                                                             ascending:NO];
        NSArray *sortDescriptors = [NSArray arrayWithObject:sort];
        [sort release];

        NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
        [fetch setEntity:entity];
        [fetch setFetchLimit:1];

        [fetch setSortDescriptors:sortDescriptors];
        NSArray *results = [context executeFetchRequest:fetch error:nil];
        [fetch release];

        if (results != nil && [results count]) {
            CMItem *itemWithHighestID = [results lastObject];
            highestUniqueID = [[itemWithHighestID UniqueID] integerValue];
        } else {
            highestUniqueID = 0;
        }
    }

    highestUniqueID++;

    [self setPrimitiveValue:[NSNumber numberWithInt:highestUniqueID] forKey:@"UniqueID"];   
}

2 个答案:

答案 0 :(得分:2)

对于任何进入这个问题而且像我一样失去一整天的人:

NSPredicate *pred = [NSPredicate predicateWithFormat:@"DisplayName = \"%@\"", inDisplayName];

崩溃是由谓词/

中缺少括号\"%@\"引起的

答案 1 :(得分:0)

你所做的让我非常紧张。来自Apple Docs:

  

通常不鼓励您在其中执行提取   执行awakeFromInsert。虽然允许,但执行   获取请求可以触发内部核心数据的发送   通知可能会产生不必要的副作用。例如,在OS中   X,NSArrayController的一个实例可能最终会插入一个新对象   进入其内容数组两次。

我的建议是完全删除-awakeFromInsert代码,并使highestUniqueID成为每次需要时计算的瞬态属性。您可以使用Core Data非常有效地检索属性的最大值。阅读标题为Fetching Specific Values的部分。

您正在尝试重新创建objectID。这段代码没有理由。