Skip to content

Commit 8c4d7eb

Browse files
committed
Update the promises in NetworkAPI to dispatch to another thread to illustrate best practices.
Update the readme or version 6.
1 parent a5d1ba4 commit 8c4d7eb

File tree

2 files changed

+170
-60
lines changed

2 files changed

+170
-60
lines changed

FranticApparatusDemo/FranticApparatusDemo/NetworkAPI.swift

+19-15
Original file line numberDiff line numberDiff line change
@@ -57,29 +57,33 @@ public final class NetworkAPI {
5757

5858
fileprivate func parseJSONData(_ data: Data) -> Promise<NSDictionary> {
5959
return Promise<NSDictionary> { (fulfill, reject, isCancelled) in
60-
do {
61-
let object = try JSONSerialization.jsonObject(with: data, options: [])
62-
63-
if let dictionary = object as? NSDictionary {
64-
fulfill(dictionary)
60+
dispatcher.dispatch {
61+
do {
62+
let object = try JSONSerialization.jsonObject(with: data, options: [])
63+
64+
if let dictionary = object as? NSDictionary {
65+
fulfill(dictionary)
66+
}
67+
else {
68+
reject(NetworkError.unexpectedData(data))
69+
}
6570
}
66-
else {
67-
reject(NetworkError.unexpectedData(data))
71+
catch {
72+
reject(error)
6873
}
6974
}
70-
catch {
71-
reject(error)
72-
}
7375
}
7476
}
7577

7678
fileprivate func parseImageData(_ data: Data) -> Promise<UIImage> {
7779
return Promise<UIImage> { (fulfill, reject, isCancelled) in
78-
if let image = UIImage(data: data) {
79-
fulfill(image)
80-
}
81-
else {
82-
reject(NetworkError.unexpectedData(data))
80+
dispatcher.dispatch {
81+
if let image = UIImage(data: data) {
82+
fulfill(image)
83+
}
84+
else {
85+
reject(NetworkError.unexpectedData(data))
86+
}
8387
}
8488
}
8589
}

0 commit comments

Comments
 (0)