Ich versuche, eine UISearchBar
zu verwenden, um mehrere Eigenschaften eines NSManagedObject
abzufragen Ich habe ein NSManagedObject
genannt Person
, jede Person, die ein name
und socialSecurity
Eigenschaft hat. Im Moment kann mein Code eine Suche (Fetch) für eine dieser Eigenschaften oder die andere durchführen, aber nicht beide gleichzeitig.iOS Coredata NSPredicate an mehreren Eigenschaften abfragen einmal
- (void) performFetch
{
[NSFetchedResultsController deleteCacheWithName:@"Master"];
// Init a fetch request
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"MainObject" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Apply an ascending sort for the color items
//NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Term" ascending:YES selector:nil];
NSSortDescriptor *sortDescriptor;
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"fullName" ascending:YES selector:@selector(caseInsensitiveCompare:)];
NSArray *descriptors = [NSArray arrayWithObject:sortDescriptor];
[fetchRequest setSortDescriptors:descriptors];
// Recover query
NSString *query = self.searchDisplayController.searchBar.text;
//if (query && query.length) fetchRequest.predicate = [NSPredicate predicateWithFormat:@"Term contains[cd] %@", query];
if(searchValue==1)
{
if (query && query.length) fetchRequest.predicate = [NSPredicate predicateWithFormat:@"name contains[cd] %@", query];
}
else {
if (query && query.length) fetchRequest.predicate = [NSPredicate predicateWithFormat:@"socialSecurity contains[cd] %@", query];
}
// Init the fetched results controller
NSError *error;
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"pLLetter" cacheName:nil];
self.fetchedResultsController.delegate = self;
if (![[self fetchedResultsController] performFetch:&error]) NSLog(@"Error: %@", [error localizedDescription]);
[self.tableView reloadData];
}
Ich weiß nicht, wie beide Eigenschaften in dieser Aussage setzen ...
if (query && query.length) fetchRequest.predicate = [NSPredicate predicateWithFormat:@"name contains[cd] %@", query];
Jede Hilfe oder Anregungen wäre sehr dankbar.
if (query && query.length) fetchRequest.predicate = [NSPredikat-PrädikatWithFormat: @ "(fullName enthält [cd]% @) || (socialSecurity enthält [cd]% @) ", Abfrage, Abfrage]; – OscarTheGrouch