0

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

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)

        })
    }
4

1 回答 1

1

你可以这样做:

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

        })
    }
于 2017-07-24T13:46:02.710 回答