After retrieving data from Firebase, how to send retrieved data from one view controller to another view controller?

可紊 提交于 2019-12-25 09:17:37

问题


This is my table created in Firebase here. I have a search button. The button action will be at first it will fetch data from firebase and then it will send it to another view controller and will show it in a table view. but main problem is that before fetching all data from Firebase

self.performSegueWithIdentifier("SearchResultPage", sender: self )

triggers and my next view controller shows a empty table view. Here was my effort here.

From this post here I think that my code is not placed well.


回答1:


Write this line of code in your dataTransfer method in if block after complete for loop

self.performSegueWithIdentifier("SearchResultPage", sender: self )



回答2:


Try sending the search string as the sender when you are performing the segue, for example:

self.performSegueWithIdentifier("SearchResultPage", sender: "searchString")

Then, in your prepare for segue get the destination view controller and send over the string to the new view controller. something like:

destinationViewController.searchString = sender as! String

When the segue is completed and you have come to the new view controller, you should now have a searchString value that is set, use this value to perform your query and get the relevant data to show in the new table view.

Please note that this is just one solution of many, there are other ways to achieve this.




回答3:


  • The reason why you are not able to send data is because, you are trying to send data even before the data could actually be fetched.
  • Try the following where you are pushing to next view controller only after getting the data

    func dataTransfer() {
    
    let BASE_URL_HotelLocation = "https://**************.firebaseio.com/Niloy"
    
    let ref = FIRDatabase.database().referenceFromURL(BASE_URL_HotelLocation)
    
    ref.queryOrderedByChild("location").queryStartingAtValue("uttara").observeEventType(.Value, withBlock: { snapshot in
    
        if let result = snapshot.children.allObjects as? [FIRDataSnapshot] {
    
            for child in result {
    
                let downloadURL = child.value!["image"] as! String;
    
                self.storage.referenceForURL(downloadURL).dataWithMaxSize(25 * 1024 * 1024, completion: { (data, error) -> Void in
                    let downloadImage = UIImage(data: data!)
    
                    let h = Hotel()
    
    
                    h.name = child.value!["name"] as! String
                    print("Object \(count) : ",h.name)
                    h.deal = child.value!["deal"] as! String
                    h.description = child.value!["description"] as! String
                    h.distance = child.value!["distance"] as! String
                    h.latestBooking = child.value!["latestBooking"] as! String
                    h.location = child.value!["location"] as! String
                    h.image = downloadImage!
    
                    self.HotelObjectArray.append(h)
                })
    
            }   
                let storyboard = UIStoryboard(name:"yourStoryboardName", bundle: nil)
                let vc = storyboard.instantiateViewControllerWithIdentifier("SearchResultPageIdentifier") as! SearchResultPage                           
                self.navigationController.pushViewController(vc, animated: true)
    
        }
        else {
            print("no results")
        }
    
        })
    

    }



来源:https://stackoverflow.com/questions/40457604/after-retrieving-data-from-firebase-how-to-send-retrieved-data-from-one-view-co

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!