Java Code Examples for io.realm.RealmResults.first()

The following are Jave code examples for showing how to use first() of the io.realm.RealmResults class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: store2realm   File: RealmDao.java   Source Code and License Vote up 7 votes
/**
 * 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));
}
 
Example 2
Project: RealmTasks   File: TaskActivity.java   Source Code and License Vote up 6 votes
private void updateList(RealmResults<TaskList> results)
{
    // Use `findAllAsync` because change listeners are not called when items are deleted and using `findFirst()`
    // See https://github.com/realm/realm-java/issues/3138
    if (results.size() > 0)
    {
        TaskList element = results.first();
        setTitle(element.getText());
        if (adapter == null)
        {
            adapter = new TaskAdapter(TaskActivity.this, element.getItems());
            touchHelper = new TouchHelper(new Callback(), adapter);
            touchHelper.attachToRecyclerView(recyclerView);
        }
    }
    else
    {
        setTitle(getString(R.string.title_deleted));
    }
}
 
Example 3
Project: OpenLibre   File: ReadingData.java   Source Code and License Vote up 6 votes
private GlucoseData makeGlucoseData(Realm realmProcessedData, int glucoseLevelRaw, int ageInSensorMinutes, long dataDate) {
    // if this data point has been read from this sensor before, reuse the object form the database, instead of changing the old data
    RealmResults<GlucoseData> previousGlucoseData = realmProcessedData.where(GlucoseData.class)
            .equalTo(GlucoseData.ID, GlucoseData.generateId(sensor, ageInSensorMinutes, false, glucoseLevelRaw)).findAll();

    // check if a valid previous data point was found
    if (!previousGlucoseData.isEmpty()) {
        if (previousGlucoseData.first().getGlucoseLevelRaw() == glucoseLevelRaw) {
            return previousGlucoseData.first();
        }
        // if the old value does not equal the new one and the sensor has been running for more than three hours, there is an error in the data
        if (ageInSensorMinutes > 3 * minSensorAgeInMinutes) {
            Log.e("OpenLibre::ReadingData", "error in glucose level raw:" + previousGlucoseData.first().getGlucoseLevelRaw() + " != " + glucoseLevelRaw
                    + " for glucose data with id: " + previousGlucoseData.first().getId());
            history.clear();
            trend.clear();
            return null;
        }
    }
    return new GlucoseData(sensor, ageInSensorMinutes, timezoneOffsetInMinutes, glucoseLevelRaw, false, dataDate);
}
 
Example 4
Project: GitHub   File: RealmAllocBenchmarks.java   Source Code and License Vote up 5 votes
@Benchmark
public void createObjects(long reps) {
    RealmResults<AllTypes> results = realm.where(AllTypes.class).findAll();
    for (long i = 0; i < reps; i++) {
        results.first();
    }
}
 
Example 5
Project: OpenLibre   File: SensorStatusFragment.java   Source Code and License Vote up 5 votes
public void setupUi(View view) {
    Realm realmProcessedData = Realm.getInstance(realmConfigProcessedData);
    RealmResults<SensorData> sensorDataResults = realmProcessedData.where(SensorData.class).
            findAllSorted(START_DATE, Sort.DESCENDING);

    TextView sensorId = (TextView) view.findViewById(R.id.tv_sensor_id_value);
    TextView sensorStartDate = (TextView) view.findViewById(R.id.tv_sensor_start_date_value);
    TextView sensorEndsIn = (TextView) view.findViewById(R.id.tv_sensor_ends_in_value);

    if (sensorDataResults.size() == 0) {
        sensorId.setText(getResources().getString(R.string.no_sensor_registered));
        sensorStartDate.setText("");
        sensorEndsIn.setText("");
    } else {
        SensorData sensorData = sensorDataResults.first();
        sensorId.setText(sensorData.getTagId());
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT);
        sensorStartDate.setText(dateFormat.format(new Date(sensorData.getStartDate())));
        long timeLeft = sensorData.getTimeLeft();
        if (timeLeft >= TimeUnit.MINUTES.toMillis(1L)) {
            sensorEndsIn.setText(getDurationBreakdown(getResources(), sensorData.getTimeLeft()));
        } else {
            sensorEndsIn.setText(getResources().getString(R.string.sensor_expired));
        }
    }
    realmProcessedData.close();
}