Swift全局变量不更新

时间:2017-08-04 02:23:19

标签: ios swift firebase firebase-realtime-database

我好几天都在寻找这个答案。每当我尝试实例化一个名为" Govind"的新视图控制器时。我收到SIGARBT错误。我收到SIGARBT错误的原因是因为在名为I的视图控制器中使用了变量; 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所在的每个地方都会产生错误,因为它是一个空字符串,因为变量没有更新

2 个答案:

答案 0 :(得分: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 :(得分:1)

AS ryantxr在闭包内部声明了callGovind,并且在所有类之间共享变量的好方法是singleTon。

据我所知,你必须创建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类对象,并始终获得上次更新的值。