How to fetch one to many relationship in core data?

烂漫一生 提交于 2020-01-02 23:04:07

问题


Let say the data entities are: Bookshop <--->> Books

How do I fetch all books belonging to a specific book shop that contain "cloud" in the name for example? The following method feel clunky.

Bookshop *bookshop = (Bookshop *) nsManagedObjectFromOwner;
NSString *searchTerm = @"cloud";

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Books" 
                          inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];

NSPredicate *predicate = [NSPredicate predicateWithFormat:
              @"ANY bookshop.Name == '%@' && Name contain[cd] '%@'", 
              bookshop.Name, searchTerm];
[fetchRequest setPredicate: predicate];

How could I improve this code to handle if there are 2 book shops with very similar names? And maybe also performance improvement for fetching as the UITableView should update as the user type in book name.


回答1:


This might be a matter of personal taste, but I like to define and use fetch request templates. Then you can do something like this:

Bookshop *bookshop = (Bookshop *) nsManagedObjectFromOwner;
NSString *searchTerm = @"cloud";

NSDictionary *substitutionDictionary = [NSDictionary dictionaryWithObjectsAndKeys:bookshop.Name, @"BOOKSHOP_NAME", searchTerm, @"BOOK_NAME", nil];
// BOOKSHOP_NAME and BOOK_NAME are the name of the variables in your fetch request template

NSFetchRequest *fetchRequest = [model fetchRequestFromTemplateWithName:@"YourFetchRequestTemplateName" substitutionVariables:substitutionDictionary];

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Books" 
                      inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];

It isn't less code, but it is less clunky because all of your fetch requests are defined in one place. Or maybe I'm misunderstanding your question?

As far as the second part of your question is concerned, I haven't actually used this myself, but the NSFetchedResultsControllerDelegate provides the methods you need to automatically keep your table of results up-to-date. Try -controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:. Obviously it implies using an NSFetchedResultsController.



来源:https://stackoverflow.com/questions/4010000/how-to-fetch-one-to-many-relationship-in-core-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!