HardBirch

ContentResolver.query 和Activity.managedQuery的区别

时间:13-06-19 栏目:安卓技术篇 作者:魔豆先生 评论:0 点击: 7,146 次

摘取几段文字来说明下他们的区别,一般的解释都差不多~ 只是不同的说明解释的到位程度不一

文字一:

To query a content provider, you can use either the ContentResolver.query() method or the Activity.managedQuery() method. Both methods take the same set of arguments, and both return a Cursor object. However, managedQuery() causes the activity to manage the life cycle of the Cursor. A managed Cursor handles all of the niceties, such as unloading itself when the activity pauses, and requerying itself when the activity restarts. You can ask an Activity to begin managing an unmanaged Cursor object for you by calling Activity.startManagingCursor().

managerQuery() Wrapper around ContentResolver.query() and invoke startManagingCursor(Cursor);

 

文字二:

有人问道managedQuery和query的区别,我们都知道在Android系统中,SQLite数据库的相关操作方式被封装为内容提供 Content Provider,可以帮助那些不会SQL语言的开发者快速实现Android平台上的数据库操作,但是平时我们在查询时一般返回的是Cursor对象, 从本质上来看这两个API是不同的类提供的。Android123提示比如 ContentResolver.query(),以及 Activity.managedQuery()所以,我们看到一个是ContentResolver提供的查询方法,位于 android.content.ContextWrapper.getContentResolver(),另一个则为Activity。

这两种方法的参数是一样的,但是Activity类的方法在整个声明周期中受Activity的影响,而常规我们处理数据逻辑可能单独分成一个类,直接使 用Context对象传递实例句柄,同时Android开发网还建议大家,对于数据库查询操作如果数据较为庞大尽量使用异步的 AsyncQueryHandler方法防止阻塞线程。

 

文字三:

managedQuery()和ContentResolver.query()的区别
managedQuery() will use ContentResolver's query(). The difference is that with managedQuery() the activity will keep a reference to your Cursor and close it whenever needed (in onDestroy() for instance.) If you do query() yourself, you will have to manage the Cursor as a sensitive resource. If you forget, for instance, to close() it in onDestroy(), you will leak underlying resources (logcat will warn you about it.)

To query a content provider, you can use either the ContentResolver.query() method or the Activity.managedQuery() method. Both methods take the same set of arguments, and both return a Cursor object. However, managedQuery() causes the activity to manage the life cycle of the Cursor. A managed Cursor handles all of the niceties, such as unloading itself when the activity pauses, and requerying itself when the activity restarts. You can ask an Activity to begin managing an unmanaged Cursor object for you by calling Activity.startManagingCursor().

 

 

声明: 本文由( 魔豆先生 )原创编译,转载请保留链接: ContentResolver.query 和Activity.managedQuery的区别

ContentResolver.query 和Activity.managedQuery的区别:等您坐沙发呢!

发表评论


QQ群互动

Linux系统与内核学习群:194051772

WP建站技术学习交流群:194062106

魔豆之路QR

魔豆的Linux内核之路

魔豆的Linux内核之路

优秀工程师当看优秀书籍

优秀程序员,要看优秀书!

赞助商广告

友荐云推荐