Java Code Examples for io.realm.RealmResults#deleteAllFromRealm()

The following examples show how to use io.realm.RealmResults#deleteAllFromRealm() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
@Override
public void clearAll() {
    //When we use a different account to login in ,we should delete all the ReadLaterArticleData in realm
    Realm realm = RealmHelper.newRealmInstance();
    RealmResults<ReadLaterArticleData> results =
            realm.where(ReadLaterArticleData.class).findAll();
    realm.beginTransaction();
    results.deleteAllFromRealm();
    realm.commitTransaction();
    realm.close();
}
 
Example 2
/**
 * Hint:use in transaction
 * delete all avatars from RealmAvatar
 *
 * @param ownerId use this id for delete from RealmAvatar
 */
public static void deleteAllAvatars(final long ownerId, Realm realm) {
    RealmResults<RealmAvatar> ownerAvatars = realm.where(RealmAvatar.class).equalTo(RealmAvatarFields.OWNER_ID, ownerId).findAll();
    if (ownerAvatars.size() > 0) {
        ownerAvatars.deleteAllFromRealm();
    }
}
 
Example 3
Source Project: openwebnet-android   File: DatabaseRealm.java    License: MIT License 5 votes vote down vote up
public <T extends RealmObject> void delete(Class<T> clazz, String field, String value) {
    Realm realm = getRealmInstance();
    RealmResults<T> models = realm.where(clazz).equalTo(field, value).findAll();
    realm.beginTransaction();
    models.deleteAllFromRealm();
    realm.commitTransaction();
}
 
Example 4
Source Project: openwebnet-android   File: DatabaseRealm.java    License: MIT License 5 votes vote down vote up
public <T extends RealmObject> void delete(Class<T> clazz, String field, Integer value) {
    Realm realm = getRealmInstance();
    RealmResults<T> models = realm.where(clazz).equalTo(field, value).findAll();
    realm.beginTransaction();
    models.deleteAllFromRealm();
    realm.commitTransaction();
}
 
Example 5
Source Project: openwebnet-android   File: DatabaseRealm.java    License: MIT License 5 votes vote down vote up
public <T extends RealmObject> void deleteAll(Class<T> clazz) {
    Realm realm = getRealmInstance();
    RealmResults<T> models = realm.where(clazz).findAll();
    realm.beginTransaction();
    models.deleteAllFromRealm();
    realm.commitTransaction();
}
 
Example 6
Source Project: quill   File: BlogMetadataDBMigration.java    License: MIT License 4 votes vote down vote up
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
    RealmSchema schema = realm.getSchema();
    Crashlytics.log(Log.INFO, TAG, "MIGRATING DATABASE from v" + oldVersion + " to v" + newVersion);

    if (oldVersion == 0) {
        if (schema.get("Post").isNullable("slug")) {
            // get rid of null-valued slugs, if any exist
            RealmResults<DynamicRealmObject> postsWithNullSlug = realm
                    .where(Post.class.getSimpleName())
                    .isNull("slug")
                    .findAll();
            Crashlytics.log(Log.DEBUG, TAG, "CONVERTING " + postsWithNullSlug.size() + " SLUGS FROM NULL TO \"\"");
            for (DynamicRealmObject obj : postsWithNullSlug) {
                obj.setString("slug", "");
            }
            // finally, make the field required
            schema.get("Post").setNullable("slug", false);
        }

        schema.get("Post")
                .setNullable("html", true)
                .setNullable("image", true)
                .setNullable("createdAt", true)
                .setNullable("publishedAt", true)
                .setNullable("metaTitle", true)
                .setNullable("metaDescription", true);
        schema.get("User")
                .addIndex("id")
                .setNullable("image", true)
                .setNullable("bio", true);
        schema.get("Tag")
                .setNullable("slug", true)
                .setNullable("description", true)
                .setNullable("image", true)
                .setNullable("metaTitle", true)
                .setNullable("metaDescription", true)
                .setNullable("createdAt", true)
                .setNullable("updatedAt", true);
        schema.get("Setting")
                .addIndex("id");
        ++oldVersion;
    }

    if (oldVersion == 1) {
        // delete all etags, so the info can be fetched and stored
        // again, with role-based permissions enforced
        RealmResults<DynamicRealmObject> allEtags = realm
                .where(ETag.class.getSimpleName())
                .equalTo("type", ETag.TYPE_CURRENT_USER)
                .or()
                .equalTo("type", ETag.TYPE_ALL_POSTS)
                .findAll();
        Crashlytics.log(Log.DEBUG, TAG, "DELETING ALL ETAGS TO REFRESH DATA COMPLETELY");
        allEtags.deleteAllFromRealm();

        if (!schema.contains("Role")) {
            // create the Role table
            Crashlytics.log(Log.DEBUG, TAG, "CREATING ROLE TABLE");
            schema.create("Role")
                    .addField("id", Integer.class, FieldAttribute.PRIMARY_KEY)
                    .addField("uuid", String.class, FieldAttribute.REQUIRED)
                    .addField("name", String.class, FieldAttribute.REQUIRED)
                    .addField("description", String.class, FieldAttribute.REQUIRED);
        }

        if (!schema.get("User").hasField("roles")) {
            Crashlytics.log(Log.DEBUG, TAG, "ADDING ROLES FIELD TO USER TABLE");
            schema.get("User").addRealmListField("roles", schema.get("Role"));
        }
        ++oldVersion;
    }

    if (oldVersion == 2) {
        if (!schema.get("Post").hasField("conflictState")) {
            Crashlytics.log(Log.DEBUG, TAG, "ADDING CONFLICT STATE FIELD TO POST TABLE");
            schema.get("Post").addField("conflictState", String.class, FieldAttribute.REQUIRED);
        }
        ++oldVersion;
    }

    if (oldVersion == 3) {
        // Ghost 1.0 upgrade, drop all data
        Crashlytics.log(Log.WARN, TAG, "DROPPING ALL DATA");
        final SpectreApplication app = SpectreApplication.getInstance();
        app.setOldRealmSchemaVersion(3);

        // clear logged in state
        AppState.getInstance(app).setBoolean(AppState.Key.LOGGED_IN, false);
        UserPrefs.getInstance(app).clear(UserPrefs.Key.EMAIL);
        UserPrefs.getInstance(app).clear(UserPrefs.Key.PASSWORD);
        UserPrefs.getInstance(app).clear(UserPrefs.Key.PERMALINK_FORMAT);

        ++oldVersion;
    }

    // STARTING FROM V4, THE REALM THAT USED TO STORE THE BLOG DATA NOW
    // ONLY STORES THE *METADATA* FOR ALL CONNECTED BLOGS
}