使用带有完成处理程序的函数时变量未初始化

Variable not getting initialised when using a function with completion handler

我正在尝试使用我的 SendBird 聊天应用程序中可用的聊天频道数来初始化变量 channels。我为此过程使用了一个名为:private func loadChannels() 的函数,以便将通道加载到上述变量中​​。我不明白的是,调用函数时会加载频道,并且可以显示,如下面的代码所示。但是,当我想在 loadChannels() 之外显示同一变量 channels 的内容时,我得到一个空变量。可能是什么问题?

import UIKit
import SendBirdSDK
import JSQMessagesViewController


class ViewController: UIViewController {


    var messages = [JSQMessage]()

    var channels =  [SBDOpenChannel]()
    private var refreshControl: UIRefreshControl?
    private var openChannelListQuery: SBDOpenChannelListQuery?



    override func viewDidLoad() {


        //connecting to the application
        SBDMain.initWithApplicationId("1662A8E8-F45F-454B-9E5E-02362342ECC5")

        //Connecting the user
        SBDMain.connect(withUserId: "tahrisqalli", completionHandler: { (user, error) in
            // ...
            print("connected tahrisqalli")


                print ("printing channels")
                self.loadChannels()
                print (self.channels)


            print ("printing channels")
            self.loadChannels()
            // Here content of channels variable is empty 
            print (self.channels)

        })



    }

 private func loadChannels() {
        self.openChannelListQuery = SBDOpenChannel.createOpenChannelListQuery()
        self.openChannelListQuery?.limit = 20

        if self.openChannelListQuery?.hasNext == false {
            return
        }

        self.openChannelListQuery?.loadNextPage(completionHandler: { (channels, error) in
            if error != nil {
                print ("error")
                return
            }

            for channel in channels! {

                self.channels.append(channel)

            }
            // Here content of channels is full with the correct channels
            print (self.channels)

        })
    }

你可以这样做:

import UIKit
import SendBirdSDK
import JSQMessagesViewController


class ViewController: UIViewController {


    var messages = [JSQMessage]()

    var channels =  [SBDOpenChannel]()
    private var refreshControl: UIRefreshControl?
    private var openChannelListQuery: SBDOpenChannelListQuery?



    override func viewDidLoad() {


        //connecting to the application
        SBDMain.initWithApplicationId("1662A8E8-F45F-454B-9E5E-02362342ECC5")

        //Connecting the user
        SBDMain.connect(withUserId: "tahrisqalli", completionHandler: { (user, error) in
            // ...
            print("connected tahrisqalli")


                print ("printing channels")
                self.loadChannels(){
                     print (self.channels)
                }



            //print ("printing channels")
            //self.loadChannels()
            // Here content of channels variable is empty 
            //print (self.channels)

        })



    }

 private func loadChannels(callback: @escaping () -> void) {
        self.openChannelListQuery = SBDOpenChannel.createOpenChannelListQuery()
        self.openChannelListQuery?.limit = 20

        if self.openChannelListQuery?.hasNext == false {
            return
        }

        self.openChannelListQuery?.loadNextPage(completionHandler: { (channels, error) in
            if error != nil {
                print ("error")
                return
            }

            for channel in channels! {

                self.channels.append(channel)

            }
            // Here content of channels is full with the correct channels
           // print (self.channels)
            callback()

        })
    }