android - 在 Realm 中批量插入

标签 android realm

我决定在我的项目中使用 Realm。我已经阅读了文档,但无法理解如何将我所有的电话联系人导入到我的 Realm 数据库中。 以前有人做过这种项目吗?请帮忙。

我使用了具有批量插入选项的 Sugar ORM。 Realm 是否具有相同的功能或是否有替代方案?

这是我到目前为止所做的:

package com.advisualinc.switchchat.Realm_DB;

import io.realm.RealmObject;
import io.realm.annotations.PrimaryKey;

/**
 * Created by Veeresh on 10/19/2015.
 */
public class R_ContactDB extends RealmObject {
    private String name;
    @PrimaryKey
    private String phone;
    private boolean matchedWithRecent;
    private int status;


    public R_ContactDB(String name, String phone, boolean matchedWithRecent, int   status)
    {
        this.name = name;
        this.phone = phone;
        this.matchedWithRecent = matchedWithRecent;
        this.status = status;
    }

    public R_ContactDB()
    {

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public boolean isMatchedWithRecent() {
        return matchedWithRecent;
    }

    public void setMatchedWithRecent(boolean matchedWithRecent) {
        this.matchedWithRecent = matchedWithRecent;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }
}

最佳答案

有了 Realm,就不需要直接满足 SQLite 中可用的批量插入的东西。不涉及查询语言的开销。

您可以通过 Realm#copyToRealm 插入多个对象在单个写入事务中批处理。 如果需要导入JSON数据,有Realm#createOrUpdateAllFromJson(JSONArray) .

关于android - 在 Realm 中批量插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33229006/

相关文章:

android - 如何给listView这个阴影样式?

java - 如何限制用户在数据库中多次写入

c# - 使用 WCF (IIS 7.5) 从数据库获取值时出现系统错误

android - 查询 Realm Class Sub 对象的大小 - Kotlin

ios - RealmSwift : Module Compiled with Swift 3. 0 不能用Swift 3.0.2导入

swift - 了解 Realm 中的迁移 - Swift

java - 带图片和文字的自定义按钮?

android - 将付费应用迁移到 Google Play 上的应用内购买

swift - 使用 Realm,如何将查询的结果存储在全局属性中并访问它?

ios - 如何同时连接 Realm 与 watchOS 和 iOS 应用程序?