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)
})
}
다음과 같이 할 수 있습니다.
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()
})
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다