Search in sources :

Example 1 with RealmException

use of io.realm.exceptions.RealmException in project realm-rxjava-example by kboyarshinov.

the class OnSubscribeRealm method call.

@Override
public void call(final Subscriber<? super T> subscriber) {
    synchronized (lock) {
        boolean canceled = this.canceled.get();
        if (!canceled && !subscribers.isEmpty()) {
            subscriber.add(newUnsubscribeAction(subscriber));
            subscribers.add(subscriber);
            return;
        } else if (canceled) {
            return;
        }
    }
    subscriber.add(newUnsubscribeAction(subscriber));
    subscribers.add(subscriber);
    RealmConfiguration.Builder builder = new RealmConfiguration.Builder(context);
    if (fileName != null) {
        builder.name(fileName);
    }
    Realm realm = Realm.getInstance(builder.build());
    boolean withError = false;
    T object = null;
    try {
        if (!this.canceled.get()) {
            realm.beginTransaction();
            object = get(realm);
            if (object != null && !this.canceled.get()) {
                realm.commitTransaction();
            } else {
                realm.cancelTransaction();
            }
        }
    } catch (RuntimeException e) {
        realm.cancelTransaction();
        sendOnError(new RealmException("Error during transaction.", e));
        withError = true;
    } catch (Error e) {
        realm.cancelTransaction();
        sendOnError(e);
        withError = true;
    }
    if (object != null && !this.canceled.get() && !withError) {
        sendOnNext(object);
    }
    try {
        realm.close();
    } catch (RealmException ex) {
        sendOnError(ex);
        withError = true;
    }
    if (!withError) {
        sendOnCompleted();
    }
    this.canceled.set(false);
}
Also used : RealmConfiguration(io.realm.RealmConfiguration) Realm(io.realm.Realm) RealmException(io.realm.exceptions.RealmException)

Example 2 with RealmException

use of io.realm.exceptions.RealmException in project realm-java by realm.

the class RealmJsonTests method createObjectFromJson_streamNullJson.

@Test
public void createObjectFromJson_streamNullJson() throws IOException {
    assumeThat(Build.VERSION.SDK_INT, greaterThanOrEqualTo(Build.VERSION_CODES.HONEYCOMB));
    InputStream in = TestHelper.loadJsonFromAssets(context, "all_types_invalid.json");
    realm.beginTransaction();
    try {
        realm.createObjectFromJson(AnnotationTypes.class, in);
        fail();
    } catch (RealmException ignored) {
    } finally {
        realm.commitTransaction();
        in.close();
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) RealmException(io.realm.exceptions.RealmException) Test(org.junit.Test)

Example 3 with RealmException

use of io.realm.exceptions.RealmException in project realm-java by realm.

the class RealmJsonTests method createOrUpdateObjectFromJson_streamInvalidJson.

@Test
public void createOrUpdateObjectFromJson_streamInvalidJson() throws IOException {
    assumeThat(Build.VERSION.SDK_INT, greaterThanOrEqualTo(Build.VERSION_CODES.HONEYCOMB));
    AllTypesPrimaryKey obj = new AllTypesPrimaryKey();
    obj.setColumnLong(1);
    realm.beginTransaction();
    realm.copyToRealm(obj);
    realm.commitTransaction();
    InputStream in = TestHelper.loadJsonFromAssets(context, "all_types_invalid.json");
    realm.beginTransaction();
    try {
        realm.createOrUpdateObjectFromJson(AllTypesPrimaryKey.class, in);
        fail();
    } catch (RealmException ignored) {
    } finally {
        realm.commitTransaction();
        in.close();
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) RealmException(io.realm.exceptions.RealmException) AllTypesPrimaryKey(io.realm.entities.AllTypesPrimaryKey) Test(org.junit.Test)

Example 4 with RealmException

use of io.realm.exceptions.RealmException in project realm-java by realm.

the class SyncObjectServerFacade method init.

@Override
public void init(Context context) {
    //noinspection TryWithIdenticalCatches
    try {
        // FIXME: Reflection can be avoided by moving some functions of SyncManager and ObjectServer out of public
        Class<?> syncManager = Class.forName("io.realm.ObjectServer");
        Method method = syncManager.getDeclaredMethod("init", Context.class);
        method.setAccessible(true);
        method.invoke(null, context);
    } catch (NoSuchMethodException e) {
        throw new RealmException("Could not initialize the Realm Object Server", e);
    } catch (InvocationTargetException e) {
        throw new RealmException("Could not initialize the Realm Object Server", e);
    } catch (IllegalAccessException e) {
        throw new RealmException("Could not initialize the Realm Object Server", e);
    } catch (ClassNotFoundException e) {
        throw new RealmException("Could not initialize the Realm Object Server", e);
    }
    if (applicationContext == null) {
        applicationContext = context;
        applicationContext.registerReceiver(new NetworkStateReceiver(), new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
    }
}
Also used : IntentFilter(android.content.IntentFilter) Method(java.lang.reflect.Method) NetworkStateReceiver(io.realm.internal.network.NetworkStateReceiver) RealmException(io.realm.exceptions.RealmException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 5 with RealmException

use of io.realm.exceptions.RealmException in project realm-java by realm.

the class SyncObjectServerFacade method invokeRemoveSession.

//FIXME remove this reflection call once we redesign the SyncManager to separate interface
//      from implementation to avoid issue like exposing internal method like SyncManager#removeSession
//      or SyncSession#close. This happens because SyncObjectServerFacade is internal, whereas
//      SyncManager#removeSession or SyncSession#close are package private & should not be public.
private void invokeRemoveSession(SyncConfiguration syncConfig) {
    try {
        if (removeSessionMethod == null) {
            synchronized (SyncObjectServerFacade.class) {
                if (removeSessionMethod == null) {
                    Method removeSession = SyncManager.class.getDeclaredMethod("removeSession", SyncConfiguration.class);
                    removeSession.setAccessible(true);
                    removeSessionMethod = removeSession;
                }
            }
        }
        removeSessionMethod.invoke(null, syncConfig);
    } catch (NoSuchMethodException e) {
        throw new RealmException("Could not lookup method to remove session: " + syncConfig.toString(), e);
    } catch (InvocationTargetException e) {
        throw new RealmException("Could not invoke method to remove session: " + syncConfig.toString(), e);
    } catch (IllegalAccessException e) {
        throw new RealmException("Could not remove session: " + syncConfig.toString(), e);
    }
}
Also used : Method(java.lang.reflect.Method) RealmException(io.realm.exceptions.RealmException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

RealmException (io.realm.exceptions.RealmException)31 Test (org.junit.Test)10 JSONException (org.json.JSONException)9 AllJavaTypes (io.realm.entities.AllJavaTypes)5 JSONObject (org.json.JSONObject)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 TargetApi (android.annotation.TargetApi)3 RealmConfiguration (io.realm.RealmConfiguration)3 AllTypesPrimaryKey (io.realm.entities.AllTypesPrimaryKey)3 Table (io.realm.internal.Table)3 Date (java.util.Date)3 Scanner (java.util.Scanner)3 JSONArray (org.json.JSONArray)3 Realm (io.realm.Realm)2 AllTypes (io.realm.entities.AllTypes)2 RealmPrimaryKeyConstraintException (io.realm.exceptions.RealmPrimaryKeyConstraintException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 Method (java.lang.reflect.Method)2 ParseException (java.text.ParseException)2