在 Realm 中批量插入 [英] Bulk insert in Realm

查看:230
本文介绍了在 Realm 中批量插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have decided to use Realm for my project. I've gone through the documentation and cannot understand how to get all my phone contacts imported into my Realm database. Has anyone done this kind of project before? Please help.

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

I have used Sugar ORM which has a bulk insert option. Does Realm have the same or is there an alternative to that?

这是我到现在为止所做的:

Here's what I've done till now:

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 中可用的批量插入的东西.不涉及查询语言的开销.

With Realm there is no need for something which is directly adequate to bulk inserts available in SQLite. There is no overhead of a query language involved.

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

You can just insert multiple objects via Realm#copyToRealm batched in a single write transaction. If you need to import JSON data, there is Realm#createOrUpdateAllFromJson(JSONArray).

这篇关于在 Realm 中批量插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