SOOOO ....我是JS Dev和我建立一個應用程序在夫特3.0爲iOS 10+和我想與很多相同的方式,我在角1.6 +做數據交互...夫特3.0 + iOS的10 +辛格爾頓sharedInstance - 更新數據陣列改變之後的UI(所述角路)
這裏是我的情況:
我有一個Singleton sharedInstance,對於一個會話的WebRTC房子會話數據。 sharedInstance的其中一個屬性包含一個UInt數組,我需要更新一個UI元素(IBOutlet)來向用戶顯示在任何給定時刻他們可以調用的可用對手。
在角我只想更新模型和視圖/ UI將
我期待在雨燕3.0創建相同的行爲所以這裏去自動改變......咚的一聲...完成了...一些代碼:
class Singleton {
static let sharedInstance = Singleton()
var session = (
peers: [UInt]()
)
private init() { }
}
這裏是控制器:
class ViewController: UIViewController {
@IBOutlet weak var UIPeerList: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
UIPeerList.text = String(describing: Singleton.sharedInstance.session.peers)
self.updatePeerList(room: roomName, completion: {(oUser: AnyObject) -> Void in
QBRequest.dialogs(
for: QBResponsePage(limit: 100, skip: 0),
extendedRequest: [roomName : "name"],
successBlock: {(
response: QBResponse,
dialogs: [QBChatDialog]?,
dialogsUsersIDs: Set<NSNumber>?,
page: QBResponsePage?
) -> Void in
print("<------------------------------<<< Successfully found chat dialog - " + roomName)
}, errorBlock: { (response: QBResponse) -> Void in
print("<------------------------------<<< Handle Error finding chat dialog - " + roomName)
let chatDialog: QBChatDialog = QBChatDialog(dialogID: nil, type: QBChatDialogType.group)
//chatDialog.occupantIDs = []
//chatDialog.occupantIDs?.append(NSNumber(value: DataModel.sharedInstance.qbLoginParams.id))
//chatDialog.occupantIDs?.append(12186)
chatDialog.setValue(roomName, forKey: "Name")
QBRequest.createDialog(chatDialog, successBlock: {(response: QBResponse?, createdDialog: QBChatDialog?) in
print("<------------------------------<<< Success creating chat dialog")
print(response ?? "No Response")
print(createdDialog ?? "No Created Dialog")
}, errorBlock: {(response: QBResponse!) in
print("<------------------------------<<< Error creating chat dialog")
print(response ?? "No Response")
})
}
)
})
所以在上面的控制器我設置的UI元素的數據源的viewDidLoad中被觸發之後(我知道這不正是當前回事)現在,每當我在這個異步API請求,我希望UI更新在這裏改變Singleton.sharedInstance.session.peers數據模型,例如像:
func updatePeerList(room: String, completion: @escaping (_ response: AnyObject) ->()) {
QBRequest.users(
withTags: [room],
page: QBGeneralResponsePage(currentPage: 1, perPage: 10),
successBlock: {(response: QBResponse, page: QBGeneralResponsePage?, users: [QBUUser]?) -> Void in
guard users != nil else { return }
print("<------------------------------<<< Success getting users with room tag - "+room)
DataModel.sharedInstance.sessionInfo.peers.removeAll()
for object in users! {
DataModel.sharedInstance.sessionInfo.peers.append(object.id)
}
DispatchQueue.main.async { completion(response) }
}, errorBlock: {(response: QBResponse!) in
print("<------------------------------<<< Error getting users with room tag - "+room)
print(response)
DispatchQueue.main.async { completion(response) }
}
)
}
我該如何實現這樣的壯舉?
注:我相信這是可能的,我看着UISwitch方法但是這並沒有完全工作,我想..這來
在斯威夫特,多線程,數據種族和這樣的,有一個爲「做咚的一聲」類似的行爲,它的「咚的一聲撞車」;)所以,我想說的是,確保在您訪問「共享適當的同步「變量。 – CouchDeveloper
是不是真的大聲笑 –