Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between before and after in viewDidLoad

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?

like image 874
Mannopson Avatar asked Apr 07 '17 02:04

Mannopson


2 Answers

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

like image 163
Fayez Hellani Avatar answered Oct 29 '22 04:10

Fayez Hellani


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.

like image 35
Rokon Avatar answered Oct 29 '22 02:10

Rokon