io.realm.RealmObject Java Examples

The following examples show how to use io.realm.RealmObject. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ApiObserver.java    From ApiClient with Apache License 2.0 6 votes vote down vote up
@Override
public <Item extends RealmObject> Observable<List<Item>> getApiObservable(Observable<List<Item>> api, Class<Item> realmClass, String sortedField, List<Integer> ids) {
    if (getStorage() != null) {
        RealmResults<Item> realmResults;
        if (sortedField != null)
            realmResults = (ids == null) ? getItems(realmClass, sortedField) : getItems(realmClass, sortedField, ids);
        else
            realmResults = getItems(realmClass);
        Observable<List<Item>> realmObserver = realmResults.asObservable()
                .filter(RealmResults::isLoaded)
                .compose(getLifecycle())
                .switchMap(Observable::just);
        Observable<List<Item>> retrofitObserver = api
                .compose(applySchedulers())
                .compose(getLifecycle());
        return Observable.<List<Item>>create(subscriber -> {
            realmObserver.take(2).subscribe(subscriber::onNext, subscriber::onError, subscriber::onCompleted);
            retrofitObserver.subscribe(this::setItems, subscriber::onError);
        }).compose(getLifecycle());
    } else
        return api;
}
 
Example #2
Source File: HistoryUtils.java    From 600SeriesAndroidUploader with MIT License 6 votes vote down vote up
public static void checkTreatmentDateDuplicated(Class clazz, PumpHistoryInterface record, TreatmentsEndpoints.Treatment treatment) {
    long timestamp = record.getEventDate().getTime();
    String key = record.getKey();

    RealmResults results = RealmObject.getRealm(record).where(clazz)
            .greaterThan("eventDate", new Date(timestamp - 2000L))
            .lessThan("eventDate", new Date(timestamp + 2000L))
            .findAll();

    if (key != null && results.size() > 1) {
        Log.w(TAG, "found " + results.size() + " events with the same date");
        int n = 0;
        while (n < results.size()
                && ((PumpHistoryInterface) results.get(n)).getKey() != null
                && !(((PumpHistoryInterface) results.get(n)).getKey().equals(key))
        ) {n++;}
        if (n > 0) {
            Log.w(TAG, String.format("adjusted eventDate for nightscout +%s seconds", n * 2));
            treatment.setCreated_at(new Date(timestamp + n * 2000));
        }
    }
}
 
Example #3
Source File: OkApiModule.java    From Forage with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Custom Gson to make Retrofit Gson adapter work with Realm objects
 */
@NonNull
@Provides
@Singleton
public static Gson provideGson(@NonNull ListTypeAdapterFactory jsonArrayTypeAdapterFactory,
                               @NonNull HtmlAdapter htmlAdapter,
                               @NonNull StringCapitalizerAdapter stringCapitalizerAdapter) {

    return new GsonBuilder()
            .setExclusionStrategies(new ExclusionStrategy() {
                @Override
                public boolean shouldSkipField(FieldAttributes f) {
                    return f.getDeclaringClass().equals(RealmObject.class);
                }

                @Override
                public boolean shouldSkipClass(Class<?> clazz) {
                    return false;
                }
            })
            .registerTypeAdapterFactory(jsonArrayTypeAdapterFactory)
            .registerTypeAdapter(String.class, htmlAdapter)
            .registerTypeAdapter(String.class, stringCapitalizerAdapter)
            .create();
}
 
Example #4
Source File: GsonProvider.java    From mvvm-template with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Make gson which {@link DateDeserializer} and compatible with {@link RealmObject}
 * @return {@link Gson} object
 */
public static Gson makeGsonForRealm() {
    return makeDefaultGsonBuilder()
            .setExclusionStrategies(new ExclusionStrategy() {
                @Override
                public boolean shouldSkipField(FieldAttributes f) {
                    return f.getDeclaringClass().equals(RealmObject.class);
                }

                @Override
                public boolean shouldSkipClass(Class<?> clazz) {
                    return false;
                }
            })
            .create();
}
 
