我正在开发一个项目,该项目应使用GCD
同时从不同的服务器URL获取数据。它通过从服务器获取数据并更新视图来按预期工作。
连接控制器:
private func initiateObjects() {
self.stationCounter = 1
self.request = StationRequests()
self.object = StationObject()
}
private func initiateRequests() {
request = StationRequests(self.stationCounter)
request.object = self.object
request.delegate = self
}
private func sendRequest() {
queueStation.async {
self.request.SendRequest()
}
}
private func updateView(_ obj: BaseObject?) {
DispatchQueue.main.async {
NotificationCenter.default.post(name: .dataFetched, object: nil, userInfo: ["item": obj ?? BaseObject()])
}
}
func managerDidReceived(data: Any, request: BaseRequest) {
let parsedObj = request.object?.Parse(data)
self.updateView(parsedObj)
}
查看控制器:
override func viewDidLoad() {
super.viewDidLoad()
self.initiateStationListService()
}
private func initiateStationListService() {
self.connection.initiate()
NotificationCenter.default.addObserver(self, selector: #selector(self.dataFetched(_:)), name: .dataFetched, object: nil)
}
@objc func dataFetched(_ notification: Notification) {
guard let items = notification.userInfo as? [String:StationObject] else { return }
guard let item = items["item"] else { return }
self.stationList.append(item) //HERE IS THE PROBLEM
self.collectionView?.reloadData()
}
问题是当我想将获取的对象追加到我的列表中以在CollectionView
中显示时,列表项全部更改为最新的。我的意思是,所有以前添加的项目都与最新项目相同且相同。所以,当我重新加载CollectionView
时,所有记录都是相同的。加载的项目本身是正确的,没有问题。
答案 0 :(得分:0)
尝试以下:
self.stationList += [item]
self.collectionView?.reloadData()