Created
May 14, 2014 20:35
-
-
Save mdziadkowiec/e74a044fcd0d2cb2229c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)reloadFetchedResultsController { | |
[NSFetchedResultsController deleteCacheWithName:@"Customers"]; | |
if (_request == nil) { | |
_request = [[NSFetchRequest alloc] initWithEntityName:@"Customer"]; | |
} | |
NSPredicate *predicate = [NSPredicate predicateWithValue:YES]; | |
if (_departament) { | |
NSPredicate *query = [NSPredicate predicateWithFormat:@"departament = %@", _departament]; | |
predicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[ predicate, query ]]; | |
} | |
if (_searchBar.text.length) { | |
NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"lastname contains[cd] %@", _searchBar.text]; | |
NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"firstname contains[cd] %@", _searchBar.text]; | |
NSPredicate *query = [NSCompoundPredicate orPredicateWithSubpredicates:@[pred1,pred2]]; | |
predicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[ predicate, query ]]; | |
} | |
_request.predicate = predicate; | |
NSSortDescriptor *lastnameSort = [NSSortDescriptor sortDescriptorWithKey:_sortByKey | |
ascending:YES]; | |
[_request setSortDescriptors:@[lastnameSort]]; | |
if (_resultsCtl == nil) { | |
_resultsCtl = [[NSFetchedResultsController alloc] initWithFetchRequest:_request | |
managedObjectContext:self.moc | |
sectionNameKeyPath:@"sortLetter" | |
cacheName:@"Customers"]; | |
_resultsCtl.delegate = self; | |
} | |
NSError *error = nil; | |
[_resultsCtl performFetch:&error]; | |
if (error) { | |
NSLog(@"performFetch error = %@", error); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment