应用程序在使用realm和viewPager时挂起

时间:2016-07-12 06:19:38

标签: android realm

好吧,我使用Realm来保存数据并使其完美运行。但是当我在其中一个片段中使用它来保存数据时,其他片段开始挂起然后应用程序关闭。我正在使用改造来获取数据。我没有发布所有类,只发布那些相关的方法。

1)我检查互联网连接是否可用。如果不是我从领域获取数据并将其放在回收站视图中:

public void internet() {
    cd = new ConnectionDetector(getActivity());
    // Check if Internet present
    isInternetPresent = cd.isConnectingToInternet();
    if (isInternetPresent == false) {

        Toast.makeText(getActivity(), "Internet Not Present", Toast.LENGTH_LONG).show();
        realm = Realm.getDefaultInstance();
        recyclerView.setAdapter(new AdapterClass(this, realm.where(AndroidVersion.class).findAllAsync()));

    } else {
        realm = Realm.getDefaultInstance();
        realm.executeTransactionAsync(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                RealmResults<AndroidVersion> realmList = realm.where(AndroidVersion.class).findAll();
                realmList.deleteAllFromRealm();
            }
        });
        loadJSON();
    }
}

2)如果连接可用,我调用loadJSON方法并获取数据,使用recycleler视图设置它并将其保存在领域中:

public void loadJSON() {
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://54.187.18.119/lived2d/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    RequestInterface request = retrofit.create(RequestInterface.class);
    Call<JSONResponse> call = request.getJSON();
    call.enqueue(new Callback<JSONResponse>() {
        @Override
        public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {
            JSONResponse jsonResponse = response.body();
            data = new ArrayList<>(Arrays.asList(jsonResponse.getExplore()));
            adapter = new RecyclerAdapter(data);
            recyclerView.setAdapter(adapter);
            for (int i = 0; i < 20; i++) {
                realm = Realm.getDefaultInstance();
                final int finalI = i;
                realm.executeTransactionAsync(new Realm.Transaction() {
                    @Override
                    public void execute(Realm realm) {
                        AndroidVersion androidVersion = new AndroidVersion();
                        androidVersion.setName(data.get(finalI).getName());
                        androidVersion.setLname(data.get(finalI).getLname());
                        androidVersion.setTitle(data.get(finalI).getTitle());
                        androidVersion.setLocation(data.get(finalI).getLocation());
                        androidVersion.setTotal_comment(data.get(finalI).getTotal_comment());
                        androidVersion.setTotal_like(data.get(finalI).getTotal_like());
                        androidVersion.setDate(data.get(finalI).getDate());
                        androidVersion.setId(data.get(finalI).getId());
                        androidVersion.setUser_id(data.get(finalI).getUser_id());
                        realm.copyToRealm(androidVersion);

                        RealmResults<AndroidVersion> results = realm.where(AndroidVersion.class).findAll();
                        Log.d("ALL ANSWER", String.valueOf(results));
                    }
                });
            }
        }

        @Override
        public void onFailure(Call<JSONResponse> call, Throwable t) {
            //Log.d("Error", t.getMessage());
            if (t.getMessage().equals("timeout")) {
                new AlertDialog.Builder(getContext())
                        .setIcon(R.mipmap.ic_launche)
                        .setTitle("Network Error")
                        .setMessage("Connection is weak or not available. Please try again after some time.")
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                            }

                        })
                        .setNegativeButton("Cancel", null)
                        .show();
            }
        }
    });
}

RecyclerAdapter类:

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder> {
private ArrayList<AndroidVersion> android;
public static final String KEY_USERID = "userid";
public static final String KEY_EXPLOREID = "exploreid";

public RecyclerAdapter(ArrayList<AndroidVersion> android) {
    this.android = android;
    if (ExploreFragment.srl.isRefreshing()) {
        ExploreFragment.srl.setRefreshing(false);
    }
}

@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.content_explore_fragment, parent, false);
    return new RecyclerViewHolder(view);
}

@Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {

    Context context = holder.imageView.getContext();
    String st = android.get(position).getImage();
    String image = st.replace("\\", "");
    int length = android.get(position).getTitle().length();
    if (length >= 60) {
        String title = android.get(position).getTitle().substring(0, 60) + "...";
        holder.tv1.setText(title);
    } else {
        holder.tv1.setText(android.get(position).getTitle());
    }
    holder.time.setText(android.get(position).getDate());
    holder.totallikes.setText(android.get(position).getTotal_like());
    holder.location.setText(android.get(position).getLocation());
    holder.name.setText(android.get(position).getName());
    holder.comment.setText(android.get(position).getTotal_comment());
    holder.lname.setText(android.get(position).getLname());
    if (image.equals("")) {

        Picasso.with(context).load(R.drawable.profilepi).into(holder.imageView);
    } else {

        Picasso.with(context).load("http://ec2-54-187-18-119.us-west-2.compute.amazonaws.com/lived2d/" + image).resize(100, 100).centerCrop().into(holder.imageView);
    }

}


@Override
public int getItemCount() {
    return android.size();
}

AdapterClass类:

public class AdapterClass extends RecyclerView.Adapter<AdapterClass.RecyclerViewHolder> {
private RealmResults<AndroidVersion> arrayLists;
private Realm realm;

public AdapterClass(ExploreFragment second, RealmResults<AndroidVersion> allAsync) {
    this.arrayLists = allAsync;
}

public AdapterClass(ArrayList<AndroidVersion> data) {
}

@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.content_explore_fragment, parent, false);
    return new RecyclerViewHolder(view);
}

@Override
public void onBindViewHolder(final RecyclerViewHolder holder, final int position) {

    realm = Realm.getDefaultInstance();
    AndroidVersion user = arrayLists.get(position);
    holder.name.setText(user.getName());
    holder.lname.setText(user.getLname());
    holder.location.setText(user.getLocation());
    holder.tv1.setText(user.getTitle());
    holder.time.setText(user.getDate());
    holder.totallikes.setText(user.getTotal_like());
    holder.lname.setText(user.getLname());
    holder.comment.setText(user.getTotal_comment());
}


@Override
public int getItemCount() {
    return arrayLists.size();
}

感谢任何帮助,谢谢。

1 个答案:

答案 0 :(得分:0)

现在正在工作。这是因为我正在使用页面转换动画,这在主线程上引起了很多工作。我删除了动画,现在它正常工作。