在Swift 3中访问闭包外的数组

时间:2017-02-03 02:50:39

标签: ios swift multithreading firebase closures

我有这个闭包,我用来填充我的数组和字典。但是,当我尝试在函数外部使用它时,它是空的。我知道这个闭包在一个异步线程上工作,所以假设我尝试在它被填充之前访问该变量是正确的吗?结果,我得到一个空数组。这是我的代码。

class HomeCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout, UISearchBarDelegate, UIGestureRecognizerDelegate {

var entries = [String: DiaryEntry]()
var entryIDS =  [String]()

var searchController: UISearchController!

override func viewDidLoad() {
    super.viewDidLoad()

    // Register cell classes
    self.collectionView!.register(DiaryCell.self, forCellWithReuseIdentifier: "homeCell")
    collectionView?.backgroundColor = UIColor.white
    navigationController?.hidesBarsOnSwipe = true

    if let userID = FIRAuth.auth()?.currentUser?.uid {
        FirebaseService.service.getUserEntriesRef(uid: userID).observe(.value, with: { [weak weakSelf = self] (snapshot) in
            let enumerator = snapshot.children
            while let entry = enumerator.nextObject() as? FIRDataSnapshot {
                weakSelf?.entryIDS.append(entry.key)
                weakSelf?.entries[entry.key] = DiaryEntry(snapshot: entry)
            }
            weakSelf?.entryIDS.reverse()
            weakSelf?.collectionView?.reloadData()
        })
        print("Entries: \(entryIDS.count) ")
    }
    // Do any additional setup after loading the view.
}

处理这种多线程执行的最佳方法是什么?

3 个答案:

答案 0 :(得分:0)

使用Dispatch Groups跟踪您何时将所有元素附加到数组中,然后进行notify回调,以便在他们“#{0}”时自动调用它们。全部补充。

class HomeCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout, UISearchBarDelegate, UIGestureRecognizerDelegate {

var entries = [String: DiaryEntry]()
var entryIDS =  [String]()

let dispatchGroup = DispatchGroup()

var searchController: UISearchController!

override func viewDidLoad() {
    super.viewDidLoad()

    // Register cell classes
    self.collectionView!.register(DiaryCell.self, forCellWithReuseIdentifier: "homeCell")
    collectionView?.backgroundColor = UIColor.white
    navigationController?.hidesBarsOnSwipe = true
    self.dispatchGroup.enter()
    if let userID = FIRAuth.auth()?.currentUser?.uid {
        FirebaseService.service.getUserEntriesRef(uid: userID).observe(.value, with: { [weak weakSelf = self] (snapshot) in
            let enumerator = snapshot.children
            while let entry = enumerator.nextObject() as? FIRDataSnapshot {
                weakSelf?.entryIDS.append(entry.key)
                weakSelf?.entries[entry.key] = DiaryEntry(snapshot: entry)
            }
            self.dispatchGroup.leave()
        })
        self.dispatchGroup.notify(queue: DispatchQueue.main, execute: {
             print("Entries: \(entryIDS.count) ")
             weakSelf?.entryIDS.reverse()
             weakSelf?.collectionView?.reloadData()
        })
    }
    // Do any additional setup after loading the view.
}

我还建议您只使用self代替weakSelf?

答案 1 :(得分:0)

我遵循Raywenderlich及其团队的编码标准(针对Swift)。如果我要重新编写你的代码以拥有强大的自我,那就是这样的:

class HomeCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout, UISearchBarDelegate, UIGestureRecognizerDelegate {

    var entries = [String: DiaryEntry]()
    var entryIDS =  [String]()

    var searchController: UISearchController!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Register cell classes
        self.collectionView!.register(DiaryCell.self, forCellWithReuseIdentifier: "homeCell")
        collectionView?.backgroundColor = UIColor.white
        navigationController?.hidesBarsOnSwipe = true

        if let userID = FIRAuth.auth()?.currentUser?.uid {
            FirebaseService.service.getUserEntriesRef(uid: userID).observe(.value, with: {
                [weak self] (snapshot) in

                guard let strongSelf = self else {
                    return
                }

                let enumerator = snapshot.children
                while let entry = enumerator.nextObject() as? FIRDataSnapshot {
                    strongSelf.entryIDS.append(entry.key)
                    strongSelf.entries[entry.key] = DiaryEntry(snapshot: entry)
                }
                strongSelf.entryIDS.reverse()
                strongSelf.collectionView?.reloadData()
            })
            print("Entries: \(entryIDS.count) ")
        }
        // Do any additional setup after loading the view.
}

我希望这有效。在连接到任何API(例如Firebase和Alamofire)时,我也在编写这样的代码。

答案 2 :(得分:0)

我刚刚遇到了你的问题,在寻找同样问题的解决方案时,我终于弄明白了。所以,我会尽力回答它对我们身后的其他人有所帮助。

问题是数组只存在于闭包内。因此,解决方案是在viewDidLoad之外创建一个数组,并在拥有完整数组后使用它设置,然后使用didSet设置entryIDS

var entryIDS =  [String]() {
    didSet {
        //something
     }
}