在启动时创建表后加载我的Sqlite表的最佳方法(Best way to load my Sqlite Tables after creating tables on startup)

编程入门 行业动态 更新时间:2024-10-18 12:23:36
启动时创建表后加载我的Sqlite表的最佳方法(Best way to load my Sqlite Tables after creating tables on startup)

我有两个静态表,每个表有大约500条记录,为我的应用程序中的一些ListView提供查找材料。 当应用程序首次启动并创建表时,我运行一个进程来填充表,这需要不到一分钟但我一直在寻找使用Async Task在后台运行该进程,并使用进度对话框让用户知道发生了什么。 我担心的是,当进程正在运行并且正在添加数据然后用户倾斜电话时,该过程将取消。 那么我的数据库状态会是什么? Async Task是最好的解决方案还是我应该使用线程?

I have two static tables with about 500 records each which provide lookup material for some ListViews in my app. When the app first starts and the tables are created I run a process to populate the tables which takes less than a minute but I've been looking to run the process in background using Async Task with a progress dialog letting the user know what is happening. My concern is that while the process is running and the data is being added and then the user tilts the phone the process will cancel. What would be the state of my database then? Is Async Task the best solution for this or should I use Threading?

最满意答案

因此,当您旋转或更改手机的方向时,活动是唯一被破坏的东西。 您不一定要摆脱异步任务。 事实上,它将继续存在。 只是不要让另一个任务进入并特别地处理它。

因此,如果你想让你的活动就好像在旋转时那样你可以在你离开的地方重新开始,那么就有一个名为onRetainNonConfigurationInstance()的方法。 它基本上是一种方法,它可以存储像saveInstanceState()中那样无法分区的对象

所以这个想法是:

public void onCreate(Bundle a) { ... AsyncTask myTask = getNonConfigurationInstance(); if (myTask == null) { myTask = new AsyncTask(); myTask.execute(); } ... } public Object onRetainNonConfigurationInstance() { return myTask; }

这将使异步任务保持运行,并且当您在轮换后调用onCreate时,您只需将其备份并执行需要完成的操作。

要注意的一件事是progressView。 必须将其销毁并重新初始化为新州。 此外,它的整体解雇并首先显示它应该在AsyncTask之外完成。 但没有什么可以说AsyncTask不能调用你总是在你的onCreate()中设置的一些回调,这样它就会通知告诉更新UI或播放完成声等。

So when you rotate or change the orientation of the phone, the activity is the only thing destroyed. You don't necessarily have to get rid of the async task. In fact it will live on. Just don't let another task come in and work on it ad-hocly.

So if you want to have your activity act as if upon rotating that you can start right back up, where you left off, there is a method called onRetainNonConfigurationInstance(). It's basically the method that stashes objects which can't be parceled like in saveInstanceState()

So the idea being:

public void onCreate(Bundle a) { ... AsyncTask myTask = getNonConfigurationInstance(); if (myTask == null) { myTask = new AsyncTask(); myTask.execute(); } ... } public Object onRetainNonConfigurationInstance() { return myTask; }

This will keep the async task running, and when you get your onCreate called after the rotation you just pick it back up and do what needs to be done.

One thing to be conscious of is the progressView. It will have to be destroyed and reinitialized to the new state. Also the overall dismissing of it and showing it in the first place should be done outside the AsyncTask. But nothing is to say that the AsyncTask can't call some callback that you always set in your onCreate() so that it will notify to tell to update the UI or play a sound of completion, etc.

更多推荐

本文发布于:2023-08-04 12:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1416041.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:启动时   加载   方法   Sqlite   load

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!