2017-06-22 17 views
2

非同期タスクは最近、最も難しい実装の1つと考えられました。私は実際にここでマルチスレッドを使用するのではなく、非同期タスクを使用したいと考えています。私のコードでは、Firebaseから自分のアプリケーションにデータをフェッチするための非同期タスクを実装しました。私は非同期タスクを使用していますが、これは私がユーザーインターフェイス上で経験しているものです。Xamarin Androidでの非同期タスクの実装

私には3つのアクティビティがあります。アプリは、ユーザーが何らかの遅れを経験することなく、第1のアクティビティを開始します。 1回目のアクティビティから2回目のアクティビティにナビゲートすると、2回目のアクティビティのデータが表示される前にナビゲーションドロワのレイアウトが数秒間遅れるので、3回目のアクティビティ間を移動するとスムーズに移動します。 (それは、それが滑らかに動く前に、あるアクティビティーから別のアクティビティーに1回移動するだけで遅れることを意味します)。

私はOnResume()でthis.refreshAll()をコメントアウトするとき、それがスムーズに動く

が私の非同期がうまく実装されていることに注意してください?

Activity2

protected override void OnCreate(Bundle savedInstanceState) 
{ 
    base.OnCreate(savedInstanceState); 
    fab = FindViewById<FloatingActionButton>(Resource.Id.fab); 

    fab.Click += delegate 
    { 
     LayoutInflater HeroInflater = LayoutInflater.From(this); 
     View mView = peaceHeroInflater.Inflate(Resource.Layout.HeroDialog, null); 
     Android.Support.V7.App.AlertDialog.Builder alertDialogBuilder = new Android.Support.V7.App.AlertDialog.Builder(this); 
     alertDialogBuilder.SetView(mView); 

     var HeroName = mView.FindViewById<EditText>(Resource.Id.editText1); 
     alertDialogBuilder.SetCancelable(false) 
      .SetPositiveButton("Save", async delegate 
      { 
       Hero hero = new Hero(); 
       hero.Uid = string.Empty; 
       hero.Name = HeroName.Text;  
       var firebase = new FirebaseClient(FirebaseUrl);       
       var item = await firebase.Child("sometable").PostAsync<PeaceHero>(hero); 

       await ReloadData(); 
      }) 
      .SetNegativeButton("Cancel", delegate 
      { 
       alertDialogBuilder.Dispose(); 
      }); 

     Android.Support.V7.App.AlertDialog alertDialogAndroid = alertDialogBuilder.Create(); 
     alertDialogAndroid.Show(); 
    }; 
} 

private async void refreshAll() 
{ 
    await ReloadData(); 
} 

private async Task ReloadData() 
{ 
    var firebase = new FirebaseClient(FirebaseUrl); 
    var items = await firebase.Child("sometable").OnceAsync<>(); 

    foreach (var item in items) 
    { 
     Hero details = new Hero(); 
     details.Uid = item.Key; 
     details.Name = item.Object.Name; 
     add_Hero.Add(details); 
    } 

    rv.SetLayoutManager(new LinearLayoutManager(this)); 
    rv.SetItemAnimator(new DefaultItemAnimator());      
} 

protected override void OnResume() 
{ 
    base.OnResume(); 
    this.refreshAll(); 
} 

更新

private async Task refreshAll() 
     { 

      await RefreshData(); 


     } 


protected async override void OnResume() 
     { 
      base.OnResume(); 
      await refreshAll(); 

     } 

LOG

