Android Realm Arraylist和简单的POJO

时间:2017-05-22 20:29:28

标签: java android arraylist realm

我从外部数据库(使用改造)检索许多(6600行)对象,它看起来和我的POJO类一起使用。之后,我尝试将它放入Realm对象,但它非常慢。我怀疑这个缓慢的动作是因为我开始逐行提交事务。也许有人知道如何在一次提交中将完整的arraylist放到realm对象中? 我的主要:

private class AsyncTaskRetro extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... voids) {

            PlacesAPI.Factory.getInstance().getPlaces().enqueue(new Callback<Places>() {

                @Override
                public void onResponse(Call<Places> call, Response<Places> response) {
                    for (int i = 0; i < response.body().getPosts().size(); i++) {
                        RowModel rowModel = new RowModel(response.body().getPosts().get(i).getNazwa(),
                                Double.parseDouble(response.body().getPosts().get(i).getSzer()),
                                Double.parseDouble(response.body().getPosts().get(i).getDlug()));
                        rowModels.add(rowModel);
                    }
                    String oldName;
                    oldName = rowModels.get(0).getName();
                    shopsNames.add(rowModels.get(0).getName());


                    mRealm.beginTransaction();
                    RowModel rowModelRealm = mRealm.createObject(RowModel.class);
                    mRealm.commitTransaction();
                    for (int j = 0; j < rowModels.size(); j++) {
                        mRealm.beginTransaction();
                        rowModelRealm.setName(rowModels.get(j).getName());
                        rowModelRealm.setLattitude(rowModels.get(j).getLattitude());
                        rowModelRealm.setLongitude(rowModels.get(j).getLongitude());
                        mRealm.commitTransaction();

                        if (rowModels.get(j).getName().equals(oldName)) {
                            continue;
                        }
                        oldName = rowModels.get(j).getName();
                        shopsNames.add(rowModels.get(j).getName());
                    }



     //sortowanie 

listy z nazwami sklepow
                    Collections.sort(shopsNames);

                adapter = new ShopsAdapter(MainActivity.this, shopsNames);

                recyclerView.setAdapter(adapter);


            }

            @Override
            public void onFailure(Call<Places> call, Throwable t) {

            }
        });

        return null;
    }
}

和POJO:

public class RowModel extends RealmObject {

    @Required
    private String name;
    @Required
    private Double lattitude, longitude;

    public String getName() {
        return name;
    }

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

    public Double getLattitude() {
        return lattitude;
    }

    public void setLattitude(Double lattitude) {
        this.lattitude = lattitude;
    }

    public Double getLongitude() {
        return longitude;
    }

    public void setLongitude(Double longitude) {
        this.longitude = longitude;
    }

    public RowModel(String name, Double lattitude, Double longitude) {
        this.name = name;
        this.lattitude = lattitude;
        this.longitude = longitude;
    }

    public RowModel() {
    }
}

2 个答案:

答案 0 :(得分:1)

Realm对象具有以下方法:     insert(Iterable<E> objects) 这应该允许您一次复制所有RowModel个对象

答案 1 :(得分:1)

1。)你正在使用Realm 0.82.2,那个版本有怪癖,这就是为什么我推荐的下一个稳定版本是0.87.5;但实际上最新的稳定版(目前)是3.1.4。

你应该切换到一个不是2岁的版本吗?

2。)您正在运行AsyncTask,但在其中使用enqueue

getPlaces().enqueue(new Callback<Places>() {    
      @Override
      public void onResponse(Call<Places> call, Response<Places> response) {

这意味着Retrofit回调将在UI线程上执行。

然后,你进行同步事务,所以你要在UI线程上写入Realm,这很糟糕。

3。)您尝试手动对下载的元素进行排序,然后手动将其显示在适配器中,如果您正在使用Realm,这是没有意义的,因为Realm允许排序元素由一个特定的领域。此外shopsNames绝对不是RealmResults,这意味着您甚至不会监听对数据库所做的更改。

ShopsAdapter应为RealmBaseAdapter

所以代码应该更像这个

private class AsyncTaskRetro
        extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... voids) {
        try(Realm r = Realm.getDefaultInstance()) {
            Response<Place> response = PlacesAPI.Factory.getInstance().getPlaces().execute();
            Place place = response.body();
            List<Post> posts = place.getPosts();
            r.executeTransaction((realm) -> {
                for(Post post : posts) {
                    RowModel rowModel = new RowModel(post.getNazwa(),
                            Double.parseDouble(post.getSzer()),
                            Double.parseDouble(post.getDlug()));
                    rowModels.add(rowModel);
                }
                String oldName;
                oldName = rowModels.get(0).getName();
                shopsNames.add(rowModels.get(0).getName());

                RowModel rowModelRealm = realm.createObject(RowModel.class);
                for(RowModel rowModel : rowModels) {
                    rowModelRealm.setName(rowModel.getName());
                    rowModelRealm.setLattitude(rowModel.getLattitude());
                    rowModelRealm.setLongitude(rowModel.getLongitude());

                    if(rowModel.getName().equals(oldName)) {
                        continue;
                    }
                    oldName = rowModel.getName();
                    shopsNames.add(rowModel.getName());
                }
            });
            // no need to manually sort if you use RealmBaseAdapter with findAllSorted()
        }
        return null;
    }
}