Xamarin Android中的异步任务实现

时间:2017-06-22 07:16:47

标签: c# android asynchronous xamarin.android

异步任务似乎是最近最严格的实施之一。我实际上不想在这里使用多线程但是异步任务。在我的下面的代码中,我已经实现了异步任务来从firebase获取我的数据到我的应用程序。即使我使用的是异步任务,这也是我在用户界面上遇到的问题。

我有3项活动。该应用程序启动到第一个活动,没有用户遇到任何形式的滞后。当我从第一个活动导航到第二个活动时,导航抽屉布局在第二个活动的数据出现之前滞后几秒钟,因此对于活动3,导致抽屉布局滞后。当我在3个活动之间导航时,它会平滑移动。 (意思是它在从一个活动移动到另一个活动之前就会滞后,然后才能顺利移动)。

请注意,当我在OnResume()中注释this.refreshAll()时,它会顺利移动

我的异步是否已经很好地实现了?

活性2

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

2 个答案:

答案 0 :(得分:0)

left:50%,transform:translate(-50%), display:inline,block, margin: auto中的this.refreshAll()来电可能会被阻止。尝试制作OnResume() OnResume()并等待async的结果。

this.refreshAll()

这至少应该允许UI在加载数据时保持响应。我的猜测是protected async override void OnResume() { base.OnResume(); await this.refreshAll(); } 可能会在调用之间缓存您的结果(可能只是寻找更改),因此在初始调用之后要做的工作要少得多。

请注意,在生命周期事件中放置async / await可能会产生意想不到的后果 - 例如,如果OnceAsync<>()方法等待的呼叫需要花费时间,async OnCreate()和{{1}方法通常在OnStart()完成之前启动。

答案 1 :(得分:0)

可重用的异步任务

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace Android.Basic.Core
{
    public class AsyncTasker : AsyncTask<Java.Lang.Void, Java.Lang.Void, Java.Lang.Void>
    {
        private Action Action { get; }
        public event EventHandler<object> TaskCompleted;
        public void NotifyCompletedTask(object result)
        {
            Xamarin.Essentials.MainThread.BeginInvokeOnMainThread(() =>
            {
                this.TaskCompleted?.Invoke(this, result);
            });
           
        }
        public AsyncTasker(Action action)
        {
            this.Action = action;
        }
        protected override Java.Lang.Void RunInBackground(params Java.Lang.Void[] @params)
        {
            if (this.Action == null)
                return null;
            this.Action();
            return null;
        }
    }
}

用法

var asyncTasker = new AsyncTasker(() =>
            {
                //Do non UI task, Use "MainThread.BeginInvokeOnMainThread" to update UI.
                

                //Notify that task completed
                asyncTasker.NotifyCompletedTask(null);

            });
            asyncTasker.TaskCompleted += delegate
            {
                Xamarin.Essentials.MainThread.BeginInvokeOnMainThread(() =>
                {
                    
                     //Do completed task
                });
            };
            asyncTasker.Execute();