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

The following are Jave code examples for showing how to use isEmpty() 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.
+ Save this method
Example 1
Project: store2realm   File: RealmDao.java   View Source Code 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: CurrentActivity   File: CurrentActivity.java   View Source Code Vote up 6 votes
@Nullable
public List<CurrentActivityData> loadCurrentActivityData() {
    RealmResults<CurrentActivityData> results = realm.where(CurrentActivityData.class).findAll();
    if (!results.isEmpty()) {
        List<CurrentActivityData> data = new ArrayList<>();
        int resultsNum = results.size() - 1;
        if (resultsNum >= 10) {
            for (int i = resultsNum; i >= resultsNum - 10; i--)
                data.add(results.get(i));
        } else {
            for (int i = resultsNum; i >= 0; i--) data.add(results.get(i));
        }
        return data;
    }
    return null;
}
 
Example 3
Project: OpenLibre   File: ReadingData.java   View Source Code 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: Espresso   File: PackagesLocalDataSource.java   View Source Code Vote up 5 votes
/**
 * Query the existence of a specific number.
 * @param packageId the package number to query.
 *                  See {@link Package#number}
 * @return whether the number is in the database.
 */
@Override
public boolean isPackageExist(@NonNull String packageId) {
    Realm rlm = RealmHelper.newRealmInstance();
    RealmResults<Package> results = rlm.where(Package.class)
            .equalTo("number", packageId)
            .findAll();
    return (results != null) && (!results.isEmpty());
}
 
Example 5
Project: CurrentActivity   File: CurrentActivity.java   View Source Code Vote up 5 votes
@NonNull
private String getCurrentActivity(Context context) {
    String packageName = null;
    String activityName = null;
    String applicationName;

    switch (Settings.getString(Settings.Mode.SELECTION, Settings.Mode.NONE)) {
        case Settings.Mode.ROOT:
            String request = SU.getSU().runCommand("dumpsys activity | grep \"mFocusedActivity\"");

            if (!TextUtils.isEmpty(request)) {
                String requests[] = request.split(" ")[3].split("/");

                packageName = requests[0];
                activityName = requests[1].substring(0, 1).equals(".") ? requests[0] + requests[1] : requests[1];
            }
            break;
        case Settings.Mode.ACCESSIBILITY_SERVICE:
            if (AccessibilityServiceUtils.isAccessibilityServiceEnabled(context, CurrentActivityAccessibilityService.class)) {
                packageName = CurrentActivityAccessibilityService.foregroundPackageName();
                activityName = CurrentActivityAccessibilityService.foregroundClassName();
                if (packageName.equals("null") || activityName.equals("null")) return "";
            } else {
                return context.getString(R.string.should_re_enable_accessibility_service);
            }
            break;
    }

    if (packageName != null && activityName != null) {
        applicationName = ApplicationUtil.getApplicationNameByPackageName(context, packageName);
        if (applicationName != null) {
            RealmResults<CurrentActivityData> results = realm.where(CurrentActivityData.class).findAll();
            if (!Objects.equals(packageName, getPackageName())) {
                if (results.isEmpty()) {
                    saveCurrentActivityData(applicationName, packageName, activityName);
                } else {
                    CurrentActivityData lastData = results.get(results.size() - 1);
                    if (!Objects.equals(lastData.getPackageName(), packageName) || !Objects.equals(lastData.getActivityName(), activityName))
                        saveCurrentActivityData(applicationName, packageName, activityName);
                }
            }
            return (applicationName + " ( " + packageName + " )" + "\n" + activityName);
        } else {
            return (packageName + "\n" + activityName);
        }
    } else {
        return context.getString(R.string.failed_to_get);
    }
}
 
Example 6
Project: Ghost-Android   File: AccountManager.java   View Source Code Vote up 5 votes
public static void deleteBlog(@NonNull String blogUrl) {
    RealmResults<BlogMetadata> matchingBlogs = findAllBlogsMatchingUrl(blogUrl);
    if (matchingBlogs.isEmpty()) {
        throw new IllegalStateException("No blog found matching the URL: " + blogUrl);
    }
    // we don't allow adding more than 1 blog with the same URL, so this should never happen
    if (matchingBlogs.size() > 1) {
        throw new IllegalStateException("More than 1 blog found matching the URL: " + blogUrl);
    }

    // delete blog metadata before data because data without metadata is harmless, but vice-versa is not
    // keep a copy of the metadata around so we can delete the data Realm after this
    final Realm realm = Realm.getDefaultInstance();
    BlogMetadata blogToDelete = matchingBlogs.get(0);
    RealmConfiguration dataRealmToDelete = realm.copyFromRealm(blogToDelete).getDataRealmConfig();
    RealmUtils.executeTransaction(realm, r -> {
        RealmObject.deleteFromRealm(blogToDelete);
    });

    // delete blog data
    Realm.deleteRealm(dataRealmToDelete);

    // if the active blog was deleted, set the active blog to a different one
    if (blogUrl.equals(getActiveBlogUrl())) {
        List<BlogMetadata> allBlogs = getAllBlogs();
        if (!allBlogs.isEmpty()) {
            setActiveBlog(allBlogs.get(0).getBlogUrl());
        } else {
            setActiveBlog("");
        }
    }
}