Example #5
Source File: Database.java    From aptoide-client-v8 with GNU General Public License v3.0 6 votes vote down vote up
public <E extends RealmObject> void deleteAllExcluding(Class<E> classType, String classField,
    List<String> fieldsIn) {
  Realm realm = get();
  try {
    realm.beginTransaction();
    RealmQuery<E> query = realm.where(classType);
    for (String field : fieldsIn) {
      query.notEqualTo(classField, field);
    }
    query.findAll()
        .deleteAllFromRealm();
    realm.commitTransaction();
  } finally {
    if (realm != null) {
      realm.close();
    }
  }
}
 
Example #6
Source File: Database.java    From aptoide-client-v8 with GNU General Public License v3.0 6 votes vote down vote up
public <E extends RealmObject> void deleteAllIn(Class<E> classType, String classField,
    String[] fieldsIn) {
  Realm realm = get();
  try {
    realm.beginTransaction();
    realm.where(classType)
        .in(classField, fieldsIn)
        .findAll()
        .deleteAllFromRealm();
    realm.commitTransaction();
  } finally {
    if (realm != null) {
      realm.close();
    }
  }
}
 
Example #7
Source File: AccessorFactory.java    From aptoide-client-v8 with GNU General Public License v3.0 5 votes vote down vote up
@NonNull
public static <T extends RealmObject, A extends Accessor> A getAccessorFor(Database database,
    Class<T> clazz) {
  if (clazz.equals(Store.class)) {
    return (A) new StoreAccessor(database);
  }

  throw new RuntimeException("Create accessor for class " + clazz.getName());
}
 
Example #8
Source File: Database.java    From aptoide-client-v8 with GNU General Public License v3.0 5 votes vote down vote up
public <E extends RealmObject> Observable<List<E>> findAsList(RealmQuery<E> query) {
  return Observable.just(query.findAll())
      .filter(realmObject -> realmObject != null)
      .flatMap(realmObject -> realmObject.<E>asObservable().unsubscribeOn(
          RealmSchedulers.getScheduler()))
      .flatMap(realmObject -> copyFromRealm(realmObject))
      .defaultIfEmpty(null);
}
 
Example #9
Source File: Database.java    From aptoide-client-v8 with GNU General Public License v3.0 5 votes vote down vote up
public <E extends RealmObject> void delete(Class<E> clazz, String key, String value) {
  Realm realm = get();
  try {
    E obj = realm.where(clazz)
        .equalTo(key, value)
        .findFirst();
    deleteObject(realm, obj);
  } finally {
    if (realm != null) {
      realm.close();
    }
  }
}
 
Example #10
Source File: Database.java    From aptoide-client-v8 with GNU General Public License v3.0 5 votes vote down vote up
public <E extends RealmObject> void deleteObject(Realm realm, E obj) {
  realm.beginTransaction();
  try {
    if (obj != null && obj.isValid()) {
      obj.deleteFromRealm();
      realm.commitTransaction();
    } else {
      realm.cancelTransaction();
    }
  } catch (Exception ex) {
    CrashReport.getInstance()
        .log(ex);
    realm.cancelTransaction();
  }
}
 
Example #11
Source File: Database.java    From aptoide-client-v8 with GNU General Public License v3.0 5 votes vote down vote up
public <E extends RealmObject> void delete(Class<E> clazz, String key, Integer value) {
  Realm realm = get();
  try {
    E obj = realm.where(clazz)
        .equalTo(key, value)
        .findFirst();
    deleteObject(realm, obj);
  } finally {
    if (realm != null) {
      realm.close();
    }
  }
}
 
Example #12
Source File: Database.java    From aptoide-client-v8 with GNU General Public License v3.0 5 votes vote down vote up
public <E extends RealmObject> void delete(Class<E> clazz, String key, Long value) {
  Realm realm = get();
  try {
    E obj = realm.where(clazz)
        .equalTo(key, value)
        .findFirst();
    deleteObject(realm, obj);
  } finally {
    if (realm != null) {
      realm.close();
    }
  }
}
 
