使用NotificationCenter Observer处理异步请求 [英] Using NotificationCenter Observer to Handle Asynchronous Requests

查看:185
本文介绍了使用NotificationCenter Observer处理异步请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似的问题已经被问到了,所以我很抱歉,但没有人能够帮助我。



我努力从这个异步向Firebase请求完成处理程序。我从Firebase中检索的值是一个数组,它确实存在。但是

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

  class SearchManager {

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

从Firebase获取公司列表
func getMosaicTitles(completionHandler:@escaping(_ mosaics:[String]) - >()){
Database.database()。reference()。child(mosaics)。observeSingleEvent(.value ,其中:{(snapshot)in
guard let allMosaics = snapshot.value as?[String] else {
print(无法解开datasnapshot)
return
}
completionHandler(allMosaics)
})
}

//重置搜索结果数组
func resetSearch(){
searchResults = []
}

//根据搜索文本获取所有马赛克和过滤器列表
func filterAllMosaics(searchText:String){
sea rchResults = listOfMosaics.filter {$ 0.contains(searchText)}

}

}

在AppDelegate中,我把它称为发布通知:
$ b $ pre $ let searchManager = SearchManager()

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

makeRootViewLaunchScreen()
FirebaseApp.configure()
searchManager.getMosaicTitles {$ results $ b $ self.searchManager.listOfMosaics = results
NotificationCenter .default.post(name:NSNotification.Name(mosaicsReturned),object:nil)
self.stopDisplayingLaunchScreen()
}
//将边框添加到导航栏底部
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

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

//在Firebase请求完成后重新分配根视图
func stopDisplayingLaunchScreen(){
let mainStoryboard:UIStoryboard = UIStoryboard(name:Main,bundle:nil)
let viewController = mainStoryboard.instantiateViewController(withIdentifier:centralViewController)
UIApplication .shared.keyWindow?.rootViewController = viewController
}

在viewController的viewDidLoad支持tableView,它使用检索的数组来填充它,我添加一个通知观察者。

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

覆盖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。

 扩展程序SearchResultsTableViewController:UISearchBarDelegate {

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






$ p

感谢您的帮忙。

解决方案

现在,我想你没有把你的AppDelegate的SearchManager实例传递给你的ViewController。我猜你在ViewController中创建了一个新的SearchManager实例,它有一个空的数组。



搜索管理器



pre $ $ $ $ $ $ $ $ $ $ $ $ $ )

func getMosaicTitles(completionHandler:@escaping(_ mosaics:[String]) - >()){
Database.database()。reference()。child(mosaics ).observeSingleEvent(:.value,其中:{(snapshot)in
guard let allMosaics = snapshot.value as?[String] else {
print(无法解开datasnapshot)
completionHandler([])//< - 你也应该包括这个。
return
}
completionHandler(allMosaics)
})
}

func resetSearch(){
searchResults = []
}

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

视图控制器:

  class TableViewController:UITableViewController {

var searchManager:SearchManager?
var listOfMosaics = [String]()
var searchResults = [String](){
didSet {
tableView.reloadData()
}
}
$ b $重写func viewDidLoad(){
super.viewDidLoad()
guard让searchManager = searchManager else {return}
listOfMosaics = searchManager.listOfMosaics
print (马赛克列表:\(listOfMosaics))
}

覆盖func numberOfSections(在tableView:UITableView中) - > Int {
return 0
}

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

AppDelegate:

  class AppDelegate:UIResponder,UIApplicationDelegate {

var window:UIWindow?
让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

$ b func makeRootViewLaunchScreen(){
let mainStoryboard:UIStoryboard = UIStoryboard(name:LaunchScreen,bundle:nil)
让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()
}
}


Similar questions to this have been asked so I apologize, but none of them have been able to help me.

I am struggling to return the value from this asynchronous request to Firebase with a completion handler. The value I am retrieving from Firebase is an array and it does exist. But

Here is my function for making the request to 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) }

    }

}

And in the AppDelegate I call it like this posting a Notification:

    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
}

In the viewDidLoad of the viewController that supports the tableView that uses the retrieved array to populate it I add a 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
}

But when I call the below code it doesn't work the arrays print as empty and as a result it doesn't update my tableView.

extension SearchResultsTableViewController: UISearchBarDelegate {

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


   }
 }

Thank you in advanced for the help.

解决方案

This should work for you now. I think you didn't pass the instance of SearchManager from your AppDelegate to your ViewController. I'm guessing you created a new instance of SearchManager in your ViewController, which has an empty array.

Search Manager:

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()
    }
}

这篇关于使用NotificationCenter Observer处理异步请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