NSFetchRequest 中函数计数的表达式返回的结果比常规获取少 1

发布于 2024-10-06 12:29:51 字数 2680 浏览 11 评论 0原文

在我第一次尝试在获取请求中使用 NSExpression 时,我得到的结果与使用常规获取请求时得到的结果一致。

MO“主题”与 MO“书”具有一对多关系,相反是一对一关系。

这是我正在使用的 NSExpression fetchRequest:(

Project_AppDelegate *appDelegate = [[NSApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@“Book”
                                                     inManagedObjectContext:context];
Subject *subjectToDelete = [self.arrayOfSubjects objectAtIndex:indexSelected];    
NSPredicate *pred = [NSPredicate predicateWithFormat:@"subject == %@", subjectToDelete];
NSExpression *expn = [NSExpression expressionForFunction:@"count:" 
                                                     arguments:[NSArray arrayWithObject:[NSExpression expressionForKeyPath:@"idPerm"]]];
NSExpressionDescription *expnDesc = [[NSExpressionDescription alloc] init];
[expnDesc setExpression:expn];
[expnDesc setName:@“countMatchingBooks”];
[expnDesc setExpressionResultType:NSInteger64AttributeType];
NSArray *properties = [NSArray arrayWithObject:expnDesc];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDescription]; 
[request setPredicate:pred];
[request setPropertiesToFetch:properties];
[request setResultType:NSDictionaryResultType];
NSError *error = nil; 
NSArray *results = [context executeFetchRequest:request error:&error];
if (error) {
    // error handling here
}
[request release];
[expnDesc release];

// Retrieve the count from the results array.
NSNumber *numBooksAssignedSubjectToDelete = [[results objectAtIndex:0] valueForKey:@“countMatchingBooks”];
uint64_t uloloBooksAssignedSubjectToDelete = [numBooksAssignedSubjectToDelete unsignedLongLongValue];

这个想法是向用户呈现一个确认面板,告知他们如果选择删除所选主题,将通过级联规则删除多少本书 - 不会对 Book MO 造成错误)

这是我用作测试的简单 fetchRequest:

NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@“Book”
                                                     inManagedObjectContext:contextMain];

NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDescription]; 
NSError *error = nil; 
NSArray *booksAll = [contex executeFetchRequest:request error:&error];
[request release];
// Loop through the “booksAll” array and count those whose subject matches the one assigned as “subjectToDelete”

发生的情况是,如果 NSExpression fetchRequest 返回 n 的计数,则简单的 fetchRequest 返回 n + 1 的计数。

认为 fetchRequest 本身可能以某种方式更改数据,我尝试以不同的顺序运行它们,但结果相同。

也许使用表达式的请求会跳过尚未保存的 MO?不。我进行了一项测试,创建了一堆新的“Book”MO,以查看表达式请求和常规请求之间的差距是否会扩大。它仍然是一次性的。

知道我做错了什么吗?

In my first attempt at using NSExpression in a fetch request, I’m getting a result that is consistently one off from what I get if I use a regular fetch request.

The MO “Subject” has a to-many relationship to the MO “Book,” the inverse being to-one.

This is the NSExpression fetchRequest I’m using:

Project_AppDelegate *appDelegate = [[NSApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@“Book”
                                                     inManagedObjectContext:context];
Subject *subjectToDelete = [self.arrayOfSubjects objectAtIndex:indexSelected];    
NSPredicate *pred = [NSPredicate predicateWithFormat:@"subject == %@", subjectToDelete];
NSExpression *expn = [NSExpression expressionForFunction:@"count:" 
                                                     arguments:[NSArray arrayWithObject:[NSExpression expressionForKeyPath:@"idPerm"]]];
NSExpressionDescription *expnDesc = [[NSExpressionDescription alloc] init];
[expnDesc setExpression:expn];
[expnDesc setName:@“countMatchingBooks”];
[expnDesc setExpressionResultType:NSInteger64AttributeType];
NSArray *properties = [NSArray arrayWithObject:expnDesc];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDescription]; 
[request setPredicate:pred];
[request setPropertiesToFetch:properties];
[request setResultType:NSDictionaryResultType];
NSError *error = nil; 
NSArray *results = [context executeFetchRequest:request error:&error];
if (error) {
    // error handling here
}
[request release];
[expnDesc release];

// Retrieve the count from the results array.
NSNumber *numBooksAssignedSubjectToDelete = [[results objectAtIndex:0] valueForKey:@“countMatchingBooks”];
uint64_t uloloBooksAssignedSubjectToDelete = [numBooksAssignedSubjectToDelete unsignedLongLongValue];

(The idea is to present the user with a confirmation panel advising them of how many Books will be deleted via the Cascade rule if they choose to delete the chosen Subject — without faulting the Book MOs at this point.)

And this is the simple fetchRequest I’m using as a test:

NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@“Book”
                                                     inManagedObjectContext:contextMain];

NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDescription]; 
NSError *error = nil; 
NSArray *booksAll = [contex executeFetchRequest:request error:&error];
[request release];
// Loop through the “booksAll” array and count those whose subject matches the one assigned as “subjectToDelete”

What happens is that if the NSExpression fetchRequest returns a count of n, the simple fetchRequest returns a count of n + 1.

Thinking the fetchRequests themselves might be somehow altering the data, I tried running them in a different order, but with the same result.

Maybe requests using expressions skip MOs which have not yet been saved? No. I ran a test that creates a bunch of new “Book” MOs to see if the gap between expression request and regular request would widen. It remained exactly one off.

Any idea what I’m doing wrong?

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

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

发布评论

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

评论(1

孤独患者 2024-10-13 12:29:52

使用 NSExpressionDescription 的 NSFetchRequest 不包含未保存的对象。 NSFetchRequest 有一个方法 -setIncludePendingChanges:,当结果类型为 NSDictionaryResultType 时,该方法不接受 YES。这意味着您无法使用 NSExpressionDescription 来获取未保存的对象。

NSFetchRequests using NSExpressionDescription does not include unsaved objects. NSFetchRequest has a method -setIncludePendingChanges:, which does not accept YES when the result type is NSDictionaryResultType. This means that you cannot use NSExpressionDescription to get unsaved objects.

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