最近,Async Task 似乎是最棘手的实现之一。我实际上不想在这里使用多线程,而是使用 async tasks。在下面的代码中,我已经实现了 async task 来从 firebase 中获取我的数据到我的应用程序中。即使我正在使用 async task,但这就是我在用户界面上所经历的。
我有三个活动。应用程序启动到第一个活动时,用户不会感受到任何形式的延迟。当我从第一个活动导航到第二个活动时,导航抽屉布局会有几秒钟的延迟,然后才会显示第二个活动和第三个活动的数据。之后,在我在三个活动之间导航时,它会稍微流畅一些。(也就是说,只有在从一个活动移动到另一个活动时才会出现延迟,之后就会流畅地移动)。
请注意,当我在 OnResume() 中注释掉 this.refreshAll()
后,它会流畅地移动
我的 Async 实现得好吗?
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();
}
日志
[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
async
/await
中没有实现某些东西,如果只是滞后(即用户仍然可以滚动、点击应用程序中的按钮等),那么这是文件读取/数据下载代码的问题,而async
/await
代码是好的。 - Jonathan Twite