8
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ContentProviderClientで高速化

Posted at

JellyBeanでも関連するAPIが拡張されている、ContentProviderClientです。
軽量なクエリだとContentResolver経由で発行するより5倍ほど高速です。
詳細:http://yuki312.blogspot.jp/2012/07/androidcontentresolvercontentprovidercl.html

使い終わったらreleaseを忘れないようにします。

ContentProviderClientのサンプルコード
@Override
protected void onResume() {
    super.onResume();
    initContentProviderClient();
}
 
@Override
protected void onPause() {
    super.onPause();
    releaseContentProviderClient();
}
 
private void initContentProviderClient() {
    releaseContentProviderClient();
    mContentProviderClient = getContentResolver()
            .acquireContentProviderClient(
                    ContactsContract.Contacts.CONTENT_URI);
}
 
private void releaseContentProviderClient() {
    if (mContentProviderClient != null) {
        mContentProviderClient.release();
    }
}
 
private void onUpdate() {
    Cursor c = null;
    try {
        c = mContentProviderClient.query(
                ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);
    } catch (RemoteException e) {
        // ContentProviderがDeadObject化している。
        // 必要に応じて、ContentProviderClientの再取得とリクエリを実施
        initContentProviderClient();
    } finally {
        if (c != null) {
            try {
                c.close();
            } catch (Exception e) {
                // N.O.P
            }
        }
    }
}
8
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?