2015年3月31日 星期二

[記錄]GCD 不是每種惡搞的流程都會接受的


本來打算試著利用  Object-C 的 GCD 特性, 進行資料下載後的 callback 回傳機制 ...

結果總是會有些意外的情況發生, GCD 的結果判定比預期中來得早,

簡單的說就是 使用 dispatch_group_async 指令包裝起來的執行內容, 裡頭其實不能又包含了一個 block ...




- ( 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 );
}];
});
}
view raw gistfile1.m hosted with ❤ by GitHub
本來預期 dispatch_group_notify 能在兩個執行緒 dispatch_group_async 處理完成之後( 包含 block ), 再跑出來的, 以結果論來說, 並不是這回事 =..="

結果卻長這樣


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
view raw gistfile1.txt hosted with ❤ by GitHub


所以目前的結論是, 想要兩個或以上的執行緒都跑完之後再把最後的資訊透過 callback block 機制進行回傳的方式, 得找其他方案來試。

沒有留言:

張貼留言