@Override
  public void call(final Subscriber<? super T> subscriber) {
    final Realm realm = Realm.getInstance(context);
    thread = Thread.currentThread();
    subscriber.add(
        Subscriptions.create(
            new Action0() {
              @Override
              public void call() {
                if (thread != null && !thread.isInterrupted()) {
                  thread.interrupt();
                }
              }
            }));

    boolean interrupted = false;
    boolean withError = false;

    T object = null;
    try {
      realm.beginTransaction();
      object = get(realm);
      interrupted = thread.isInterrupted();
      if (object != null && !interrupted) {
        realm.commitTransaction();
      } else {
        realm.cancelTransaction();
      }
    } catch (RuntimeException e) {
      realm.cancelTransaction();
      subscriber.onError(new RealmException("Error during transaction.", e));
      withError = true;
    } catch (Error e) {
      realm.cancelTransaction();
      subscriber.onError(e);
      withError = true;
    }
    if (!interrupted && !withError) {
      subscriber.onNext(object);
    }

    try {
      realm.close();
    } catch (RealmException ex) {
      subscriber.onError(ex);
      withError = true;
    }
    thread = null;
    if (!withError) {
      subscriber.onCompleted();
    }
    if (interrupted) {
      Thread.currentThread().interrupt();
    }
  }
示例#2
0
 private static void executeTransaction(Realm realm, Realm.Transaction transaction) {
   if (transaction == null) return;
   realm.beginTransaction();
   try {
     transaction.execute(realm);
     realm.commitTransaction();
   } catch (RuntimeException e) {
     realm.cancelTransaction();
     Log.e(LOG_TAG, e.getMessage(), e);
     throw new RealmException("Error during transaction.", e);
   } catch (Error e) {
     realm.cancelTransaction();
     Log.e(LOG_TAG, e.getMessage(), e);
     throw e;
   }
 }
  @Override
  public void call(final Subscriber<? super RealmResults<T>> subscriber) {
    final Realm realm =
        dbName != null ? Realm.getInstance(context, dbName) : Realm.getInstance(context);
    subscriber.add(
        Subscriptions.create(
            new Action0() {
              @Override
              public void call() {
                try {
                  realm.close();
                } catch (RealmException ex) {
                  subscriber.onError(ex);
                }
              }
            }));

    RealmResults<T> object;
    realm.beginTransaction();
    try {
      object = get(realm);
      realm.commitTransaction();
    } catch (RuntimeException e) {
      realm.cancelTransaction();
      subscriber.onError(new RealmException("Error during transaction.", e));
      return;
    } catch (Error e) {
      realm.cancelTransaction();
      subscriber.onError(e);
      return;
    }
    if (object != null) {
      subscriber.onNext(object);
    }
    subscriber.onCompleted();
  }
 // idをキーにTodoデータを削除
 public boolean deleteTodoById(int id) {
   // トランザクション開始
   realm.beginTransaction();
   try {
     // idに一致するレコードを削除
     realm.where(Todo.class).equalTo(Todo.PRIMARY_KEY, id).findAll().clear();
     // コミット
     realm.commitTransaction();
   } catch (Exception e) {
     // ロールバック
     realm.cancelTransaction();
     return false;
   }
   return true;
 }
 // Todoデータを登録
 public boolean insertOrUpdateTodo(Todo todo) {
   synchronized (this) {
     if (todo.getId() == 0) {
       // 登録されているTodoデータの最大idを取得し、+1 したものをidとする(つまり連番)
       todo.setId(selectTodoMaxId() + 1);
     }
     // トランザクション開始
     realm.beginTransaction();
     try {
       // idにプライマリキーを張ってあるため既に同一idのデータが存在していれば更新となる
       realm.copyToRealmOrUpdate(todo);
       // コミット
       realm.commitTransaction();
     } catch (Exception e) {
       // ロールバック
       realm.cancelTransaction();
       return false;
     }
     return true;
   }
 }