こんにちは私はコアデータとNsFetchedResultsControllerを使用して、私は3つのエンティティ内の属性を検索するためにsearchBarを使用してこのアプリケーションがあります。SwiftのUISearchBarとNSFetchedResultsControllerを使用してコアデータに一度に複数のエンティティをフェッチする方法は?
問題は、それぞれの属性が自然に異なることです。しかし私は、ユーザーからの同じsearchBar入力を使って、3つのエンティティすべてを一度に検索したいと考えています。
すべての3つのエンティティには属性「name」があり、「name」属性に含まれるsearchBar文字列を持つすべてのエンティティを検索することが考えられます。
私はこのコードを使用していますので、これまでのところ、私は唯一の1エンティティを検索することができます。
だから、// MARK: - Fetched results controller
var fetchedResultsController: NSFetchedResultsController {
if _fetchedResultsController != nil {
return _fetchedResultsController!
}
let fetchRequest = NSFetchRequest()
// Edit the entity name as appropriate.
let entity = NSEntityDescription.entityForName("Category", inManagedObjectContext: moc)
fetchRequest.entity = entity
// Set the batch size to a suitable number.
fetchRequest.fetchBatchSize = 20
// Edit the sort key as appropriate.
let sortDescriptor = NSSortDescriptor(key: "index", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: moc, sectionNameKeyPath: nil, cacheName: nil)
aFetchedResultsController.delegate = self
_fetchedResultsController = aFetchedResultsController
do {
try _fetchedResultsController!.performFetch()
} catch {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
//print("Unresolved error \(error), \(error.userInfo)")
abort()
}
return _fetchedResultsController!
}
// MARK: - Search
var searchController: UISearchController? = nil
func reloadFRC(predicate: NSPredicate?) {
fetchedResultsController.fetchRequest.predicate = predicate
performFecth()
}
//Delegate: UIsearchController
func updateSearchResultsForSearchController(searchController: UISearchController) {
if let searchBarText = searchController.searchBar.text {
var predicate: NSPredicate?
if searchBarText != "" {
predicate = NSPredicate(format: "name contains[cd] %@", searchBarText)
}
self.reloadFRC(predicate)
}
}
//Delegate: UISearchBar
func searchBarTextDidEndEditing(searchBar: UISearchBar) {
self.reloadFRC(nil)
}
//Configure Search
func configureSearch() {
self.searchController = UISearchController(searchResultsController: nil)
if let _searchController = self.searchController {
_searchController.delegate = self
_searchController.searchResultsUpdater = self
_searchController.dimsBackgroundDuringPresentation = false
_searchController.searchBar.delegate = self
_searchController.searchBar.sizeToFit()
_searchController.searchBar.barTintColor = Utils.colorFromHex(0x2FC5DA)
_searchController.searchBar.tintColor = Utils.colorFromHex(0x585858)
self.tableView.tableHeaderView = _searchController.searchBar
} else {print("ERROR configuring _searchController in %@", #function)}
}
、私はそれをどのように行うことができますか?
したがって、3つのエンティティすべてから「名前」属性を削除し、親エンティティに配置する必要がありますか?検索が親エンティティからオブジェクト名にアクセスする方法はわかりません。 –
属性が検索に転送された場合、どのようにオブジェクトを各エンティティに追加し続けることができますか?私が言ったように、各エンティティは「名前」属性を持ち、ユーザは各エンティティ内にオブジェクトを作成する必要があるためです。したがって、3つのエンティティから「名前」を削除して親エンティティに入れると、3つのエンティティの追加作業はどのようになりますか?私のポイントを理解していますか? –
子エンティティは、親エンティティからすべての属性を継承します。そのため、親エンティティに対して定義された属性( 'name')も、子エンティティに対して定義されます。 これは、https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/KeyConcepts.html – bsmith11