iOS CoreData NSPredicate一次查询多个属性 [英] iOS CoreData NSPredicate to query multiple properties at once

查看:154
本文介绍了iOS CoreData NSPredicate一次查询多个属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用 UISearchBar 查询 NSManagedObject的多个属性
我有一个 NSManagedObject 调用 Person ,每个人都有名称 code> socialSecurity 属性。现在我的代码可以为这些属性之一或其他属性执行搜索(fetch),但不能同时进行。

   - (void)performFetch 
{
[NSFetchedResultsController deleteCacheWithName:@Master];

//初始化请求
NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription * entity = [NSEntityDescription entityForName:@MainObjectinManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];

//对颜色项应用升序排序
// NSSortDescriptor * sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@Termascending:YES selector:nil];
NSSortDescriptor * sortDescriptor;
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@fullNameascending:YES selector:@selector(caseInsensitiveCompare :)];

NSArray * descriptors = [NSArray arrayWithObject:sortDescriptor];
[fetchRequest setSortDescriptors:descriptors];

//恢复查询
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];
}

//初始化获取的结果控制器
NSError * error;
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@pLLettercacheName:nil];

self.fetchedResultsController.delegate = self;

if(![[self fetchedResultsController] performFetch:& error])NSLog(@Error:%@,[error localizedDescription]);

[self.tableView reloadData];
}

我不知道如何将两个属性都放到这个语句中...

  if(query&& query.length)fetchRequest.predicate = [NSPredicate predicateWithFormat:@name contains [cd] %@,query]; 

任何帮助或想法都将非常感激。

您可以使用通常的布尔操作数(例如AND / OR)在 NSPredicate 中追加多个搜索字词。

这样的东西应该可以做。

  [NSPredicate predicateWithFormat:@name contains [cd]%@ OR ssid contains [cd]%@,query,query]; 

希望有助于:)


I am trying to use a UISearchBar to query multiple properties of a NSManagedObject I have a NSManagedObject called Person, every person has a name and socialSecurity property. Right now my code can perform a search (fetch) for one of those properties or the other, but not both at the same time.

- (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];
}

I don't know how to put both properties into this statement...

if (query && query.length) fetchRequest.predicate = [NSPredicate predicateWithFormat:@"name contains[cd] %@", query];

Any help or ideas would be greatly appreciated.

解决方案

You can append multiple search terms in an NSPredicate using the usual boolean operands such as AND/OR.

Something like this should do the trick.

[NSPredicate predicateWithFormat:@"name contains[cd] %@ OR ssid contains[cd] %@", query, query];

Hope that helps :)

这篇关于iOS CoreData NSPredicate一次查询多个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