通过 HTTP 检索数据的最佳多线程架构?
世界!
我正在构建一个应用程序,该应用程序必须在多个不同的 Activity 中从服务器检索数据。一些数据被缓存到 SQLite 数据库(并在将来从那里检索),而一些数据必须每次都从服务器调用。我需要知道最适合此应用程序的多线程架构是什么。多个Activity
将连接到服务器。
我认为绑定 Service
是最好的,但我当然不确定。如果这是要走的路,我到底如何在其中实现多线程?
谢谢
world!
I'm building an application that has to retrieve data from a server in several different Activity
s. Some data is cached to a SQLite database (and retrieved from there instead in future), and some must be called fresh from the server every time. I need to know what the best multi-threaded architecture would be for this application. Multiple Activity
s will be connecting to the server.
I'm thinking a bound Service
would be best, but of course I'm not sure. If that's the way to go, how exactly do I implement multi-threading in it?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
IntentService 和 AsynTask 等 Android 类是线程最安全的,因为当调用 Activity 的 finish 方法时,它们不会使 Activity 保持打开状态。如果您创建自己的线程实现,则必须记住在活动完成时停止线程,以便回收进程的内存。
The android classes such as IntentService and AsynTask are thread safest because they will not keep your activity open when your activity's finish method is called. If you create your own Thread implementation you must remember to stop the thread on Activity finish so that the process' memory will be reclaimed.
在像你这样的情况下,我将使用一组单例工厂类来访问本地 SQLite 数据库和执行异步复制的后台服务
In a situation like yours I am going with a set of singleton factory classes to access local SQLite db and a background service doing asynchronous replication