Search code examples
swiftscopecompletionhandlersendbird

Variable not getting initialised when using a function with completion handler


I am trying to initialise variable channels with the number of chat channels that are available in my SendBird chat application. I use for this process a function called: private func loadChannels() in order to load the channels into that variable mentioned above. What I don't understand is that the channels are loaded when the function is called and can be displayed as you can see in the code below. However, when I want to display the content of the same variable channels outside of loadChannels() I get an empty variable. What can be the problem?

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)

        })
    }

Solution

  • You can do it like this:

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