Execute transaction method using Realm 在每个 运行 android 上继续循环
Execute transaction method using Realm continues to loop on each run android
在我的应用程序中,我使用 Realm DB 将所有数据存储在本地数据库中。我有一些初始模拟数据,我想在应用程序启动时显示这些数据。以前我实现了 begintransaction 方法。但是在阅读了文档之后,我实施了执行交易方法。因为这种方法很容易更新我的新数据。现在的问题是,每当我单击显示 recyclerview 的选项时,数据每次都在循环。例如我有 3 个数据。如果我返回到上一页并再次转到此 recyclerview 页面,它会变成两倍,下次它会变成三倍,依此类推。我只想在视图中显示更新的数据。就像我添加第 4 个数据一样,我想将其显示给 recyclerview。如何停止在我的应用程序中的每个 运行 上循环。
这是我的代码
个人页面
public class InitialData implements Realm.Transaction {
@Override
public void execute(Realm realm) {
List<PersoneModel> person = new ArrayList<>();
PersonModel model = new PersonModel();
model.setId(1 + System.currentTimeMillis());
model.setName("Name1");
model.setCompany("Comapny1");
model.setTitle("Title1");
person.add(model);
model = new PersonModel();
model.setId(2 + System.currentTimeMillis());
model.setName("Name2");
model.setCompany("Company2");
model.setTitle("Title2");
person.add(model);
model = new PersonModel();
model.setId(2 + System.currentTimeMillis());
model.setName("Name2");
model.setCompany("Company2");
model.setTitle("Title2");
person.add(model);
model = new PersonModel();
model.setId(3 + System.currentTimeMillis());
model.setName("Name3");
model.setCompany("Comapny3");
model.setTitle("Title3");
person.add(model);
for (PersonModel realmModel : colleague) {
realm.insertOrUpdate(realmModel);
}
}
@Override
public boolean equals(Object obj) {
return obj != null && obj instanceof InitialData;
}
@Override
public int hashCode() {
return InitialData.class.hashCode();
}
}
新闻页面初始数据
public class InitialDataNews implements Realm.Transaction {
@Override
public void execute(Realm realm) {
List<NewsModel> newsItem = new ArrayList<>();
NewsModel model = new NewsModel();
model.setId(1+System.currentTimeMillis());
model.setImage(R.drawable.image1);
model.setTitle("title1");
model.setDate("12.07.2017");
model.setDetail("deascription");
newsItem.add(model);
model = new NewsModel();
model.setId(3+System.currentTimeMillis());
model.setImage(R.drawable.image3);
model.setTitle("title2");
model.setDate("05.05.2017");
model.setDetail("description");
newsItem.add(model);
model = new NewsModel();
model.setId(4+System.currentTimeMillis());
model.setImage(R.drawable.image4);
model.setTitle("title3");
model.setDate("13.04.2017");
model.setDetail("description3");
newsItem.add(model);
.....
for (NewsModel realmModel : newsItem) {
realm.insertOrUpdate(realmModel);
}
}
@Override
public boolean equals(Object obj) {
return obj != null && obj instanceof InitialData;
}
@Override
public int hashCode() {
return InitialData.class.hashCode();
}
}
基本应用程序
@Override
public void onCreate() {
super.onCreate();
// The Realm file will be located in Context.getFilesDir() with name "default.realm"
//Here we setup the realm configuration.
//Setting a default configuration in custom Application class, will ensure that it is available in the rest of the code.
Realm.init(this);
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder()
.deleteRealmIfMigrationNeeded()
.initialData(CompositeInitialData.of(new InitialDataNews(), new InitialData()).build());
Realm.setDefaultConfiguration(realmConfiguration);
}
添加更改监听器
public class PersonPage extends AppCompatActivity implements PersonAdapter.PersonListListener{
private RecyclerView recyclerView;
private PersonAdapter adapter;
private Realm PersonRealm;
private RealmResults<PersoneModel> personResult;
private RealmChangeListener realmListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mycolleagues_layout);
personRealm = Realm.getDefaultInstance();
recyclerView = (RecyclerView) findViewById(R.id.colleagues_recycler);
realmListener=new RealmChangeListener() {
@Override
public void onChange(Object o) {
showAllPersons();
}};
colleagueRealm.addChangeListener(realmListener);
setUpRecycler();
}
private void showAllPersons() {
realmResult = personRealm.where(PersonModel.class).findAllAsync();
setAdapter(realmResult);
adapter.notifyDataSetChanged();
}
private void setAdapter(RealmResults<PersonModel> results) {
adapter = new PersonAdapter(this, personRealm.where(PersonModel.class).findAllSortedAsync("id"),this);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
那是因为在 onCreate()
中,您 运行 basicCRUD(personRealm);
添加了新的人物模型
PersonModel model = new PersonModel();
model.setId(1 + System.currentTimeMillis());
model.setName("Name1");
model.setCompany("Comapny1");
model.setTitle("Title1");
person.add(model);
当您将 id 设置为 1 + System.currentTimeMillis()
时,它将是一个具有不同主键的新对象,并被插入。如果它只是 1
、2
等等,那么它会将它们更新为同一个对象。
谢天谢地,Realm 提供了initialData(Realm.Transaction)
method on the RealmConfiguration.Builder()
允许你只在第一次创建数据库时为数据库创建初始数据,或者在 deleteRealmIfMigrationNeeded()
.[=27] 之后删除数据=]
public class InitialData implements Realm.Transaction {
@Override
public void execute(Realm realm) {
List<PersonModel> person = new ArrayList<>();
PersonModel model = new PersonModel();
model.setId(1 + System.currentTimeMillis());
model.setName("Name1");
model.setCompany("Comapny1");
model.setTitle("Title1");
person.add(model);
model = new PersonModel();
model.setId(2 + System.currentTimeMillis());
model.setName("Name2");
model.setCompany("Company2");
model.setTitle("Title2");
person.add(model);
model = new PersonModel();
model.setId(3 + System.currentTimeMillis());
model.setName("Name3");
model.setCompany("Company23");
model.setTitle("Title3");
person.add(model);
model = new PersonModel();
model.setId(4 + System.currentTimeMillis());
model.setName("Name4");
model.setCompany("Comapny4");
model.setTitle("Title4");
person.add(model);
for (PersonModel realmModel : person) {
realm.insertOrUpdate(realmModel);
}
}
@Override
public boolean equals(Object object) {
return object != null && object instanceof InitialData;
}
@Override
public int hashCode() {
return InitialData.class.hashCode();
}
}
和
Realm.init(this);
Realm.setDefaultConfiguration(new RealmConfiguration.Builder()
.deleteRealmIfMigrationNeeded()
.initialData(new InitialData())
.build());
编辑:
要在一个"initial data"中提供多个"initial datas"(因为你提供了1笔交易),你可以依靠composite design pattern
在一个初始数据中提供多个初始数据,像这样
public final class InitialDataPersons implements Realm.Transaction {
@Override
public void execute(Realm realm) {
List<PersonModel> person = new ArrayList<>();
PersonModel model = new PersonModel();
model.setId(1 + System.currentTimeMillis());
model.setName("Name1");
model.setCompany("Comapny1");
model.setTitle("Title1");
person.add(model);
model = new PersonModel();
model.setId(2 + System.currentTimeMillis());
model.setName("Name2");
model.setCompany("Company2");
model.setTitle("Title2");
person.add(model);
model = new PersonModel();
model.setId(3 + System.currentTimeMillis());
model.setName("Name3");
model.setCompany("Company23");
model.setTitle("Title3");
person.add(model);
model = new PersonModel();
model.setId(4 + System.currentTimeMillis());
model.setName("Name4");
model.setCompany("Comapny4");
model.setTitle("Title4");
person.add(model);
for (PersonModel realmModel : person) {
realm.insertOrUpdate(realmModel);
}
}
@Override
public boolean equals(Object object) {
return object != null && object instanceof InitialDataPersons;
}
@Override
public int hashCode() {
return InitialDataPersons.class.hashCode();
}
}
和
public final class InitialDataNews implements Realm.Transaction {
@Override
public void execute(Realm realm) {
List<NewsModel> newsItem = new ArrayList<>();
NewsModel model = new NewsModel();
model.setId(1+System.currentTimeMillis());
model.setImage(R.drawable.image1);
model.setTitle("title1");
model.setDate("12.07.2017");
model.setDetail("deascription");
newsItem.add(model);
model = new NewsModel();
model.setId(3+System.currentTimeMillis());
model.setImage(R.drawable.image3);
model.setTitle("title2");
model.setDate("05.05.2017");
model.setDetail("description");
newsItem.add(model);
model = new NewsModel();
model.setId(4+System.currentTimeMillis());
model.setImage(R.drawable.image4);
model.setTitle("title3");
model.setDate("13.04.2017");
model.setDetail("description3");
newsItem.add(model);
.....
for (NewsModel realmModel : newsItem) {
realm.insertOrUpdate(realmModel);
}
}
@Override
public boolean equals(Object obj) {
return obj != null && obj instanceof InitialDataNews;
}
@Override
public int hashCode() {
return InitialDataNews.class.hashCode();
}
}
和
public final class CompositeInitialData implements Realm.Transaction {
private final Realm.Transaction[] transactions;
private CompositeInitialData(Realm.Transaction... transactions) { // `of` is prettier
this.transactions = transactions;
}
public static CompositeInitialData of(Realm.Transaction... transactions) {
return new CompositeInitialData(transactions);
}
@Override
public void execute(Realm realm) {
for(Realm.Transaction transaction : transactions) {
transaction.execute(realm);
}
}
@Override
public boolean equals(Object obj) {
return obj != null && obj instanceof CompositeInitialData; // you might want autogenerated equals instead
}
@Override
public int hashCode() {
return CompositeInitialData.class.hashCode(); // you might want autogenerated hashCode instead
}
}
这样你可以提供
Realm.init(this);
Realm.setDefaultConfiguration(new RealmConfiguration.Builder()
.deleteIfMigrationNeeded()
.initialData(CompositeInitialData.of(new InitialDataNews(), new InitialDataPersons()))
.build());
在我的应用程序中,我使用 Realm DB 将所有数据存储在本地数据库中。我有一些初始模拟数据,我想在应用程序启动时显示这些数据。以前我实现了 begintransaction 方法。但是在阅读了文档之后,我实施了执行交易方法。因为这种方法很容易更新我的新数据。现在的问题是,每当我单击显示 recyclerview 的选项时,数据每次都在循环。例如我有 3 个数据。如果我返回到上一页并再次转到此 recyclerview 页面,它会变成两倍,下次它会变成三倍,依此类推。我只想在视图中显示更新的数据。就像我添加第 4 个数据一样,我想将其显示给 recyclerview。如何停止在我的应用程序中的每个 运行 上循环。
这是我的代码
个人页面
public class InitialData implements Realm.Transaction {
@Override
public void execute(Realm realm) {
List<PersoneModel> person = new ArrayList<>();
PersonModel model = new PersonModel();
model.setId(1 + System.currentTimeMillis());
model.setName("Name1");
model.setCompany("Comapny1");
model.setTitle("Title1");
person.add(model);
model = new PersonModel();
model.setId(2 + System.currentTimeMillis());
model.setName("Name2");
model.setCompany("Company2");
model.setTitle("Title2");
person.add(model);
model = new PersonModel();
model.setId(2 + System.currentTimeMillis());
model.setName("Name2");
model.setCompany("Company2");
model.setTitle("Title2");
person.add(model);
model = new PersonModel();
model.setId(3 + System.currentTimeMillis());
model.setName("Name3");
model.setCompany("Comapny3");
model.setTitle("Title3");
person.add(model);
for (PersonModel realmModel : colleague) {
realm.insertOrUpdate(realmModel);
}
}
@Override
public boolean equals(Object obj) {
return obj != null && obj instanceof InitialData;
}
@Override
public int hashCode() {
return InitialData.class.hashCode();
}
}
新闻页面初始数据
public class InitialDataNews implements Realm.Transaction {
@Override
public void execute(Realm realm) {
List<NewsModel> newsItem = new ArrayList<>();
NewsModel model = new NewsModel();
model.setId(1+System.currentTimeMillis());
model.setImage(R.drawable.image1);
model.setTitle("title1");
model.setDate("12.07.2017");
model.setDetail("deascription");
newsItem.add(model);
model = new NewsModel();
model.setId(3+System.currentTimeMillis());
model.setImage(R.drawable.image3);
model.setTitle("title2");
model.setDate("05.05.2017");
model.setDetail("description");
newsItem.add(model);
model = new NewsModel();
model.setId(4+System.currentTimeMillis());
model.setImage(R.drawable.image4);
model.setTitle("title3");
model.setDate("13.04.2017");
model.setDetail("description3");
newsItem.add(model);
.....
for (NewsModel realmModel : newsItem) {
realm.insertOrUpdate(realmModel);
}
}
@Override
public boolean equals(Object obj) {
return obj != null && obj instanceof InitialData;
}
@Override
public int hashCode() {
return InitialData.class.hashCode();
}
}
基本应用程序
@Override
public void onCreate() {
super.onCreate();
// The Realm file will be located in Context.getFilesDir() with name "default.realm"
//Here we setup the realm configuration.
//Setting a default configuration in custom Application class, will ensure that it is available in the rest of the code.
Realm.init(this);
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder()
.deleteRealmIfMigrationNeeded()
.initialData(CompositeInitialData.of(new InitialDataNews(), new InitialData()).build());
Realm.setDefaultConfiguration(realmConfiguration);
}
添加更改监听器
public class PersonPage extends AppCompatActivity implements PersonAdapter.PersonListListener{
private RecyclerView recyclerView;
private PersonAdapter adapter;
private Realm PersonRealm;
private RealmResults<PersoneModel> personResult;
private RealmChangeListener realmListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mycolleagues_layout);
personRealm = Realm.getDefaultInstance();
recyclerView = (RecyclerView) findViewById(R.id.colleagues_recycler);
realmListener=new RealmChangeListener() {
@Override
public void onChange(Object o) {
showAllPersons();
}};
colleagueRealm.addChangeListener(realmListener);
setUpRecycler();
}
private void showAllPersons() {
realmResult = personRealm.where(PersonModel.class).findAllAsync();
setAdapter(realmResult);
adapter.notifyDataSetChanged();
}
private void setAdapter(RealmResults<PersonModel> results) {
adapter = new PersonAdapter(this, personRealm.where(PersonModel.class).findAllSortedAsync("id"),this);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
那是因为在 onCreate()
中,您 运行 basicCRUD(personRealm);
添加了新的人物模型
PersonModel model = new PersonModel();
model.setId(1 + System.currentTimeMillis());
model.setName("Name1");
model.setCompany("Comapny1");
model.setTitle("Title1");
person.add(model);
当您将 id 设置为 1 + System.currentTimeMillis()
时,它将是一个具有不同主键的新对象,并被插入。如果它只是 1
、2
等等,那么它会将它们更新为同一个对象。
谢天谢地,Realm 提供了initialData(Realm.Transaction)
method on the RealmConfiguration.Builder()
允许你只在第一次创建数据库时为数据库创建初始数据,或者在 deleteRealmIfMigrationNeeded()
.[=27] 之后删除数据=]
public class InitialData implements Realm.Transaction {
@Override
public void execute(Realm realm) {
List<PersonModel> person = new ArrayList<>();
PersonModel model = new PersonModel();
model.setId(1 + System.currentTimeMillis());
model.setName("Name1");
model.setCompany("Comapny1");
model.setTitle("Title1");
person.add(model);
model = new PersonModel();
model.setId(2 + System.currentTimeMillis());
model.setName("Name2");
model.setCompany("Company2");
model.setTitle("Title2");
person.add(model);
model = new PersonModel();
model.setId(3 + System.currentTimeMillis());
model.setName("Name3");
model.setCompany("Company23");
model.setTitle("Title3");
person.add(model);
model = new PersonModel();
model.setId(4 + System.currentTimeMillis());
model.setName("Name4");
model.setCompany("Comapny4");
model.setTitle("Title4");
person.add(model);
for (PersonModel realmModel : person) {
realm.insertOrUpdate(realmModel);
}
}
@Override
public boolean equals(Object object) {
return object != null && object instanceof InitialData;
}
@Override
public int hashCode() {
return InitialData.class.hashCode();
}
}
和
Realm.init(this);
Realm.setDefaultConfiguration(new RealmConfiguration.Builder()
.deleteRealmIfMigrationNeeded()
.initialData(new InitialData())
.build());
编辑:
要在一个"initial data"中提供多个"initial datas"(因为你提供了1笔交易),你可以依靠composite design pattern
在一个初始数据中提供多个初始数据,像这样
public final class InitialDataPersons implements Realm.Transaction {
@Override
public void execute(Realm realm) {
List<PersonModel> person = new ArrayList<>();
PersonModel model = new PersonModel();
model.setId(1 + System.currentTimeMillis());
model.setName("Name1");
model.setCompany("Comapny1");
model.setTitle("Title1");
person.add(model);
model = new PersonModel();
model.setId(2 + System.currentTimeMillis());
model.setName("Name2");
model.setCompany("Company2");
model.setTitle("Title2");
person.add(model);
model = new PersonModel();
model.setId(3 + System.currentTimeMillis());
model.setName("Name3");
model.setCompany("Company23");
model.setTitle("Title3");
person.add(model);
model = new PersonModel();
model.setId(4 + System.currentTimeMillis());
model.setName("Name4");
model.setCompany("Comapny4");
model.setTitle("Title4");
person.add(model);
for (PersonModel realmModel : person) {
realm.insertOrUpdate(realmModel);
}
}
@Override
public boolean equals(Object object) {
return object != null && object instanceof InitialDataPersons;
}
@Override
public int hashCode() {
return InitialDataPersons.class.hashCode();
}
}
和
public final class InitialDataNews implements Realm.Transaction {
@Override
public void execute(Realm realm) {
List<NewsModel> newsItem = new ArrayList<>();
NewsModel model = new NewsModel();
model.setId(1+System.currentTimeMillis());
model.setImage(R.drawable.image1);
model.setTitle("title1");
model.setDate("12.07.2017");
model.setDetail("deascription");
newsItem.add(model);
model = new NewsModel();
model.setId(3+System.currentTimeMillis());
model.setImage(R.drawable.image3);
model.setTitle("title2");
model.setDate("05.05.2017");
model.setDetail("description");
newsItem.add(model);
model = new NewsModel();
model.setId(4+System.currentTimeMillis());
model.setImage(R.drawable.image4);
model.setTitle("title3");
model.setDate("13.04.2017");
model.setDetail("description3");
newsItem.add(model);
.....
for (NewsModel realmModel : newsItem) {
realm.insertOrUpdate(realmModel);
}
}
@Override
public boolean equals(Object obj) {
return obj != null && obj instanceof InitialDataNews;
}
@Override
public int hashCode() {
return InitialDataNews.class.hashCode();
}
}
和
public final class CompositeInitialData implements Realm.Transaction {
private final Realm.Transaction[] transactions;
private CompositeInitialData(Realm.Transaction... transactions) { // `of` is prettier
this.transactions = transactions;
}
public static CompositeInitialData of(Realm.Transaction... transactions) {
return new CompositeInitialData(transactions);
}
@Override
public void execute(Realm realm) {
for(Realm.Transaction transaction : transactions) {
transaction.execute(realm);
}
}
@Override
public boolean equals(Object obj) {
return obj != null && obj instanceof CompositeInitialData; // you might want autogenerated equals instead
}
@Override
public int hashCode() {
return CompositeInitialData.class.hashCode(); // you might want autogenerated hashCode instead
}
}
这样你可以提供
Realm.init(this);
Realm.setDefaultConfiguration(new RealmConfiguration.Builder()
.deleteIfMigrationNeeded()
.initialData(CompositeInitialData.of(new InitialDataNews(), new InitialDataPersons()))
.build());