forked from htaiwan/KKBoxPractice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTTPBinManagerOperation.swift
More file actions
92 lines (77 loc) · 3.94 KB
/
Copy pathHTTPBinManagerOperation.swift
File metadata and controls
92 lines (77 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
// HTTPBinManagerOperation.swift
// KK_Operation
//
// Created by CHENG-TAI CHENG on 11/5/15.
// Copyright © 2015 CHENG-TAI CHENG. All rights reserved.
//
import UIKit
// 4. HTTPBinManagerOperation 使用 delegate 向外部傳遞自己的狀態。
protocol HTTPBinManagerOperationDelegate {
func HTTPBinManagerOperationDidFail()
func HTTPBinManagerOperationDidCancel()
func HTTPBinManagerOperationDidSuccess(dic1: NSDictionary, dic2: NSDictionary, image: UIImage)
func HTTPBinManagerOperationUpdateProgress(progrss: Float)
}
// 4. 寫一個叫做 HTTPBinManagerOperation 的 NSOperation subclass
class HTTPBinManagerOperation: AsynOperation {
private let manager = ApiManager()
private var dic1: NSDictionary?
private var dic2: NSDictionary?
private var image: UIImage?
var delegate: HTTPBinManagerOperationDelegate?
override func execute() {
// cancal 好像不該這樣寫
sleep(1)
if self.cancelled { return }
// 4.1 對我們之前寫的 SDK 發送 fetchGetResponseWithCallback: 並等候回應。
manager.fetchGetResponse()
.continueWithSuccessBlock { (task) -> AnyObject! in
print("fetchGetResponse 成功了")
self.dic1 = task.result as? NSDictionary
// 4.2 如果前一步成功,先告訴 delegate 我們的執行進度到了 33%
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.delegate?.HTTPBinManagerOperationUpdateProgress(0.33)
})
// 4.3 對我們之前寫的 SDK 發送 postCustomerName:callback: 並等候回應。
return self.manager.postCustomerName("Alex")
}.continueWithSuccessBlock { (task) -> AnyObject! in
print("postCustomerName 成功了")
self.dic2 = task.result as? NSDictionary
// 4.4 如果前一步成功,先告訴 delegate 我們的執行進度到了 66%
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.delegate?.HTTPBinManagerOperationUpdateProgress(0.66)
})
// 4.5 對我們之前寫的 SDK 發送 fetchImageWithCallback: 並等候回應。
return self.manager.fetchImage()
}.continueWithSuccessBlock { (task) -> AnyObject! in
print("fetchImage 成功了")
self.image = task.result as? UIImage
// 4.6 如果前一步成功,先告訴 delegate 我們的執行進度到了 100%,並且告訴 delegate 執行成功,並回傳前面抓取到的兩個 NSDcitionary 與一個 UIImage 物件
self.finish()
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.delegate?.HTTPBinManagerOperationUpdateProgress(1)
self.delegate?.HTTPBinManagerOperationDidSuccess(self.dic1!, dic2: self.dic2!, image: self.image!)
})
return nil
}.continueWithBlock { (task) -> AnyObject! in
if task.error != nil {
print("失敗了")
print("\(task.error)")
// 如果失敗就整個取消作業,並且告訴 delegate 失敗。 delegate method 要在 main thread 當中執行。
self.finish()
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.delegate?.HTTPBinManagerOperationDidFail()
})
}
return nil
}
}
// 5. 這個 operation 要實作 cancel,發送 cancel 時,要立刻讓operation 停止,包括清除所有進行中的連線。
override func cancel() {
self.cancels()
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.delegate?.HTTPBinManagerOperationDidCancel()
})
}
}