Example #13
Source File: Database.java    From aptoide-client-v8 with GNU General Public License v3.0 5 votes vote down vote up
public <E extends RealmObject> void deleteAll(Class<E> clazz) {
  Realm realm = get();
  try {
    realm.beginTransaction();
    realm.delete(clazz);
    realm.commitTransaction();
  } finally {
    if (realm != null) {
      realm.close();
    }
  }
}
 
Example #14
Source File: Database.java    From aptoide-client-v8 with GNU General Public License v3.0 5 votes vote down vote up
public <E extends RealmObject> void insertAll(List<E> objects) {
  Realm realm = get();
  try {
    realm.beginTransaction();
    realm.insertOrUpdate(objects);
    realm.commitTransaction();
  } finally {
    if (realm != null) {
      realm.close();
    }
  }
}
 
Example #15
Source File: Database.java    From aptoide-client-v8 with GNU General Public License v3.0 5 votes vote down vote up
public <E extends RealmObject> void insert(E object) {
  Realm realm = get();
  try {
    realm.beginTransaction();
    realm.insertOrUpdate(object);
    realm.commitTransaction();
  } finally {
    if (realm != null) {
      realm.close();
    }
  }
}
 
Example #16
Source File: GsonRealmBuilder.java    From batteryhub with Apache License 2.0 5 votes vote down vote up
private static GsonBuilder getBuilder() {
    return new GsonBuilder()
            .setExclusionStrategies(new ExclusionStrategy() {
                @Override
                public boolean shouldSkipField(FieldAttributes f) {
                    return f.getDeclaringClass().equals(RealmObject.class);
                }

                @Override
                public boolean shouldSkipClass(Class<?> clazz) {
                    return false;
                }
            });
}
 
Example #17
Source File: ApiClient.java    From ApiClient with Apache License 2.0 5 votes vote down vote up
/**
 * Get the api singleton from the api interface
 *
 * @return api
 */
@Override
public Api getApi() {
    if (mApi == null) {
        mApi = new Retrofit.Builder()
                .client(new OkHttpClient.Builder()
                        .addInterceptor(API_KEY_INTERCEPTOR)
                        .build())
                .baseUrl(mApiBaseUrl)
                .addConverterFactory(GsonConverterFactory.create(getGsonBuilder(new GsonBuilder())
                        .setExclusionStrategies(new ExclusionStrategy() {
                            @Override
                            public boolean shouldSkipField(FieldAttributes f) {
                                return f.getDeclaringClass().equals(RealmObject.class);
                            }

                            @Override
                            public boolean shouldSkipClass(Class<?> clazz) {
                                return false;
                            }
                        })
                        .create()))
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build()
                .create(mClazz);
    }
    return mApi;
}
 
Example #18
Source File: RealmHelper.java    From EMQ-Android-Toolkit with Apache License 2.0 5 votes vote down vote up
public <T extends RealmObject> void deleteTopic(Class<T> clazz, String id) {
    final RealmResults results = mRealm.where(clazz)
            .equalTo("connectionId", id)
            .findAll();
    mRealm.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {
            results.deleteAllFromRealm();

        }
    });
}
 
Example #19
Source File: RealmHelper.java    From EMQ-Android-Toolkit with Apache License 2.0 5 votes vote down vote up
public <T extends RealmObject> void deleteTopicMessage(Class<T> clazz, String topic) {
    final RealmResults results = mRealm.where(clazz)
            .equalTo("topic", topic)
            .findAll();
    mRealm.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {
            results.deleteAllFromRealm();

        }
    });
}
 
Example #20
Source File: RealmHelper.java    From EMQ-Android-Toolkit with Apache License 2.0 5 votes vote down vote up
public <T extends RealmObject> void deleteAll(Class<T> clazz) {
    final RealmResults results = mRealm.where(clazz).findAll();
    mRealm.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {
            results.deleteAllFromRealm();

        }
    });
}
 
