@Override public Flowable<Optional<List<T>>> getAll(List<T> items) { Realm realm = Realm.getDefaultInstance(); RealmQuery<T> query = realm.where(clazz); if(items.size() > 0){ String paramName = items.get(0).getUniqueIdentifierName(); Integer[] keys = new Integer[items.size()]; for(int i = 0; i < items.size(); i++){ keys[i] = items.get(i).getUniqueIdentifier(); } query.in(paramName, keys); } List<T> copies = realm.copyFromRealm(query.findAll()); realm.close(); return Flowable.just(Optional.wrap(copies)); }
/** * Get one with a specific filter object * @param filter * @return */ @Override public Flowable<Optional<T>> getOne(Filter filter, SortingMode sortingMode) { Realm realm = Realm.getDefaultInstance(); RealmQuery<T> query = realm.where(clazz); query = filterToQuery(filter, query); T item = null; if (sortingMode != null) { RealmResults<T> items = applySortingMode(sortingMode, query); if (!items.isEmpty()) { item = items.first(); } } else { item = query.findFirst(); } T copy = null; if (item != null) { copy = realm.copyFromRealm(item); } realm.close(); return Flowable.just(Optional.wrap(copy)); }
private RealmResults<Contact> searchContacts() { Contact c = Parcels.unwrap(getIntent().getParcelableExtra(EXTRA_SEARCH_FILTERS)); realm = Realm.getDefaultInstance(); RealmQuery<Contact> query = realm.where(Contact.class); query.contains(LASTNAME, c.lastName, Case.INSENSITIVE) .or() .contains(FIRSTNAME, c.lastName, Case.INSENSITIVE); if (!c.owner) { query.equalTo(OWNER, false); } if (!c.cornac) { query.equalTo(CORNAC, false); } if (!c.vet) { query.equalTo(VET, false); } return query.findAll(); }
/** * Get all for a specific filters * @return */ @Override public Flowable<Optional<List<T>>> getAll(Filter filter, SortingMode sortingMode) { Realm realm = Realm.getDefaultInstance(); RealmQuery<T> query = realm.where(clazz); query = filterToQuery(filter, query); RealmResults<T> items; if(sortingMode != null && sortingMode.entries.size() > 0) { items = applySortingMode(sortingMode, query); } else { items = query.findAll(); } List<T> copies = realm.copyFromRealm(items); realm.close(); return Flowable.just(Optional.wrap(copies)); }
private RealmResults<Elephant> searchElephants() { Elephant e = Parcels.unwrap(getIntent().getParcelableExtra(EXTRA_SEARCH_ELEPHANT)); RealmQuery<Elephant> query = realm.where(Elephant.class); query.contains(NAME, e.name, Case.INSENSITIVE); if (e.chips1 != null) { query.contains(CHIPS1, e.chips1, Case.INSENSITIVE); } if (!e.male) { query.equalTo(MALE, false); } if (!e.female) { query.equalTo(FEMALE, false); } return query.findAll(); }
public static RealmResults<Trip> getMissingConfirmTrips(Realm realm) { RealmResults<StationUse> uses = realm.where(StationUse.class) .greaterThan("entryDate", new Date(new Date().getTime() - TimeUnit.DAYS.toMillis(7))).findAll().where() .equalTo("type", "NETWORK_ENTRY").or().equalTo("type", "VISIT").findAll(); // now we have all station uses that **might** be part of editable trips // get all trips that contain these uses and which are yet to be confirmed RealmQuery<Trip> tripsQuery = realm.where(Trip.class); if (uses.size() > 0) { // first item detached from the others because otherwise "Missing left-hand side of OR" might happen // https://github.com/realm/realm-java/issues/1014#issuecomment-107235374 tripsQuery = tripsQuery.equalTo("userConfirmed", false).equalTo("path.station.id", uses.get(0).getStation().getId()).equalTo("path.entryDate", uses.get(0).getEntryDate()); for (int i = 1; i < uses.size(); i++) { tripsQuery = tripsQuery.or().equalTo("userConfirmed", false).equalTo("path.station.id", uses.get(i).getStation().getId()).equalTo("path.entryDate", uses.get(i).getEntryDate()); } return tripsQuery.findAll(); } else { // realm is just terrible. not only is it hard to do a proper WHERE ... IN ... query, it's also hard to generate an empty result set. // https://github.com/realm/realm-java/issues/1862 // https://github.com/realm/realm-java/issues/1575 // https://github.com/realm/realm-java/issues/4011 return tripsQuery.equalTo("id", "NEVER_BE_TRUE").findAll(); } }
void selectData(String name, String phoneNum, String sex, String education, final MaterialDialog materialDialog) { Logger.t(TAG).i("name=" + name + "\nphoneNum=" + phoneNum + "\nsex=" + sex + "\neducation=" + education); Realm realm = RealmManager.getRealm(); RealmResults<User> userRealmResults; RealmQuery<User> realmQuery = realm.where(User.class) .contains("name", name) .contains("phoneNum", phoneNum); if (!TextUtils.isEmpty(sex)) { realmQuery.equalTo("sex", sex); } if (!TextUtils.isEmpty(education)) { realmQuery.equalTo("mExtendBeanRealmList.value", education); } userRealmResults = realmQuery.findAll(); loadLatestData(userRealmResults); materialDialog.dismiss(); }
@Override public List<Item> getItems(Realm realm, boolean onlyUnread) { // Get all feeds belonging to Folder treeItem List<Feed> feeds = getFeeds(realm, onlyUnread); RealmQuery<Item> query = null; if(feeds != null && feeds.size() > 0) { // Find all items belonging to any feed from this folder Iterator<Feed> feedIterator = feeds.iterator(); query = realm.where(Item.class).beginGroup() .equalTo(Item.FEED_ID, feedIterator.next().getId()); while (feedIterator.hasNext()) { query.or().equalTo(Item.FEED_ID, feedIterator.next().getId()); } query.endGroup(); if(onlyUnread) query.equalTo(Item.UNREAD, true); } return query != null ? query.distinct(Item.FINGERPRINT) : null; }
@NonNull public static List<Folder> getAll(Realm realm, boolean onlyUnread) { RealmQuery<Folder> query = null; if(onlyUnread) { RealmResults<Feed> unreadFeeds = realm.where(Feed.class).greaterThan(Feed.UNREAD_COUNT, 0).notEqualTo(Feed.FOLDER_ID, 0).findAll(); if(unreadFeeds.size() > 0) { Iterator<Feed> feedIterator = unreadFeeds.iterator(); query = realm.where(Folder.class) .equalTo(Folder.ID, feedIterator.next().getFolderId()); while (feedIterator.hasNext()) { query.or().equalTo(Folder.ID, feedIterator.next().getFolderId()); } } } else { query = realm.where(Folder.class); } return query != null ? query.findAllSorted(Folder.NAME, Sort.ASCENDING) : Collections.emptyList(); }
@Nullable @Override public T findOneSync(ID id) { Realm realm = null; try { realm = repositoryConfiguration.getRealmProvider().provideRealm(); RealmQuery<T> query = realm.where(entityClass); T entity = idSearch.searchId(query, idFieldName, id) .findFirst(); return entity == null ? null : realm.copyFromRealm(entity); } finally { if (realm != null) { realm.close(); } } }
@Override public void deleteSync(ID id) { Realm realm = null; try { realm = repositoryConfiguration.getRealmProvider().provideRealm(); realm.beginTransaction(); RealmQuery<T> query = realm.where(entityClass); T entity = idSearch.searchId(query, idFieldName, id) .findFirst(); if (entity != null) { entity.deleteFromRealm(); } realm.commitTransaction(); } finally { if (realm != null) { realm.close(); } } }
void getUnsentMessages(final Conversation conversation, final GetCallback<List<Message>> callback) { RealmStore.QueryBuilder<MessageCacheObject> queryBuilder = new RealmStore.QueryBuilder<MessageCacheObject>() { @Override public RealmQuery<MessageCacheObject> buildQueryFrom(RealmQuery<MessageCacheObject> baseQuery) { RealmQuery<MessageCacheObject> query = baseQuery .equalTo(KEY_CONVERSATION_ID, conversation.getId()) .isNotNull(KEY_SEND_DATE) .beginGroup() .equalTo(KEY_ALREADY_SYNC_TO_SERVER, false) .or() .equalTo(KEY_FAIL, true) .endGroup(); return query; } }; if (callback != null) { this.store.getMessages(queryBuilder, -1, "creationDate", new RealmStore.ResultCallback<Message[]>() { @Override public void onResultGet(Message[] messages) { callback.onSuccess(Arrays.asList(messages)); } }); } }
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(); } } }
@Override public <T extends RealmModel> RealmQuery<T> transform(RealmQuery<T> realmQuery, Condition condition) { // Checks. if (!condition.isValid()) throw new IllegalArgumentException("Condition isn't valid."); if (condition.getType() != Condition.Type.NORMAL) throw new IllegalArgumentException("Condition type is not NORMAL."); // Get data from Conditions. String field = condition.getField(); FieldType fieldType = condition.getFieldType(); Object[] args = condition.getArgs(); // Use different methods based on field type. if (FieldType.DATE == fieldType) return realmQuery.lessThan(field, (Date) args[0]); else if (FieldType.DOUBLE == fieldType) return realmQuery.lessThan(field, (Double) args[0]); else if (FieldType.FLOAT == fieldType) return realmQuery.lessThan(field, (Float) args[0]); else if (FieldType.INTEGER == fieldType) return realmQuery.lessThan(field, (Integer) args[0]); else if (FieldType.LONG == fieldType) return realmQuery.lessThan(field, (Long) args[0]); else throw new IllegalArgumentException(String.format("Illegal argument type \"%s\".", fieldType.getTypeName())); }
@Override public <T extends RealmModel> RealmQuery<T> transform(RealmQuery<T> realmQuery, Condition condition) { // Checks. if (!condition.isValid()) throw new IllegalArgumentException("Condition isn't valid."); if (condition.getType() != Condition.Type.NORMAL) throw new IllegalArgumentException("Condition type is not NORMAL."); // Get data from Conditions. String field = condition.getField(); FieldType fieldType = condition.getFieldType(); Object[] args = condition.getArgs(); // Use different methods based on field type. if (FieldType.STRING == fieldType) return realmQuery.contains(field, (String) args[0]); else throw new IllegalArgumentException(String.format("Illegal argument type \"%s\".", fieldType.getTypeName())); }
@Override public <T extends RealmModel> RealmQuery<T> transform(RealmQuery<T> realmQuery, Condition condition) { // Checks. if (!condition.isValid()) throw new IllegalArgumentException("Condition isn't valid."); if (condition.getType() != Condition.Type.NORMAL) throw new IllegalArgumentException("Condition type is not NORMAL."); // Get data from Conditions. String field = condition.getField(); FieldType fieldType = condition.getFieldType(); Object[] args = condition.getArgs(); // Use different methods based on field type. if (FieldType.DATE == fieldType) return realmQuery.lessThanOrEqualTo(field, (Date) args[0]); else if (FieldType.DOUBLE == fieldType) return realmQuery.lessThanOrEqualTo(field, (Double) args[0]); else if (FieldType.FLOAT == fieldType) return realmQuery.lessThanOrEqualTo(field, (Float) args[0]); else if (FieldType.INTEGER == fieldType) return realmQuery.lessThanOrEqualTo(field, (Integer) args[0]); else if (FieldType.LONG == fieldType) return realmQuery.lessThanOrEqualTo(field, (Long) args[0]); else throw new IllegalArgumentException(String.format("Illegal argument type \"%s\".", fieldType.getTypeName())); }
@Override public <T extends RealmModel> RealmQuery<T> transform(RealmQuery<T> realmQuery, Condition condition) { // Checks. if (!condition.isValid()) throw new IllegalArgumentException("Condition isn't valid."); if (condition.getType() != Condition.Type.NORMAL) throw new IllegalArgumentException("Condition type is not NORMAL."); // Get data from Conditions. String field = condition.getField(); FieldType fieldType = condition.getFieldType(); Object[] args = condition.getArgs(); if (FieldType.STRING == fieldType) return realmQuery.beginsWith(field, (String) args[0]); else throw new IllegalArgumentException(String.format("Illegal argument type \"%s\".", fieldType.getTypeName())); }
@Override public <T extends RealmModel> RealmQuery<T> transform(RealmQuery<T> realmQuery, Condition condition) { // Checks. if (!condition.isValid()) throw new IllegalArgumentException("Condition isn't valid."); if (condition.getType() != Condition.Type.NORMAL) throw new IllegalArgumentException("Condition type is not NORMAL."); // Get data from Conditions. String field = condition.getField(); FieldType fieldType = condition.getFieldType(); Object[] args = condition.getArgs(); // Use different methods based on field type. if (FieldType.DATE == fieldType) return realmQuery.between(field, (Date) args[0], (Date) args[1]); else if (FieldType.DOUBLE == fieldType) return realmQuery.between(field, (Double) args[0], (Double) args[1]); else if (FieldType.FLOAT == fieldType) return realmQuery.between(field, (Float) args[0], (Float) args[1]); else if (FieldType.INTEGER == fieldType) return realmQuery.between(field, (Integer) args[0], (Integer) args[1]); else if (FieldType.LONG == fieldType) return realmQuery.between(field, (Long) args[0], (Long) args[1]); else throw new IllegalArgumentException(String.format("Illegal argument type \"%s\".", fieldType.getTypeName())); }
@Override public <T extends RealmModel> RealmQuery<T> transform(RealmQuery<T> realmQuery, Condition condition) { // Checks. if (!condition.isValid()) throw new IllegalArgumentException("Condition isn't valid."); if (condition.getType() != Condition.Type.NORMAL) throw new IllegalArgumentException("Condition type is not NORMAL."); // Get data from Conditions. String field = condition.getField(); FieldType fieldType = condition.getFieldType(); Object[] args = condition.getArgs(); // Use different methods based on field type. if (FieldType.BOOLEAN == fieldType) return realmQuery.equalTo(field, (Boolean) args[0]); else if (FieldType.DATE == fieldType) return realmQuery.equalTo(field, (Date) args[0]); else if (FieldType.DOUBLE == fieldType) return realmQuery.equalTo(field, (Double) args[0]); else if (FieldType.FLOAT == fieldType) return realmQuery.equalTo(field, (Float) args[0]); else if (FieldType.INTEGER == fieldType) return realmQuery.equalTo(field, (Integer) args[0]); else if (FieldType.LONG == fieldType) return realmQuery.equalTo(field, (Long) args[0]); else if (FieldType.SHORT == fieldType) return realmQuery.equalTo(field, (Short) args[0]); else if (FieldType.STRING == fieldType) return realmQuery.equalTo(field, (String) args[0]); else throw new IllegalArgumentException(String.format("Illegal argument type \"%s\".", fieldType.getTypeName())); }
@Override public <T extends RealmModel> RealmQuery<T> transform(RealmQuery<T> realmQuery, Condition condition) { // Checks. if (!condition.isValid()) throw new IllegalArgumentException("Condition isn't valid."); if (condition.getType() != Condition.Type.NORMAL) throw new IllegalArgumentException("Condition type is not NORMAL."); // Get data from Conditions. String field = condition.getField(); FieldType fieldType = condition.getFieldType(); Object[] args = condition.getArgs(); if (FieldType.STRING == fieldType) return realmQuery.endsWith(field, (String) args[0]); else throw new IllegalArgumentException(String.format("Illegal argument type \"%s\".", fieldType.getTypeName())); }
@Override public <T extends RealmModel> RealmQuery<T> transform(RealmQuery<T> realmQuery, Condition condition) { // Checks. if (!condition.isValid()) throw new IllegalArgumentException("Condition isn't valid."); if (condition.getType() != Condition.Type.NORMAL) throw new IllegalArgumentException("Condition type is not NORMAL."); // Get data from Conditions. String field = condition.getField(); FieldType fieldType = condition.getFieldType(); Object[] args = condition.getArgs(); // Use different methods based on field type. if (FieldType.BOOLEAN == fieldType) return realmQuery.notEqualTo(field, (Boolean) args[0]); else if (FieldType.DATE == fieldType) return realmQuery.notEqualTo(field, (Date) args[0]); else if (FieldType.DOUBLE == fieldType) return realmQuery.notEqualTo(field, (Double) args[0]); else if (FieldType.FLOAT == fieldType) return realmQuery.notEqualTo(field, (Float) args[0]); else if (FieldType.INTEGER == fieldType) return realmQuery.notEqualTo(field, (Integer) args[0]); else if (FieldType.LONG == fieldType) return realmQuery.notEqualTo(field, (Long) args[0]); else if (FieldType.SHORT == fieldType) return realmQuery.notEqualTo(field, (Short) args[0]); else if (FieldType.STRING == fieldType) return realmQuery.notEqualTo(field, (String) args[0]); else throw new IllegalArgumentException(String.format("Illegal argument type \"%s\".", fieldType.getTypeName())); }
@Override public <T extends RealmModel> RealmQuery<T> transform(RealmQuery<T> realmQuery, Condition condition) { // Checks. if (!condition.isValid()) throw new IllegalArgumentException("Condition isn't valid."); if (condition.getType() != Condition.Type.NORMAL) throw new IllegalArgumentException("Condition type is not NORMAL."); // Get data from Conditions. String field = condition.getField(); FieldType fieldType = condition.getFieldType(); Object[] args = condition.getArgs(); // Use different methods based on field type. if (FieldType.DATE == fieldType) return realmQuery.greaterThan(field, (Date) args[0]); else if (FieldType.DOUBLE == fieldType) return realmQuery.greaterThan(field, (Double) args[0]); else if (FieldType.FLOAT == fieldType) return realmQuery.greaterThan(field, (Float) args[0]); else if (FieldType.INTEGER == fieldType) return realmQuery.greaterThan(field, (Integer) args[0]); else if (FieldType.LONG == fieldType) return realmQuery.greaterThan(field, (Long) args[0]); else throw new IllegalArgumentException(String.format("Illegal argument type \"%s\".", fieldType.getTypeName())); }
@Override public <T extends RealmModel> RealmQuery<T> transform(RealmQuery<T> realmQuery, Condition condition) { // Checks. if (!condition.isValid()) throw new IllegalArgumentException("Condition isn't valid."); if (condition.getType() != Condition.Type.NORMAL) throw new IllegalArgumentException("Condition type is not NORMAL."); // Get data from Conditions. String field = condition.getField(); FieldType fieldType = condition.getFieldType(); Object[] args = condition.getArgs(); // Use different methods based on field type. if (FieldType.DATE == fieldType) return realmQuery.greaterThanOrEqualTo(field, (Date) args[0]); else if (FieldType.DOUBLE == fieldType) return realmQuery.greaterThanOrEqualTo(field, (Double) args[0]); else if (FieldType.FLOAT == fieldType) return realmQuery.greaterThanOrEqualTo(field, (Float) args[0]); else if (FieldType.INTEGER == fieldType) return realmQuery.greaterThanOrEqualTo(field, (Integer) args[0]); else if (FieldType.LONG == fieldType) return realmQuery.greaterThanOrEqualTo(field, (Long) args[0]); else throw new IllegalArgumentException(String.format("Illegal argument type \"%s\".", fieldType.getTypeName())); }
/** * Query the database * * @param passedQuery The query to search with. If null is passed here, it will build a default * query in which it searches everything. * @param myClass Class (table) that is being searched * @param <T> T extends RealmModel (RealmResults) * @return An object from the database (one from that table) */ public <T extends RealmModel> Object queryDatabaseSingle(RealmQuery<T> passedQuery, Class myClass) { Realm realm = DatabaseUtilities.buildRealm(this.realmConfiguration); if (passedQuery == null) { passedQuery = this.buildRealmQuery(realm, myClass); } //Start transaction RealmResults<T> results = passedQuery.findAll(); if (results != null) { Object object = results.get(0); return object; } return null; }
/** * Query the database * * @param passedQuery The query to search with. If null is passed here, it will build a default * query in which it searches everything. * @param myClass Class (table) that is being searched * @param <T> T extends RealmModel (RealmResults) * @return An list of objects from the database (all in that table) */ public <T extends RealmModel> List<Object> queryDatabaseList(RealmQuery<T> passedQuery, Class myClass) { Realm realm = DatabaseUtilities.buildRealm(this.realmConfiguration); if (passedQuery == null) { passedQuery = this.buildRealmQuery(realm, myClass); } //Start transaction RealmResults<T> results = passedQuery.findAll(); List<Object> objects = new ArrayList<>(); //<T extends RealmModel> if (results != null) { for (T t : results) { Object object = (Object) t; if (object != null) { objects.add(object); } } } return objects; }
public static List<Store> getStoreInBounds(double minLat, double minLng, double maxLat, double maxLng) { Realm realm = Realm.getDefaultInstance(); List<Store> storeList = new ArrayList<>(); // Build the query looking at all users: RealmQuery<StoreEntity> query = realm.where(StoreEntity.class); // Add query conditions: query.between("latitude", minLat, maxLat); query.between("longitude", minLng, maxLng); // Execute the query: RealmResults<StoreEntity> result = query.findAll(); for (int i = 0; i < result.size(); i++) { Store store = new Store(result.get(i)); storeList.add(store); } realm.close(); return storeList; }
private RealmResults<BeaconEventRealm> queryStoredBeaconEvents(Realm realm, List<OrchextraBeacon> beacons) { RealmQuery<BeaconEventRealm> query = realm.where(BeaconEventRealm.class); for (int i = 0; i < beacons.size(); i++) { if (i > 0) { query = query.or(); } query = query.equalTo(BeaconEventRealm.CODE_FIELD_NAME, beacons.get(i).getCode()) .equalTo(BeaconEventRealm.DISTANCE_FIELD_NAME, beacons.get(i).getBeaconDistance().getStringValue()); } return query.findAll(); }
public static <E extends RealmObject> RealmResults<E> queryEqualTo(final Realm realm, final Class<E> eClass, final String field, final boolean... filters) { try { final RealmQuery<E> realmQuery = realm.where(eClass); int i = 0; for (final boolean filter : filters) { if (i != 0) realmQuery.or(); realmQuery.equalTo(field, filter); i++; } return getRealmResults(realmQuery); } catch (Exception e) { Log.d(TAG, "", e); return null; } }
public static <E extends RealmObject> RealmResults<E> queryEqualTo(final Realm realm, final Class<E> eClass, final String field, final String... filters) { try { final RealmQuery<E> realmQuery = realm.where(eClass); int i = 0; for (final String filter : filters) { if (i != 0) realmQuery.or(); realmQuery.equalTo(field, filter); i++; } return getRealmResults(realmQuery); } catch (Exception e) { Log.d(TAG, "", e); return null; } }
public static <E extends RealmObject> RealmResults<E> queryEqualTo(final Realm realm, final Class<E> eClass, final String field, final int... filters) { try { final RealmQuery<E> realmQuery = realm.where(eClass); int i = 0; for (final int filter : filters) { if (i != 0) realmQuery.or(); realmQuery.equalTo(field, filter); i++; } return getRealmResults(realmQuery); } catch (Exception e) { Log.d(TAG, "", e); return null; } }
public static <E extends RealmObject> RealmResults<E> queryContains(final Realm realm, final Class<E> eClass, final String field, final String... filters) { try { final RealmQuery<E> realmQuery = realm.where(eClass); int i = 0; for (final String filter : filters) { if (i != 0) realmQuery.or(); realmQuery.contains(field, filter, false); i++; } return getRealmResults(realmQuery); } catch (Exception e) { Log.d(TAG, "", e); return null; } }
public void loadFavoriteMovies(final ReadCallback<List<Movie>> callback) { readAllAsync(new ReadAction<Movie>() { @NonNull @Override public RealmQuery<Movie> getQuery(@NonNull Realm realm) { return realm.where(Movie.class).equalTo("isFavorite", true); } @Override public void onResults(RealmResults<Movie> results) { List<Movie> favorites = new ArrayList<>(results.size()); for (Movie movie : results) { favorites.add(AppUtil.copy(movie, Movie.class)); } callback.done(favorites); } }); }
public void loadMovie(final int id, final ReadCallback<Movie> callback) { if (BuildConfig.DEBUG) { Log.d(TAG, "[READ ] Load Movie with id = " + id); } readAllAsync(new ReadAction<Movie>() { @NonNull @Override public RealmQuery<Movie> getQuery(@NonNull Realm realm) { return realm.where(Movie.class).equalTo("id", id); } @Override public void onResults(RealmResults<Movie> results) { if (!results.isEmpty()) { if (BuildConfig.DEBUG) { Log.d(TAG, "[READ ] Success: Movie with id = " + id); } callback.done(AppUtil.copy(results.first(), Movie.class)); } else { callback.failed(new RuntimeException("No Movie found with id = " + id)); } } }); }
private <T extends RealmObject> void readAllAsync(@NonNull final ReadAction<T> readAction) { Realm realm = null; try { realm = Realm.getDefaultInstance(); RealmQuery<T> query = readAction.getQuery(realm); final RealmResults<T> results = query.findAllAsync(); results.addChangeListener(new RealmChangeListener() { @Override public void onChange() { results.removeChangeListener(this); readAction.onResults(results); } }); } finally { if (realm != null) { realm.close(); } } }
public void testSave1() { // 1. Realm Setup RealmConfiguration realmConfig1 = new RealmConfiguration.Builder(getContext()).name("1testSave1.realm").build(); Realm.deleteRealm(realmConfig1); Realm realm1 = Realm.getInstance(realmConfig1); // 2. Object Setup Dog dog1 = new Dog(); dog1.setName("Kitty1"); // 3. RoyalTransaction.save() RoyalTransaction.save(realm1, dog1); // 4. Query RealmQuery<Dog> query = realm1.where(Dog.class); RealmResults<Dog> dogs = query.findAll(); // 5. Assert assertNotNull(dogs); assertEquals(1, dogs.size()); assertEquals("Kitty1", dogs.get(0).getName()); // 6. Realm Close realm1.close(); }
public void testSave5() { // 1. Realm Setup RealmConfiguration realmConfig1 = new RealmConfiguration.Builder(getContext()).name("1testSave5.realm").build(); Realm.deleteRealm(realmConfig1); Realm realm1 = Realm.getInstance(realmConfig1); // 2. Object Setup Dog dog1 = new Dog(); dog1.setName("Kitty1"); // 3. RoyalTransaction.save() RoyalTransaction.save(realm1); // 4. Query RealmQuery<Dog> query = realm1.where(Dog.class); RealmResults<Dog> dogs = query.findAll(); // 5. Assert assertNotNull(dogs); assertEquals(0, dogs.size()); // 6. Realm Close realm1.close(); }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if(savedInstanceState!=null) { selectedSensor = savedInstanceState.getInt(Constants.SELECTED_SENSOR); } else { Bundle args = getArguments(); selectedSensor = args.getInt(Constants.SELECTED_SENSOR); } realm = Realm.getInstance(getActivity()); RealmQuery<SensorModel> query = realm.where(SensorModel.class); query.equalTo("sensorType",selectedSensor); RealmResults<SensorModel> result = query.findAllSorted("timestamp",RealmResults.SORT_ORDER_DESCENDING); if(result.size() > 0) { recordData = result.subList(0,result.size()); } else { recordData = new ArrayList<SensorModel>(0); } }
@Override public List<Record> fetch() { List<Record> records = new ArrayList<>(); Realm realm = null; try { realm = Realm.getInstance(realmConfig); RealmQuery<RecordObject> query = realm.where(RecordObject.class); RealmResults<RecordObject> results = query.findAll(); for (RecordObject obj : results) { records.add(new Record(Utils.forName(obj.getClassName()), obj.getBody())); } return records; } finally { if (realm != null) realm.close(); } }
@Override public RealmList<Person> getPersonFromPersonPOJO(List<PersonPOJO> people) { RealmList<Person> listOfPersons = new RealmList<>(); if (people == null || people.size() == 0) { return listOfPersons; } RealmQuery<Person> query = realm.where(Person.class) .equalTo(Person.Column.ID, people.get(0).getId()); for (int i = 1; i < people.size(); i++) { query = query.or().equalTo(Person.Column.ID, people.get(i).getId()); } RealmResults<Person> results = query.findAll(); for (int i = 0; i < results.size(); i++) { listOfPersons.add(results.get(i)); } return listOfPersons; }
/** * Liefert eine List der Lehrveranstaltungen des übergebenen Tages und Ds für einen Raum * * @param realm aktuelle Datenbankverbindung * @param calendar Tag für welchen die Lehrveranstaltungen gelistet werden soll * @param room Raum für welchen die Belegung angezeigt werden soll * @param ds Zeit in welcher die Lehrveranstaltungen stattfinden sollen * @param filterCurrentWeek Nur Lehrveranstaltungen der aktuellen Kalenderwoche zurückgeben * @return Liste von passenden Lehrveranstaltungen */ public static RealmResults<LessonRoom> getLessonsByDateAndDs(@NonNull final Realm realm, @NonNull final Calendar calendar, @NonNull final String room, final int ds, final boolean filterCurrentWeek) { final int dsIndex = ds > 0 ? ds - 1 : 0; final RealmQuery<LessonRoom> realmQuery = realm.where(LessonRoom.class) .equalTo(Const.database.LessonRoom.ROOM, room) .equalTo(Const.database.Lesson.DAY, calendar.get(Calendar.DAY_OF_WEEK) - 1) // Nach Kalenderwoche einschränken .beginGroup() .equalTo(Const.database.Lesson.WEEK, getWeekTyp(calendar.get(Calendar.WEEK_OF_YEAR))) .or().equalTo(Const.database.Lesson.WEEK, 0) .endGroup() // Vor dem Ende dieser DS beginnen und länger gehen als DS startet .lessThan(Const.database.Lesson.BEGIN_TIME, Const.Timetable.endDS[dsIndex]) .greaterThan(Const.database.Lesson.END_TIME, Const.Timetable.beginDS[dsIndex]); if (filterCurrentWeek) { realmQuery.beginGroup() .isEmpty(Const.database.Lesson.WEEKS_ONLY) .or().equalTo(Const.database.Lesson.WEEKS_ONLY + ".weekOfYear", calendar.get(Calendar.WEEK_OF_YEAR)) .endGroup(); } return realmQuery.findAll(); }