Pratik Butani
Pratik Butani

Reputation: 62429

ContentProviderClient release() deprecated

I found some solution to delete database and recreate it using ContentProvider.

        ContentResolver resolver = mContext.getContentResolver();
        ContentProviderClient client = resolver.acquireContentProviderClient(KOOPSContentProvider.AUTHORITY);

        assert client != null;
        KOOPSContentProvider provider = (KOOPSContentProvider) client.getLocalContentProvider();

        assert provider != null;
        provider.resetDatabase();

        client.release();

but in that ContentProviderClient class has release() which is deprecated, Is there any other way to free up resources.

Edited: If I try to use close(), It is displaying as warning as follow.

This ContentProviderClient should be freed up after use with #release().

Many resources, such as TypedArrays, VelocityTrackers, etc., should be recycled (with a recycle() call) after use. This lint check looks for missing recycle() calls.

and close() displaying as disabled, why?

enter image description here

Upvotes: 2

Views: 1791

Answers (2)

Wirling
Wirling

Reputation: 5375

in case anyone is wondering what the code should be:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
{
    client.close();            
}
else
{
    client.release();
}

Upvotes: 5

WenChao
WenChao

Reputation: 3665

it is replaced by close but it is available only on API 24+

see more https://developer.android.com/reference/android/content/ContentProviderClient.html#close()

close on 24 is the same as release below 24 see source code of ContentProviderClient

  /**
 * Closes this client connection, indicating to the system that the
 * underlying {@link ContentProvider} is no longer needed.
 */
@Override
public void close() {
    closeInternal();
}

/**
 * @deprecated replaced by {@link #close()}.
 */
@Deprecated
public boolean release() {
    return closeInternal();
}

it is disable because you need to select correct api level enter image description here

Upvotes: 2

Related Questions