gpt4 book ai didi

iPhone SDK核心数据: Fetch all entities with a nil relationship?

转载 作者:行者123 更新时间:2023-12-03 18:34:00 28 4
gpt4 key购买 nike

我有一个包含书籍和作者的核心数据项目。在数据模型中,作者与书籍具有一对多关系,书籍与作者具有1-1关系。我正在尝试提取所有没有作者的书籍。无论我如何尝试,都没有返回结果。在我的谓词中,我也尝试过= NIL,== nil,== NIL。如有任何建议,我们将不胜感激。

// fetch all books without authors
- (NSMutableArray *)fetchOrphanedBooks {
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Book" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:20];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"author == nil"];
[fetchRequest setPredicate:predicate];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];

NSString *sectionKey = @"name";//nil;
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext
sectionNameKeyPath:sectionKey cacheName:nil];
BOOL success = [aFetchedResultsController performFetch:nil];
NSMutableArray *orphans = nil;

// this is always 0
NSLog(@"Orphans found: %i", aFetchedResultsController.fetchedObjects.count);

if (aFetchedResultsController.fetchedObjects.count > 0)
{
orphans = [[NSMutableArray alloc] init];
for (Book *book in aFetchedResultsController.fetchedObjects)
{
if (book.author == nil)
{
[orphans addObject:book];
}
}

}

[aFetchedResultsController release];
[fetchRequest release];
[sortDescriptor release];
[sortDescriptors release];

return [orphans autorelease];

}

最佳答案

尝试使用零计数:

NSPrdicate *predicate = [NSPredicate predicateWithFormat:@"author == nil || author.@count =0"];

关于iPhone SDK核心数据: Fetch all entities with a nil relationship?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2745994/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com