在这种情况下如何确定是否发生错误(Objective-C)

发布于 2024-10-26 15:25:13 字数 1823 浏览 4 评论 0原文

嘿伙计们,我有这个函数:

//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 检查数组是否为空,但是,我听说如果发生错误并且数组为零,则返回 0 count 也是如此,因此以这种方式测试数组是否为空是安全的,但它掩盖了我对空数组的检查。因此,问题是我没有确定的方法来检查是否发生错误。关于我应该如何去做这件事有什么想法吗?

提前致谢!

编辑:

在这种情况下我将如何检查错误?

NSUInteger numEntities = [[self managedObjectContext] countForFetchRequest:fetchRequest error:&error];

由于 NSUInteger 被指定为无符号整数,因此我什至无法为其分配 -1 值并在以后检查 -1(如果我愿意)。

Hey guys, I have this function:

//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()

As you can see, it executes a fetch request, however I am unsure of how to check if an error occurred. As you can see, I am checking to see if the array is empty via [fetchedObjects count] == 0 however, I heard somewhere that if the error occurs and the array is nil, 0 is returned for count as well, thus it is safe to test for array emptiness that way, but it masks my check for a null array. Thus the problem is that I have no definitive way of checking if an error occurred. Any thoughts on how I should go about doing this?

Thanks in advance!

EDIT:

How would I check for an error in this case?

NSUInteger numEntities = [[self managedObjectContext] countForFetchRequest:fetchRequest error:&error];

As NSUInteger is specified as an unsigned int, so I can't even assign it a value of -1 and check for a -1 later if I wanted to.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

贪恋 2024-11-02 15:25:13

在调用 executeFetchRequest:: 之前将 error 设置为 nil。如果 error 不是 nil,那么它被设置,因此发生错误。

count 检查的原因是屏蔽 < code>nil 检查是因为发送到 nil 的任何消息都会返回 nil,在本例中也是 0。通过检查 可以轻松解决此问题首先 nil,或者修改您的 if 语句,如下所示:

if (fetchedObjects && [fetchedObjects count] == 0){

对于 countForFetchRequest:: 的情况,返回 NSNotFound发生错误,因此只需检查该错误即可。

Set error to nil before calling executeFetchRequest::. If error is not nil after then it was set and thus an error occured.

The reason the count check is masking the nil check is because any message sent to nil returns nil which in this case is also 0. This can be easily fixed by checking for nil first, or modify your if statement so it is as followed:

if (fetchedObjects && [fetchedObjects count] == 0){

For the case with countForFetchRequest:: NSNotFound is returned when an error occurs, so just check for that.

陪我终i 2024-11-02 15:25:13

复制&从 countForFetchRequest:error: 的文档中粘贴

返回值

如果给定的获取请求被传递给executeFetchRequest:error:,则该请求将返回的对象数量;如果发生错误,则为NSNotFound。

所以,

if (numEntities == NSNotFound) {
    // Handle the error
}

Copy & pasted from the docs for countForFetchRequest:error:

Return Value

The number of objects a given fetch request would have returned if it had been passed to executeFetchRequest:error:, or NSNotFound if an error occurs.

So,

if (numEntities == NSNotFound) {
    // Handle the error
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文