ios - 使用 NotificationCenter Observer 处理异步请求

标签 ios swift firebase asynchronous nsnotificationcenter

有人问过与此类似的问题,所以我很抱歉,但没有人能够帮助我。

我正在努力使用完成处理程序将此异步请求的值返回给 Firebase。我从 Firebase 检索的值是一个数组,它确实存在。但是

这是我向 Firebase 发出请求的函数:

class SearchManager {

    var searchResults = [String]()
    var listOfMosaics = [String]()

    // Retrieves company list from Firebase
    func getMosaicTitles(completionHandler: @escaping (_ mosaics: [String]) -> ()) {
        Database.database().reference().child("mosaics").observeSingleEvent(of: .value, with: { (snapshot) in
            guard let allMosaics = snapshot.value as? [String] else {
                print("unable to unwrapp datasnapshot")
                return
            }
            completionHandler(allMosaics)
        })
    }

    // resets search results array
    func resetSearch() {
        searchResults = []
    }

    // takes list of all mosaics and filters based on search text
    func filterAllMosaics(searchText: String) {
        searchResults = listOfMosaics.filter { $0.contains(searchText) }

    }

}

在 AppDelegate 中我这样调用它发布通知:

    let searchManager = SearchManager()

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    makeRootViewLaunchScreen()
    FirebaseApp.configure()
    searchManager.getMosaicTitles { (results) in
        self.searchManager.listOfMosaics = results
        NotificationCenter.default.post(name: NSNotification.Name("mosaicsReturned"), object: nil)
        self.stopDisplayingLaunchScreen()
    }
    // Adds border to bottom of the nav bar
    UINavigationBar.appearance().shadowImage = UIImage.imageWithColor(color: UIColor(red:0.00, green:0.87, blue:0.39, alpha:1.0))
    // Override point for customization after application launch.
    return true
}

func makeRootViewLaunchScreen() {
    let mainStoryboard: UIStoryboard = UIStoryboard(name: "LaunchScreen", bundle: nil)
    let viewController = mainStoryboard.instantiateViewController(withIdentifier: "launchScreen")
    UIApplication.shared.keyWindow?.rootViewController = viewController
}

// reassigns root view after Firebase request complete
func stopDisplayingLaunchScreen() {
    let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let viewController = mainStoryboard.instantiateViewController(withIdentifier: "centralViewController")
    UIApplication.shared.keyWindow?.rootViewController = viewController
}

在支持使用检索到的数组填充它的 tableView 的 viewController 的 viewDidLoad 中,我添加了一个 Notification Observer。

    var listOfMosaics = [String]()
var searchResults = [String]() {
    didSet {
        tableView.reloadData()
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    listOfMosaics = searchManager.listOfMosaics
    configureSearchBar()
    configureSearchBarTextField()
    self.tableView.separatorColor = UIColor(red:0.00, green:0.87, blue:0.39, alpha:1.0)

    NotificationCenter.default.addObserver(self, selector: #selector(updateListOfMosaics), name: NSNotification.Name("mosaicsReturned"), object: nil)
}

@objc func updateListOfMosaics(notification: Notification) {
    listOfMosaics = searchManager.listOfMosaics
}

但是当我调用下面的代码时,它不起作用,数组打印为空,因此它不会更新我的 tableView。

extension SearchResultsTableViewController: UISearchBarDelegate {

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
    searchManager.resetSearch()
    searchManager.filterAllMosaics(searchText: searchBar.text!)
    tableView.reloadData()
    print(listOfMosaics)
    print(searchResults)


   }
 }

在此先感谢您的帮助。

最佳答案

这现在应该对你有用了。我认为您没有将 SearchManager 的实例从您的 AppDelegate 传递到您的 ViewController。我猜你在 ViewController 中创建了一个新的 SearchManager 实例,它有一个空数组。

搜索管理器:

class SearchManager {

    var searchResults = [String]()
    var listOfMosaics = [String]()

    func getMosaicTitles(completionHandler: @escaping (_ mosaics: [String]) -> ()) {
        Database.database().reference().child("mosaics").observeSingleEvent(of: .value, with: { (snapshot) in
            guard let allMosaics = snapshot.value as? [String] else {
                print("unable to unwrapp datasnapshot")
                completionHandler([]) // <- You should include this too.
                return
            }
            completionHandler(allMosaics)
        })
    }

    func resetSearch() {
        searchResults = []
    }

    func filterAllMosaics(searchText: String) {
        searchResults = listOfMosaics.filter { $0.contains(searchText) }
    }
}

View Controller :

class TableViewController: UITableViewController {

    var searchManager: SearchManager?
    var listOfMosaics = [String]()
    var searchResults = [String]() {
        didSet {
            tableView.reloadData()
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        guard let searchManager = searchManager else { return }
        listOfMosaics = searchManager.listOfMosaics
        print("List of mosaics: \(listOfMosaics)")
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 0
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
}

AppDelegate:

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    let searchManager = SearchManager()

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?)  -> Bool {
        makeRootViewLaunchScreen()
        FirebaseApp.configure()
        searchManager.getMosaicTitles { results in
            self.searchManager.listOfMosaics = results
            self.stopDisplayingLaunchScreen()
        }
        return true
    }

    func makeRootViewLaunchScreen() {
        let mainStoryboard: UIStoryboard = UIStoryboard(name: "LaunchScreen", bundle: nil)
        let viewController = mainStoryboard.instantiateViewController(withIdentifier: "launchScreen")
        window?.rootViewController = viewController
        window?.makeKeyAndVisible()
    }

    func stopDisplayingLaunchScreen() {
        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        guard let viewController = mainStoryboard.instantiateViewController(withIdentifier: "centralViewController") as? TableViewController else { return }
        let navigationController = UINavigationController(rootViewController: viewController)
        viewController.searchManager = searchManager
        window?.rootViewController = navigationController
        window?.makeKeyAndVisible()
    }
}

关于ios - 使用 NotificationCenter Observer 处理异步请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46775381/

相关文章:

ios - Swift 翻译 UIView 坐标系中的坐标

ios - swiftui 对成员 'navigationBarTitle' 的引用不明确

ios - Swift 在 modalPresentationStyle 中隐藏 UIView

android - Firebase 存储的上传/下载速度非常慢

ios - 保持 UIScrollView iOS 的状态

ios - 在 Swift 中动态更改 SCNView 的背景

ios - 需要澄清值类型方法

swift - 在不影响老用户的情况下更新代码流

ios - 使用 Swift 的 Firebase 3.0 iOS Codelab 聊天应用程序教程

android - 向 Firebase DB 添加新字段时的向后兼容性