いつもお世話になっております。OneDriveAPIでの複数ファイルの非同期ダウンロードを制御したいのですが、
上手く行かず困っていて皆様のお知恵をお借りしたい所存です。
latch.awaitをしなければ複数ファイルの非同期ダウンロード自体は出来るのですが、
該当月の全ファイルをダウンロードし終わった時に表示を更新する処理を行いたいので
latchを使い全ファイルをダウンロードし終わったのを監視しようとした所、
ダウンロードをせずにlatch.awaitが先に働いてしまい何も出来ない状態になります。
###1:latchを組み込む前のログ
D/デバッグ: initializeControl が呼ばれました
D/デバッグ: 20170929.txt を削除しました
D/デバッグ: DrawMainActivity.onPostExecute
D/デバッグ: DrawMainActivity.sendBroadcast
D/デバッグ: BroadcastReceiver : mDrawReceiver が呼ばれました
D/デバッグ: DrawMonthlyCalendar : dayTextList.size()=42
D/デバッグ: 20170929.txt ちゃん、見~つけた
D/デバッグ: 20170929.txt(main_data) をダウンロードしています...
D/デバッグ: 20170929.txt をダウンロードしました
ダウンロードより先に描画が走っています
これをなんとかする為にlatchを組み込んだのですが
###2:下記のコードを実機にて実行した時のログ
D/デバッグ: initializeControl が呼ばれました
D/デバッグ: 全ファイルのダウンロード待ちに入ります
ダウンロードを始める前に待ちに入ってしまい、そのまま動かなくなります。
どの様に修正すればlatchで全ファイルのダウンロードを監視出来るかご教授頂けると助かります。
MainActivity.java(抜粋) public void hoge(int offset) { //カレンダーテーブル作成 CalendarInfo cl = new CalendarInfo(currentYear, currentMonth); final CountDownLatch latch = new CountDownLatch(cl.lastDate); ← lastDateには月の末日が帰ってきます Hoge hoge2 = new Hoge(); hoge2.setmLatch(latch); hoge2.setmYear(this.currentYear); hoge2.setmMonth(this.currentMonth); hoge2.run(); }
Hoge.java(一部省略) public class Hoge { private static CountDownLatch mLatch; private static int mYear; private static int mMonth; private static int row; private static int col; public void run() { final CountDownLatch latch = new CountDownLatch(42); //カレンダーテーブル作成 final CalendarInfo cl = new CalendarInfo(Hoge.mYear, Hoge.mMonth); row = 0; col = 0; for (int i = 0; i < 42; i++) { if (cl.calendarMatrix[row][col] != 0) { new Thread(new Runnable() { @Override public void run() { try { final App app = (App) getsApplication(); String DataFileName = String.format("%04d%02d%02d.txt", Hoge.mYear, Hoge.mMonth, cl.calendarMatrix[row][col]); // OneDriveからスケジュールファイルを取得する前にスケジュールファイルを削除する File file = new File(getsApplication().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), DataFileName); if (file.exists()) { file.delete(); Log.d("デバッグ", String.format("%s を削除しました", DataFileName)); } final DownloadFiles schedule = new DownloadFiles(); // DownloadTaskを実行する // 引数がdoInBackground()メソッドに渡される schedule.setCountDownLatch(Hoge.mLatch); schedule.execute(DataFileName, "main_data", null, null); } catch (NullPointerException e) { e.printStackTrace(); } } }).start(); } col += 1; if (col == 7) { row += 1; col = 0; } } new Thread(new Runnable() { @Override public void run() { try { // 全ファイルのダウンロード待ち Log.d("デバッグ", "全ファイルのダウンロード待ちに入ります"); mLatch.await(); Log.d("デバッグ", "全ファイルのダウンロード待ちから抜けました"); } catch(InterruptedException e) { } } }).start(); } }
DownloadFiles.java
1DownloadFiles.java(一部省略) 2 3public final class DownloadFiles extends AsyncTask<String,Void,Boolean> { 4 5 private static CountDownLatch mLatch; 6 7 public static void setCountDownLatch(CountDownLatch latch ) { 8 mLatch = latch; 9 } 10 11 @Override 12 protected Boolean doInBackground(final String... param) { 13 String FileName = param[0]; 14 String Mode = param[1]; 15 download_file(FileName, Mode); 16 17 return true; 18 } 19 20 private void DownloadWait(String FileName, String Mode) { 21 Log.d("デバッグ", String.format("%s(%s) をダウンロードしています...", FileName, Mode) ); 22 23 try { 24 while(true) { 25 File file = new File(getsApplication().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), FileName ); 26 if( file.exists() && file.length() > 0 ) { 27 final App app = (App)getsApplication(); 28 if( Mode.equals("set_staff") ) { 29 //略 30 } 31 else if( Mode.equals("sub_staff") ) { 32 //略 33 } 34 else if( Mode.equals("sub_data") ) { 35 //略 36 } 37 else if( Mode.equals("main_data") ) { 38 Log.d("デバッグ", String.format("%s をダウンロードしました", FileName) ); 39 // ダウンロードしたファイルの読み込みはしない 40 // 画面の更新はしない 41 mLatch.countDown(); 42 Log.d("デバッグ", String.format("latch count:%d", mLatch.getCount() )); 43 } 44 break; 45 } 46 Thread.sleep(100); 47 } 48 } catch (InterruptedException e) { 49 Log.d("デバッグ", String.format("%s", e.getMessage() ) ); 50 } 51 } 52 53 private ICallback<Item> getDownloadCallback(final App context, final String filename, final String mode ) { 54 return new DefaultCallback<Item>(context) { 55 @Override 56 public void success(final Item item) { 57 if (item.children == null || item.children.getCurrentPage().isEmpty()) { 58 Log.d("デバッグ","OneDrive 空なんですけど・・"); 59 } else { 60 boolean findflag; 61 findflag = false; 62 for (final Item childItem : item.children.getCurrentPage()) { 63 if( childItem.name.equals(filename) ) { 64 findflag = true; 65 Log.d("デバッグ", String.format("%s ちゃん、見~つけた", filename)); 66 final DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); 67 final String downloadUrl = childItem.getRawObject().get("@content.downloadUrl").getAsString(); 68 final DownloadManager.Request request = new DownloadManager.Request(Uri.parse(downloadUrl)); 69 request.setTitle(childItem.name); 70 Resources res = context.getResources(); 71 request.setDescription(res.getString(R.string.file_from_onedrive)); 72 request.allowScanningByMediaScanner(); 73 if (childItem.file != null) { 74 request.setMimeType(childItem.file.mimeType); 75 } 76 request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 77 78 File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), filename); 79 request.setDestinationUri(Uri.fromFile(file)); 80 81 downloadManager.enqueue(request); 82 //toast( res.getString(R.string.starting_download_message), Toast.LENGTH_SHORT ); 83 84 DownloadWait(filename, mode); 85 86 break; 87 } 88 } 89 if ( findflag == false ) { 90 Log.d("デバッグ", String.format("%s ちゃん、居ませんでした", filename)); 91 mLatch.countDown(); 92 Log.d("デバッグ", String.format("latch count:%d", mLatch.getCount() )); 93 } 94 } 95 } 96 97 @Override 98 public void failure(final ClientException error) { 99 Log.d("デバッグ","Itemが見つかんねぇ"); 100 } 101 }; 102 } 103 104 private void download_file( String filename, String mode ) { 105 final App app = (App)getsApplication(); 106 107 try { 108 final IOneDriveClient oneDriveClient = getOneDriveClient(); 109 final ICallback<Item> itemCallback = getDownloadCallback(app, filename, mode); 110 111 final String itemId; 112 itemId = "root"; 113 114 oneDriveClient 115 .getDrive() 116 .getItems(itemId) 117 .buildRequest() 118 .expand(getExpansionOptions(oneDriveClient)) 119 .get(itemCallback); 120 } catch ( RuntimeException e ) { 121 Log.d("デバッグ", String.format("%s", e.getMessage()) ); 122 } 123 } 124}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。