Skip to content

Commit a55c374

Browse files
committed
Fix bug: Get user data in main queue to make sure user array is not empty when layout cell
1 parent b79f96d commit a55c374

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

Example/Source/MessageViewController.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,17 @@ class MessageViewController: ChatViewController {
2222
setupData()
2323
bindViewModel()
2424

25+
// Get user data firstly
26+
DispatchQueue.main.async { [weak self] in
27+
self?.viewModel.getUserData()
28+
}
2529
viewModel.firstLoadData { [weak self] in
2630
DispatchQueue.main.async {
2731
self?.updateUI()
2832
}
2933
}
3034
}
31-
35+
3236
override func numberOfSections(in tableView: UITableView) -> Int {
3337
return 1
3438
}

Example/Source/MessageViewModel.swift

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,19 @@ class MessageViewModel {
6060
}
6161
}
6262

63-
fileprivate func getUserData() {
64-
DispatchQueue.global(qos: .background).async { [weak self] in
63+
func getUserData() {
64+
DispatchQueue.global(qos: .default).async { [weak self] in
6565
self?.getDataFromFile(fileName: "user") { (userResponse: [User], _) in
6666
self?.users = userResponse
6767
}
6868
}
6969
}
7070

71-
fileprivate func getDataFromFile<T>(fileName: String, completion: (_ data: [T], _ pagination: Pagination) -> ()) where T: Mappable {
72-
guard let jsonData = Data.dataFromJSONFile(fileName) else { return }
71+
private func getDataFromFile<T>(fileName: String, completion: (_ data: [T], _ pagination: Pagination?) -> ()) where T: Mappable {
72+
guard let jsonData = Data.dataFromJSONFile(fileName) else {
73+
completion([], nil)
74+
return
75+
}
7376

7477
do {
7578
let jsonObj = try JSON(data: jsonData)
@@ -79,9 +82,10 @@ class MessageViewModel {
7982
return
8083
}
8184

82-
completion(listReponse.data, listReponse.pagination!)
85+
completion(listReponse.data, listReponse.pagination)
8386
} catch {
8487
print("Error \(error)")
88+
completion([], nil)
8589
return
8690
}
8791
}
@@ -132,7 +136,7 @@ extension MessageViewModel {
132136
return .single
133137
}
134138

135-
fileprivate func handleDataSource(messages: [Message]) -> [Message] {
139+
private func handleDataSource(messages: [Message]) -> [Message] {
136140
let modifiedArray = messages.map { msg -> Message in
137141
var m = msg
138142
m.isOutgoing = self.isOutgoingMessage(m)
@@ -142,15 +146,15 @@ extension MessageViewModel {
142146
return modifiedArray
143147
}
144148

145-
fileprivate func isOutgoingMessage(_ message: Message) -> Bool {
149+
private func isOutgoingMessage(_ message: Message) -> Bool {
146150
guard let user = currentUser else {
147151
return false
148152
}
149153

150154
return message.sendByID == user.id
151155
}
152156

153-
fileprivate func getIndexPathWillAdds(newDataSize: Int) -> [IndexPath] {
157+
private func getIndexPathWillAdds(newDataSize: Int) -> [IndexPath] {
154158
let currentNumberMessage = messages.count
155159
let newNumberMessage = messages.count + newDataSize
156160
var indexPathWillAdds: [IndexPath] = []

0 commit comments

Comments
 (0)