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

Swift 使用带完成处理程序的函数时未初始化变量,swift,scope,completionhandler,sendbird,Swift,Scope,Completionhandler,Sendbird,我正在尝试使用SendBird聊天应用程序中可用的聊天频道数初始化可变频道。我在这个过程中使用了一个名为:private func loadChannels的函数,以便将通道加载到上述变量中。我不明白的是,调用函数时会加载通道,并且可以按照下面的代码显示。但是,当我想在LoadChannel之外显示相同变量通道的内容时,我得到一个空变量。有什么问题吗 import UIKit import SendBirdSDK import JSQMessagesViewController class

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

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()

        })
    }

既然您正在一个完成处理程序中填充channels属性,我猜这个处理程序还没有被异步调用?当你到达指纹处。。。调用LoadChannel之后的语句。在这种情况下,我应该确切地将其调用到哪里?没有问题。删除self.loadChannels之后的打印行,因为正如dfri已经提到的那样,该方法是异步工作的,并且这个特定的打印行没有意义。如果我想在函数外部使用“channels”变量,我该如何做?将使用“channels”变量的代码也放入完成块中。