Difference between before and after in viewDidLoad
Asked Answered
S

2

12

I have tried to set the searchBar as a tableHeaderView inside of the viewDidLoad:

 override func viewDidLoad() {
    super.viewDidLoad()


    // SearchController initializiation
    self.searchController = UISearchController.init(searchResultsController: nil)
    self.searchController.delegate = self
    self.searchController.searchBar.delegate = self
    self.searchController.searchBar.sizeToFit()
    self.searchController.searchResultsUpdater = self
    self.searchController.searchBar.barTintColor = UIColor.white
    self.searchController.searchBar.keyboardAppearance = .default
    self.searchController.searchBar.backgroundColor = UIColor.white
    self.searchController.hidesNavigationBarDuringPresentation = true
    self.searchController.obscuresBackgroundDuringPresentation = false


    self.tableView.tableHeaderView = self.searchController.searchBar
    self.definesPresentationContext = true

    self.fetch()
    self.tableView.reloadData()

}

And this my fetch() function:

func fetch() {
    let fetchRequest:NSFetchRequest<Phone> = Phone.fetchRequest()
    fetchRequest.sortDescriptors = [NSSortDescriptor.init(key: "header", ascending: true), NSSortDescriptor.init(key: "date", ascending: true)]

    self.fetchedResultsController = NSFetchedResultsController.init(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext, sectionNameKeyPath: "header", cacheName: nil)
    self.fetchedResultsController.delegate = self

    do {
        try self.fetchedResultsController.performFetch()
        self.tableView.reloadData()
    } catch {}

}

But I don't understand they are doesn't work. The Xcode crashes and nothing actually happens. And then I have tried to change something inside of viewDidLoad method:

override func viewDidLoad() {
    super.viewDidLoad()

    self.fetch()
    self.tableView.reloadData()

    // SearchController initializiation
    self.searchController = UISearchController.init(searchResultsController: nil)
    self.searchController.delegate = self
    self.searchController.searchBar.delegate = self
    self.searchController.searchBar.sizeToFit()
    self.searchController.searchResultsUpdater = self
    self.searchController.searchBar.barTintColor = UIColor.white
    self.searchController.searchBar.keyboardAppearance = .default
    self.searchController.searchBar.backgroundColor = UIColor.white
    self.searchController.hidesNavigationBarDuringPresentation = true
    self.searchController.obscuresBackgroundDuringPresentation = false


    self.tableView.tableHeaderView = self.searchController.searchBar
    self.definesPresentationContext = true



}

Success! Works properly. I don't understand what is the difference?

Swollen answered 7/4, 2017 at 2:38 Comment(6)
What's the error message when it crashes? What is the exact line that it crashes on? Please share the stack trace.Criminology
Nothing! In the console only (lldb)Swollen
Is there any info in the console? There's not enough info in your question to solve your issue.Criminology
Do you do anything in the delegate methods of the UISearchController that assume that the fetchedResultsController is not nil?Efta
@JonRose Did you mean the UISearchResultsUpdating protocol?Swollen
yes. I meant both UISearchResultsUpdating and UISearchBarDelegate. The viewController is being set as delegate for two different objects (searchBar.delegate and searchResultsUpdater) before the fetchedResultsController is setup. So it can get methods called by them immediately. If those methods assume that there is a fetchedResultsController setup it could cause a logical error.Efta
N
2

You should try removing the self.tableView.reloadData() from the viewDidLoad() method, because the data wasn't even loaded to be reloaded.

Nausea answered 10/7, 2017 at 11:39 Comment(0)
I
1

It is because you set the delegates for searchResultsUpdater and searchBar before even your fetchedResultsController is initialised. Some where in your code searchResultsUpdater or searchBar's delegate methods are accessing your fetchedResultsController and getting it nil so you app crashes. My suggestion is to avoid the crash is to make searchResultsController as computed or lazy property.

Insecure answered 23/4, 2017 at 10:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.