在 Realm 中批量插入
Bulk insert in Realm
我决定在我的项目中使用 Realm。我已经阅读了文档,但无法理解如何将我所有的 phone 联系人导入到我的 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).
2016 年领域添加了一个新的api 用于插入批量数据
realm.insertOrUpdate(Collections datas);
试试这个方法,更多信息,请看这里。
https://realm.io/blog/realm-java-1-1-0/
kotlin 中的完整代码
fun insertList(list: MutableList<MenuItems>) {
val realm = AppDatabase.getRealmInstance() //get Realm Instance
try {
realm.executeTransaction { realmTr ->
realmTr.copyToRealm(list)
}
} catch (error: Exception) {
error.printStackTrace()
} finally {
realm.close()
}
}
我决定在我的项目中使用 Realm。我已经阅读了文档,但无法理解如何将我所有的 phone 联系人导入到我的 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).
2016 年领域添加了一个新的api 用于插入批量数据
realm.insertOrUpdate(Collections datas);
试试这个方法,更多信息,请看这里。 https://realm.io/blog/realm-java-1-1-0/
kotlin 中的完整代码
fun insertList(list: MutableList<MenuItems>) {
val realm = AppDatabase.getRealmInstance() //get Realm Instance
try {
realm.executeTransaction { realmTr ->
realmTr.copyToRealm(list)
}
} catch (error: Exception) {
error.printStackTrace()
} finally {
realm.close()
}
}