1
gaitana 2015-09-29 13:24:58 +08:00
然后呢
|
3
gaitana 2015-09-29 13:29:36 +08:00
@dingsoung 可能他不认同 swift ,也可能是他 OC 学的太精通了,不忍心看到被后来者 Swift 替换掉。其实我也怀疑 Swift 的执行效率,虽然苹果声称某些方面比 OC 还快,但商业公司的话不能听信,谷歌每次新版本也是每次比老版本快 N 倍。
|
4
DingSoung OP @gaitana 贴代码 有点长
```C // // QiniuManager.swift // summer // // Created by Alex D. on 15/8/12. // import Qiniu class QiniuManager { static let instance = QiniuManager() var upManager:QNUploadManager? private init() { upManager = QNUploadManager() } //MARK: 上传文件 func upload(token:String, data:NSData, key:String, process:((key:String, percent:Float)->Void)?, complete:((info:QNResponseInfo, key:String, resp:NSDictionary?)->Void)?, cancel:(()->Bool)?) { let opt = QNUploadOption(mime: "text/plain", progressHandler: { (key, percent) -> Void in print(key) process?(key: key, percent: percent) }, params: ["x:foo" : "fooval"], checkCrc: true) { () -> Bool in //取消 if let block = cancel?() { return block }else { return false } } upManager?.putData(data, key: key, token: token, complete: { (info, key, resp) -> Void in complete?(info: info, key: key, resp: resp) }, option: opt) } //MARK: 断点续传 func upload(folder:String, token:String, data:NSData, key:String, process:((key:String, percent:Float)->Void)?, complete:((info:QNResponseInfo, key:String, resp:NSDictionary?)->Void)?, cancel:(()->Bool)?) { do { //写标记 self.upManager = try QNUploadManager(recorder: QNFileRecorder(folder: folder)) } catch { self.upManager = QNUploadManager() print("启动断点续传失败\(error),自动切换到连续上传") } self.upload(token, data: data, key: key, process: process, complete: { (info, key, resp) -> Void in self.upManager = QNUploadManager() complete?(info: info, key: key, resp: resp) }) { () -> Bool in cancel?() if cancel?() == true { self.upManager = QNUploadManager() return true } return false } } } ``` ```C // // QiniuManager.m // GiftBox // // Created by __ on 15/9/29. // #define kGetQiniuTokenURL xxxxx #import "QiniuManager.h" @interface QiniuManager() @property (nonatomic, strong) QNUploadManager * manager; @property (nonatomic, copy) NSString * token; @property (nonatomic, assign) NSTimeInterval lastTime; @end @implementation QiniuManager + (QiniuManager *)sharedInstance { static QiniuManager * manager; static dispatch_once_t pred; dispatch_once(&pred, ^{ manager = [[QiniuManager alloc] init]; manager.manager = [[QNUploadManager alloc] init]; }); return manager; } - (void)uploadData:(NSData *)data key:(NSString *)key success:(void(^)(NSString * key))success fail:(void (^)(NSError *))fail { if (self.needRefreshToken) { [self refreshTokenWithSuccess:^{ [self uploadData:data key:key success:success fail:fail]; } fail:^(NSError *error) { if (fail) { fail(error); } }]; } else { [self.manager putData:data key:key token:self.token complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) { if (info.isOK && resp != nil) { if (success) { success(key); } } else { if (fail) { fail(info.error); } } } option:nil]; } } - (void)refreshTokenWithSuccess:(void(^)())success fail:(void(^)(NSError * error))fail { [NetworkManager addGetRequestWithURLString:[NSString stringWithFormat:kGetQiniuTokenURL, IS_DEBUG ? 4 : 2] tag:100 contentType:GBContentType success:^(NetworkManager *network, id responseObject) { NSString * status = [responseObject objectForKey:@"status"]; if (status.integerValue == 0) { NSString * qiniuToken = [responseObject objectForKey:@"token"]; self.token = qiniuToken; self.lastTime = [NSDate date].timeIntervalSince1970; if (success) { success(); } } else { if (fail) { fail([NSError errorWithDomain:[responseObject objectForKey:@"err_msg"] code:[[responseObject objectForKey:@"code"] integerValue] userInfo:nil]); } } } fail:^(NetworkManager *network, NSError *error) { if (fail) { fail(error); } }]; } //十分钟 - (BOOL)needRefreshToken { NSTimeInterval nowTime = [NSDate date].timeIntervalSince1970; return nowTime - self.lastTime > 60 * 10; } @end ``` |
5
DingSoung OP V2EX 不是支持 MarkDown 么
|
6
itchina110 2015-09-29 14:13:00 +08:00
然后 2016 年 1 月 10 日 将在北京举办 中国首届 Swift 大会 : http://atswift.io
|
8
XadillaX 2015-09-29 14:21:22 +08:00
@dingsoung 你可以贴 gist 或者 paste.ubuntu.com 。
|
9
ibcker 2015-09-29 14:24:42 +08:00
打一架
|
10
dorentus 2015-09-29 14:28:24 +08:00
下面的那个版本似乎多了对 refresh token 的检查和支持,估计是他想加这功能,然后不懂 Swift 吧……
然后你的版本里面,第 41 行附近, cancel?() 了两次,应该是不对的吧 |
12
zapper 2015-09-29 15:12:16 +08:00 2
可是你为什么不直接去问他呢...
|
13
Felldeadbird 2015-09-29 16:54:09 +08:00
@zapper 我也觉得。他这样做肯定有原因,先了解好
|