[Mono] AOT: image 'Mono.Security.dll.so' not found: dlopen failed: library "/data/app-lib/com.companyname.peaceapp.peaceapp-130/libaot-Mono.Security.dll.so" not found 
[Mono] AOT: image '/usr/local/lib/mono/aot-cache/arm/Mono.Security.dll.so' not found: dlopen failed: library "/data/app-lib/com.companyname.peaceapp.peaceapp-130/libaot-Mono.Security.dll.so" not found 
[Mono] Config attempting to parse: 'Mono.Security.dll.config'. 
[Mono] Config attempting to parse: '/usr/local/etc/mono/assemblies/Mono.Security/Mono.Security.config'. 
[Mono] Assembly Ref addref System[0x86e311a8] -> Mono.Security[0x8792d7e8]: 2 
[Mono] Assembly Ref addref Mono.Security[0x8792d7e8] -> mscorlib[0x73d41ac0]: 68 
Loaded assembly: Mono.Security.dll [External] 
[Mono] Assembly Ref addref System.Net.Http[0x8649e068] -> System.Core[0x7f2bf708]: 9 
[Mono] Assembly Ref addref Mono.Android[0x7f1692f0] -> System[0x86e311a8]: 7 
Thread started: <Thread Pool> #4 
Thread started: <Thread Pool> #5 
[Mono] [0x87aecc90] worker starting 
Thread started: <Thread Pool> #6 
[Mono] [0x87b045b0] worker starting 
Thread started: <Thread Pool> #7 
[Mono] [0x87ae1a88] worker starting 
[Choreographer] Skipped 80 frames! The application may be doing too much work on its main thread. 
[RecyclerView] No adapter attached; skipping layout 
[ProgressBar] updateDrawableBounds: left = 72 
[ProgressBar] updateDrawableBounds: top = 0 
[ProgressBar] updateDrawableBounds: right = 72 
[ProgressBar] updateDrawableBounds: bottom = 0 
[Mono] [0x87aecc90] hill climbing, change max number of threads 4 
[RecyclerView] No adapter attached; skipping layout 
[Mono] Assembly Ref addref Mono.Security[0x8792d7e8] -> System[0x86e311a8]: 8 
Thread started: <Thread Pool> #8 
Thread started: <Thread Pool> #9 
[Mono] [0x851329e8] worker starting 
Thread started: <Thread Pool> #10 
[Mono] [0x85132de0] worker starting 
[Mono] Unloading image System.dll [0x851046d8]. 
[Mono] Image addref System[0x8548b280] -> System.dll[0x86e30720]: 5 
[Mono] Config attempting to parse: 'System.dll.config'. 
[Mono] Config attempting to parse: '/usr/local/etc/mono/assemblies/System/System.config'. 
[Mono] Unloading image System.dll [0x851046d8]. 
[Mono] Image addref System[0x8548f438] -> System.dll[0x86e30720]: 6 
[Mono] Config attempting to parse: 'System.dll.config'. 
[Mono] Config attempting to parse: '/usr/local/etc/mono/assemblies/System/System.config'. 
[Mono] Unloading image System.dll [0x851046d8]. 
[Mono] Image addref System[0x854829d8] -> System.dll[0x86e30720]: 7 
[Mono] Config attempting to parse: 'System.dll.config'. 
[Mono] Config attempting to parse: '/usr/local/etc/mono/assemblies/System/System.config'. 
[Mono] DllImport searching in: '__Internal' ('(null)'). 
[Mono] Searching for 'java_interop_jnienv_is_assignable_from'. 
[Mono] Probing 'java_interop_jnienv_is_assignable_from'. 
[Mono] Found as 'java_interop_jnienv_is_assignable_from'. 
[Mono] DllImport searching in: '__Internal' ('(null)'). 
[Mono] Searching for 'java_interop_jnienv_get_byte_array_region'. 
[Mono] Probing 'java_interop_jnienv_get_byte_array_region'. 
[Mono] Found as 'java_interop_jnienv_get_byte_array_region'. 
[Mono] DllImport searching in: '__Internal' ('(null)'). 
[Mono] Searching for 'java_interop_jnienv_set_byte_array_region'. 
[Mono] Probing 'java_interop_jnienv_set_byte_array_region'. 
[Mono] Found as 'java_interop_jnienv_set_byte_array_region'. 
[Mono] The request to load the assembly System.Core v4.0.0.0 was remapped to v2.0.5.0 
[Mono] Unloading image System.Core.dll [0x854f61d0]. 
[Mono] Image addref System.Core[0x85501bf0] -> System.Core.dll[0x7f2bedb0]: 5 
[Mono] Config attempting to parse: 'System.Core.dll.config'. 
[Mono] Config attempting to parse: '/usr/local/etc/mono/assemblies/System.Core/System.Core.config'. 
[Mono] The request to load the assembly System.Core v4.0.0.0 was remapped to v2.0.5.0 
[Mono] Unloading image System.Core.dll [0x855214c8]. 
[Mono] Image addref System.Core[0x84ff8888] -> System.Core.dll[0x7f2bedb0]: 6 
[Mono] Config attempting to parse: 'System.Core.dll.config'. 
[Mono] Config attempting to parse: '/usr/local/etc/mono/assemblies/System.Core/System.Core.config'. 
[Mono] [0x851329e8] hill climbing, change max number of threads 5 
[Mono] DllImport searching in: '__Internal' ('(null)'). 
[Mono] Searching for 'java_interop_jnienv_get_static_field_id'. 
[Mono] Probing 'java_interop_jnienv_get_static_field_id'. 
[Mono] Found as 'java_interop_jnienv_get_static_field_id'. 
[Mono] DllImport searching in: '__Internal' ('(null)'). 
[Mono] Searching for 'java_interop_jnienv_get_static_object_field'. 
[Mono] Probing 'java_interop_jnienv_get_static_object_field'. 
[Mono] Found as 'java_interop_jnienv_get_static_object_field'. 
[Mono] DllImport searching in: '__Internal' ('(null)'). 
[Mono] Searching for 'java_interop_jnienv_call_boolean_method_a'. 
[Mono] Probing 'java_interop_jnienv_call_boolean_method_a'. 
[Mono] Found as 'java_interop_jnienv_call_boolean_method_a'. 
Thread finished: <Thread Pool> #10 
[Mono] [0x85132de0] worker finishing 

答えて

0

その可能ですOnResume()のコールがブロックされています。 OnResume()asyncとし、結果はthis.refreshAll()です。

protected async override void OnResume() 
{ 
    base.OnResume(); 
    await this.refreshAll(); 
} 

これは、少なくとも、データのロード中にUIが反応し続けることができるようにする必要があります。私の推測では、OnceAsync<>()は、呼び出し間で結果をキャッシュしている可能性があります(変更を探しているだけかもしれません)ので、最初の呼び出し後に行う作業がはるかに少なくなります。

は注意してください、非同期を置く/予期しない結果をもたらす可能性があるライフサイクルイベントに待つ - 例えば async OnCreate()方法は、時間がかかっている待望のコールを持っている場合、 OnStart()OnResume()方法は、しばしば OnCreate()終了する前に開始されます。

+0

それはまだそれのように遅れています。私の更新されたコードを見てください –

+0

あなたの質問への答え_ "私の非同期井戸は実装されていますか?"あなたのアプリは遅れています**(何かをする時間を取って表示するなど)**ブロッキング**(何かをする時間がかかり、可視のコンポーネントとのユーザーのやりとりを止める、つまりそれを感じるクラッシュした)?ブロッキングの場合、何かが 'async' /' await'に実装されていないので、遅れているだけで(つまり、スクロールしてアプリケーションのボタンをクリックするなど)、それはファイルの読み込み/データダウンロードコードの問題です。 async'/'await'コードは大丈夫です –

+0

もしあなたが言っているようにうまく実装されていたら、どうやって遅れているのか、問題があるのでしょうか? –