fetching particular predicate from database
I have 4 CoreData database. Each actually having their own value. But it is too heavy and i would like to reduce it to 1 Database. So each time i want to pull information from the database, i can choose what to pull. I need to use NSPredicate to set the string that i want to pull izzit?
Do i set my NSPredicate like this?
NSString *value = @"Food";
NSString *wildcardedString = [NSString stringWithFormat:@"%@*", value];
[[NSPredicate predicateWithFormat:@"ANY places.type like %@", wildcardedString];
and how do i bind the NSPredicate with the fetch request sequence?
this is my fetchedResultsController
- (NSFetchedResultsController *)fetchedResultsController {
if (fetchedResultsController != nil)
{
return fetchedResultsController;
}
CoreDataMelakaAppDelegate *appDelegate = (CoreDataMelakaAppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"WhereTo" inManagedObjectContext:context]];
NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES] autorelease];
NSArray *sortDescriptors = [[[NSArray alloc] initWithObjects:sortDescriptor, nil] autorelease];
[fetchRequest setSortDescriptors:sortDescriptors];
// NSPredicate *pred = [NSPredicate predicateWithFormat:@"(name = %@)", wher.name];
//[fetchRequest setPredicate:pred];
NSString *value = @"Nasi";
NSString *wildcardedString = [NSString stringWithFormat:@"%@*", value];
[[NSPredicate predicateWithFormat:@"ANY wher.name like %@", wildcardedString];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectConte开发者_如何学Pythonxt:context sectionNameKeyPath:nil cacheName:@"Root"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
[aFetchedResultsController release];
return fetchedResultsController;
}
When you alter the predicate of a fetch request used by a fetched results controller (FRC), you have to create a new fetched results controller.
Your code is fine logically but it only creates a FRC once with a predicate that compiles to:
ANY wher.name like Nasi*
... each time the FRC executes its fetch, it use that exact predicate.
If you want to use a flexible predicate, you will need to create a new FRC every time you change the predicate.
精彩评论