Asynctask vs Thread vs Services vs Loader

前端 未结 6 1797
你的背包
你的背包 2020-12-04 14:55

I got slightly confused about the differences between Asynctask, Thread, Service, Loader in Android.

I know how

6条回答
  •  悲&欢浪女
    2020-12-04 15:12

    But many peoples says that "Asynctask is outdated", and don't recommend to use them.

    I haven't come across someone saying this. But it is the Android team's job to decide when some part of the framework is outdated or deprecated. CursorLoaders use AsyncTaskLoader which uses AsyncTask. AsyncTasks are an abstraction that prevent the developer from having to implement nasty Thread state logic. This means that all these classes use in turn useThreads on the background.

    So, is Asynctask really so bad and i should use framework for networking tasks? And what should i use for background (not networking) task?

    It is all about knowing when and how to use your tools. You mention CursorLoader. In that particular case, when you read the docs and play with it a bit, you realize that it purposely integrates smoothly with ContentProviders. Now, ContentProviders abstract the underlying data; you can be querying a local SQLite database or a remote server.

    Typically, AsyncTasksare used to retrieve concise pieces of "not-too-large" information (when used to talk with servers). People might say that AsyncTasks are outdated because there are better(more efficient) ways to interact with servers (see Retrofit).

提交回复
热议问题