gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:10:41 24 4
gpt4 key购买 nike

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

我正在努力使用完成处理程序将此异步请求的值返回给 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com