Skip to content

Instantly share code, notes, and snippets.

@alterakey
Last active August 29, 2015 13:57
Show Gist options
  • Save alterakey/9443793 to your computer and use it in GitHub Desktop.
Save alterakey/9443793 to your computer and use it in GitHub Desktop.
A CursorLoader implmentation that enable it to delay handling of the exceptions in loading cursors
/**
* ThrowableCursorLoader: CursorLoader for CPs that tend to throw.
*
* Copyright 2014 Takahiro Yoshimura
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gmail.altakey.toolbox;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.support.v4.content.CursorLoader;
public class ThrowableCursorLoader extends CursorLoader {
private RuntimeException mError;
public ThrowableCursorLoader(Context context) {
super(context);
}
public ThrowableCursorLoader(Context context, Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
super(context, uri, projection, selection, selectionArgs, sortOrder);
}
@Override
public Cursor loadInBackground() {
try {
mError = null;
return super.loadInBackground();
} catch (RuntimeException e) {
mError = e;
return null;
}
}
public Throwable getLoadError() {
return mError;
}
public void rethrow() {
if (mError != null) {
throw mError;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment