2017-08-04 43 views
0

我一直在尋找這個答案的天。每當我嘗試實例化一個名爲「Govind」的新視圖控制器時,我都會遇到SIGARBT錯誤。我得到SIGARBT錯誤的原因是因爲在調用的視圖控制器中我使用了變量; yourVariable,ASIN,VariationImages,在我的數據庫中查找特定節點。當我將它們設置爲等於firebase的值時,yourVariable,ASIN和VariationImages的值不會更改。來自Firebase的值不是零。這裏是我的代碼斯威夫特全球變數不更新

import UIKit 
    import Firebase 
    import FirebaseDatabase 
    var yourVariable = "" 
    var ProductsNumber = 100 
    var ASIN = "" 
    var Weblink = "" 
    var VariationImages = 5 

    class Initial: UIViewController, UICollectionViewDataSource, 
    UICollectionViewDelegate { 
    @IBOutlet weak var FrontPageCollectionView: UICollectionView! 

    var UIFrame = UIScreen.main.bounds 
    var ref: DatabaseReference! 
    var DatabaseHandle = nil as DatabaseHandle! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     ref = Database.database().reference() 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 3 
    } 

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
     self.DatabaseHandle = ref.child("Frontpage").child(String(indexPath.row)).observe(.value, with: { (TheCategory) in 
      yourVariable = TheCategory.childSnapshot(forPath: "Category").value as! String 
      ASIN = TheCategory.childSnapshot(forPath: "ASIN").value as! String 
     self.DatabaseHandle = self.ref.child(TheCategory.childSnapshot(forPath: "Category").value as! String).child(TheCategory.childSnapshot(forPath: "ASIN").value as! String).child("VariationImages").observe(.value, with: { (NumberOfVariationImages) in 
      VariationImages = Int(NumberOfVariationImages.childrenCount) 
      }) 
     }) 
     CallGovind() 
    } 

    func CallGovind() { 
     let storyboard = UIStoryboard(name: "Main", bundle: nil) 
     let controller = storyboard.instantiateViewController(withIdentifier: "Govind") 
     controller.modalPresentationStyle = .popover 
     self.present(controller, animated: true, completion: nil) 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
    { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "FrontpageCell", for: indexPath) as! myCell 
     self.DatabaseHandle = ref.child("Frontpage").child(String(indexPath.row)).child("Thumbnail").observe(.value, with: { (snapshot) in 
      cell.FrontpageImages.sd_setImage(with: URL(string: snapshot.value as! String), placeholderImage: #imageLiteral(resourceName: "Menu"), options: [.continueInBackground, .progressiveDownload]) 
     }) 
     cell.FrontpageImages.contentMode = .scaleAspectFill 
     cell.FrontpageImages.layer.cornerRadius = 5 
     cell.FrontpageImages.clipsToBounds = true 
     return cell 
    } 

    } 
    /* 
    // MARK: - Navigation 

    // In a storyboard-based application, you will often want to do a little preparation before navigation 
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     // Get the new view controller using segue.destinationViewController. 
     // Pass the selected object to the new view controller. 
    } 
    */ 

    } 

//Here's where the data goes into the second view controller 

    import UIKit 
    import Firebase 
    import FirebaseDatabase 


    class Testing: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate { 
    var ref: DatabaseReference! 
    var DatabaseHandle = nil as DatabaseHandle! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    //Populate view 
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
    { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cells", for: indexPath) as! myCell 
     self.DatabaseHandle = ref.child(yourVariable).child(ASIN).child("VariationImages").child(String(indexPath.row)).observe(.value, with: { (snapshot) in 
      cell.myImageViews.sd_setImage(with: URL(string: snapshot.value as! String), placeholderImage: #imageLiteral(resourceName: "Menu"), options: [.continueInBackground, .progressiveDownload]) 


      }) 
     return cell 
} 

無處不在DataBaseHandle是,產生一個錯誤,因爲它是一個空字符串,因爲變量沒有更新

+0

請顯示崩潰發生的代碼。 – Paulw11

+0

我編輯了帖子並添加了您請求的代碼,感謝您回覆@ Paulw11 –

+0

您可以發佈Govind的代碼嗎?由於這是崩潰,你應該發佈它。 – ryantxr

回答

1

最有可能的是越來越執行您的功能CallGovind()調用完成處理程序之前完成。這意味着您的其他視圖控制器在之前被設置爲

... .observe(.value, with: { (TheCategory) in 
    // Code in here will get executed asynchronously 
    // and likely will get called later 
    // Since you set variables in here, and they 
    // are not set by the time you call the other 
    // view controller. 
} 

CallGovind() // gets called BEFORE above code 

一種可能的解決方案是確保完成塊完成後調用另一個視圖控制器。

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    self.DatabaseHandle = ref.child("Frontpage").child(String(indexPath.row)).observe(.value, with: { (TheCategory) in 
     yourVariable = TheCategory.childSnapshot(forPath: "Category").value as! String 
     ASIN = TheCategory.childSnapshot(forPath: "ASIN").value as! String 
     self.DatabaseHandle = self.ref.child(TheCategory.childSnapshot(forPath: "Category").value as! String).child(TheCategory.childSnapshot(forPath: "ASIN").value as! String).child("VariationImages").observe(.value, with: { (NumberOfVariationImages) in 
      VariationImages = Int(NumberOfVariationImages.childrenCount) 
      // Call other controller here so that the variables 
      // are set 
      DispatchQueue.main.async { 
       CallGovind() 
      } 
     }) 
    }) 

} 
+1

謝謝@ryantxr完美的代碼。謝謝你,我有這個問題的日子。非常感謝!!! –

+0

我建議你看看在控制器之間傳遞數據的其他方法。你可以看看[傳奇馬鈴薯](https://github.com/ryantxr/legendary-potato)。 – ryantxr

+0

非常感謝@ryantxr,我從心底深深地感受到了所有的幫助,我第一次使用Stackoverflow,並且展現出很強的社區感。再次感謝你 –

0

AS ryantxr在閉包內聲明callGovind,並且所有類之間共享變量的好方法是singleTon。

按我的知識,你必須創建單獨的類來管理這種類型的變量,如創建單獨的單文件

import Foundation 
/** 
* Created by Jaydeep on 13-Feb-17. 
*/ 

public class Singleton 
{ 
    var yourVariable : String = "" 
    static let shared = Singleton() 
    private init() 
    { 
    } 
} 

使用

Singleton.shared.yourVariable = "xyz" 

你可以在任何地方,並隨時訪問Singleton類對象你會得到最新的更新值。

+0

謝謝你,我從來沒有看過SingleTon類,但因爲我有,似乎它會工作。謝謝你的幫助! @JaydeepVyas –

+0

這個變量沒有更新到「公開課」爲什麼? –