Example #21
Source File: Database.java    From aptoide-client-v8 with GNU General Public License v3.0 5 votes vote down vote up
private <E extends RealmObject> Observable<E> copyFromRealm(E object) {
  return Observable.just(object)
      .filter(data -> data.isLoaded())
      .filter(realmObject -> realmObject.isValid())
      .map(realmObject -> get().copyFromRealm(realmObject))
      .observeOn(Schedulers.io());
}
 
Example #22
Source File: Database.java    From aptoide-client-v8 with GNU General Public License v3.0 5 votes vote down vote up
public <E extends RealmObject> Observable<E> findFirst(RealmQuery<E> query) {
  return Observable.just(query.findFirst())
      .filter(realmObject -> realmObject != null)
      .flatMap(realmObject -> realmObject.<E>asObservable().unsubscribeOn(
          RealmSchedulers.getScheduler()))
      .flatMap(realmObject -> copyFromRealm(realmObject))
      .defaultIfEmpty(null);
}
 
Example #23
Source File: ApiStorage.java    From ApiClient with Apache License 2.0 5 votes vote down vote up
@Override
public <Item extends RealmObject> RealmResults<Item> getItems(Class<Item> objectClass, String sortedFieldName, List<Integer> ids) {
    RealmQuery<Item> query = getQuery(objectClass);
    for (int i = 0; i < ids.size() - 1; i++) {
        query = query.equalTo(sortedFieldName, ids.get(i)).or();
    }
    query = query.equalTo(sortedFieldName, ids.get(ids.size() - 1));

    return query.findAllSortedAsync(sortedFieldName);
}
 
Example #24
Source File: ApiObserver.java    From ApiClient with Apache License 2.0 4 votes vote down vote up
@Override
public <Item extends RealmObject> Observable<List<Item>> getApiObservable(Observable<List<Item>> api, Class<Item> realmClass) {
    return getApiObservable(api, realmClass, null);
}
 
Example #25
Source File: Database.java    From aptoide-client-v8 with GNU General Public License v3.0 4 votes vote down vote up
public <E extends RealmObject> Observable<Long> count(RealmQuery<E> query) {
  return Observable.just(query.count())
      .flatMap(count -> Observable.just(count)
          .unsubscribeOn(RealmSchedulers.getScheduler()))
      .defaultIfEmpty(0L);
}
 
Example #26
Source File: Database.java    From aptoide-client-v8 with GNU General Public License v3.0 4 votes vote down vote up
public <E extends RealmObject> Observable<List<E>> copyFromRealm(RealmResults<E> results) {
  return Observable.just(results)
      .filter(data -> data.isLoaded())
      .map(realmObjects -> get().copyFromRealm(realmObjects))
      .observeOn(Schedulers.io());
}
 
Example #27
Source File: Database.java    From aptoide-client-v8 with GNU General Public License v3.0 4 votes vote down vote up
public <E extends RealmObject> Observable<List<E>> getAll(Class<E> clazz) {
  return getRealm().flatMap(realm -> realm.where(clazz)
      .findAll().<List<E>>asObservable().unsubscribeOn(RealmSchedulers.getScheduler()))
      .flatMap(results -> copyFromRealm(results));
}
 
Example #28
Source File: RealmHelper.java    From EMQ-Android-Toolkit with Apache License 2.0 4 votes vote down vote up
public <T extends RealmObject> T queryFirst(Class<T> clazz) {
    return mRealm.where(clazz).findFirst();
}
 
Example #29
Source File: RealmCandleData.java    From JNChartDemo with Apache License 2.0 4 votes vote down vote up
public RealmCandleData(RealmResults<? extends RealmObject> result, String xValuesField, List<ICandleDataSet> dataSets) {
    super(RealmUtils.toXVals(result, xValuesField), dataSets);
}
 
Example #30
Source File: RealmPieData.java    From JNChartDemo with Apache License 2.0 4 votes vote down vote up
public RealmPieData(RealmResults<? extends RealmObject> result, String xValuesField, IPieDataSet dataSet) {
    super(RealmUtils.toXVals(result, xValuesField), dataSet);
}