本來打算試著利用 Object-C 的 GCD 特性, 進行資料下載後的 callback 回傳機制 ...
結果總是會有些意外的情況發生, GCD 的結果判定比預期中來得早,
簡單的說就是 使用 dispatch_group_async 指令包裝起來的執行內容, 裡頭其實不能又包含了一個 block ...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- ( void ) _PreLoadProcedure | |
{ | |
__block dispatch_group_t group; | |
dispatch_queue_t queue; | |
group = dispatch_group_create(); | |
dispatch_group_notify( group, dispatch_get_main_queue(), ^ | |
{ | |
NSLog( @"test download finish" ); | |
}); | |
queue = dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ); | |
dispatch_group_async( group, queue, ^() | |
{ | |
NSLog( @"Run one!" ); | |
[TDDownloadManager simpleDownload: @"https://docs.google.com/uc?authuser=0&id=0B1yHM9LysIXXdXV4TWVVdkJORkU&export=download" forDirectory: NSCachesDirectory completed: ^(NSError * error, BOOL finished) | |
{ | |
NSLog( @"one: %@, %d", error, finished ); | |
}]; | |
}); | |
dispatch_group_async( group, queue, ^() | |
{ | |
NSLog( @"Run Two!" ); | |
[TDDownloadManager simpleDownload: @"https://docs.google.com/uc?authuser=0&id=0B1yHM9LysIXXMnJWUzhvS3ZuN1k&export=download" forDirectory: NSCachesDirectory completed: ^(NSError * error, BOOL finished) | |
{ | |
NSLog( @"two: %@, %d", error, finished ); | |
}]; | |
}); | |
} |
結果卻長這樣
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2015-04-01 00:03:52.037 DemoDownloadManager[4298:174332] Run Two! | |
2015-04-01 00:03:52.037 DemoDownloadManager[4298:174330] Run one! | |
2015-04-01 00:03:52.037 DemoDownloadManager[4298:174216] test download finish | |
2015-04-01 00:03:53.173 DemoDownloadManager[4298:174216] finish download : file:/// ... /Caches/StickerLibrarySystemUpdate.txt | |
2015-04-01 00:03:53.173 DemoDownloadManager[4298:174216] two: (null), 1 | |
2015-04-01 00:03:53.275 DemoDownloadManager[4298:174216] finish download : file:/// ... /Caches/StickerLibraryTabUpdate.zip | |
2015-04-01 00:03:53.275 DemoDownloadManager[4298:174216] one: (null), 1 |
所以目前的結論是, 想要兩個或以上的執行緒都跑完之後再把最後的資訊透過 callback block 機制進行回傳的方式, 得找其他方案來試。
沒有留言:
張貼留言