大家好,我有这个功能:
//retrieves checksum within core data store, returns 0 if a checksum has not been stored
-(double)getStoredChecksum {
NSError *error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"DataFeedManager" inManagedObjectContext:[self managedObjectContext]];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [[self managedObjectContext] executeFetchRequest:fetchRequest error:&error];
//+for (NSManagedObject* item in fetchedObjects) //REMOVE
//+[[self managedObjectContext] deleteObject:item]; //REMOVE FOR CLEARING CHECKSUM CONTENTS
if ([fetchedObjects count] == 0){ //array is empty
NSLog(@"FETCHED OBJECTS IS EMPTY");
checksumStored = NO;
return 0;
}
if (fetchedObjects == nil) {
NSLog(@"Error while fetching\n%@",
([error localizedDescription] != nil) ? [error localizedDescription] : @"Unknown Error");
}
if ([fetchedObjects count] > 1)
NSLog(@"====BAD:Multiple Checksums Retrieved!!!!====");
checksumStored = YES;
double returnable = [[[fetchedObjects objectAtIndex:0] valueForKey:@"lastUpdateCheckSum"] doubleValue];
return returnable;
}//getStoredChecksum()
如您所见,它执行一个获取请求,但是我不确定如何检查是否发生错误。如您所见,我正在检查数组是否为空,[fetchedObjects count] == 0
但是,我在某处听说如果发生错误并且数组为 nil,则计数也返回 0,因此可以安全地测试数组是否为空方式,但它掩盖了我对空数组的检查。因此,问题是我没有确定的方法来检查是否发生错误。关于我应该如何去做这件事的任何想法?
提前致谢!
编辑:
在这种情况下,我将如何检查错误?
NSUInteger numEntities = [[self managedObjectContext] countForFetchRequest:fetchRequest error:&error];
由于 NSUInteger 被指定为无符号整数,所以我什至不能给它赋值 -1 并在以后检查 -1 如果我愿意。