0
我正在嘗試向實體添加自定義字段,因此,在使用NSFetchedResultsController提取對象時,可以使用該字段創建謂詞。本場是FULL_NAME:在NSPredicate中使用實體的自定義字段
@dynamic first_name;
@dynamic last_name;
@dynamic full_name;
-(NSString *)full_name {
[self willAccessValueForKey:@"full_name"];
NSString *name = [NSString stringWithFormat:@"%@ %@", self.first_name, self.last_name];
[self didAccessValueForKey:@"full_name"];
return name;
}
然後,使用NSFetchedResultsController,我試圖通過FULL_NAME獲取匹配的對象:
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:[self managedObjectContext]];
[fetchRequest setEntity:entity];
NSArray *sortDescriptors = [NSArray arrayWithObject:[[NSSortDescriptor alloc] initWithKey:@"full_name" ascending:YES]];
NSMutableArray *predicateArray = [NSMutableArray array];
[predicateArray addObject:[NSPredicate predicateWithFormat:@"contact_full_name contains == '%@' ", trimmedSearchString]];
filterPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:predicateArray];
[fetchRequest setPredicate:filterPredicate];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[self managedObjectContext] sectionNameKeyPath:nil cacheName:nil];
但是,查詢總是返回空的結果,即使查詢的全名存在在數據存儲區中。任何想法爲什麼謂詞不會返回有效的結果?
(1)和(2)是錯誤複製粘貼的結果,對不起! 因此,除非沿first_name和last_name存儲full_name(這將導致額外的存儲空間消耗),否則我將不得不基於first_name和last_name的組合構建謂詞... – Joel
@Joel:完全是。確切地說,這是SQLite存儲的限制。 (有關Core Data編程指南某處存儲特定限制的章節。) –