我在我的项目中使用Realm Adapter进行实时同步。
它有时会起作用但有时会崩溃而产生上述错误,无法弄清楚原因
当我将对象保存到Realm时,它会崩溃,而这是由适配器消耗的
这是我保存的方式
public void saveDestinations(Destination destination) {
mRealm.executeTransaction(realm -> mRealm.copyToRealmOrUpdate(destination));
}
以下是我如何将数据导入我的适配器
private void initUI() {
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
destinationRealmResults = RealmManager.destinationDAO().loadDestination();
adapter = new RecentAdapter(destinationRealmResults, getActivity());
recyclerView.setAdapter(adapter);
validateRecyclerView();
}
这是我的适配器
public class RecentAdapter extends RealmRecyclerViewAdapter<Destination, RecentAdapter.ViewHolder> {
private Context context;
private Preferences preferences;
public RecentAdapter(OrderedRealmCollection<Destination> orderedRealmCollection, Context context) {
super(orderedRealmCollection, true);
this.context = context;
preferences = new Preferences(context);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recent, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.address.setText(getData().get(position).getAddress());
holder.date.setText(getData().get(position).getDate());
if (getData().get(position).isFavorite())
holder.favCheck.setChecked(true);
else
holder.favCheck.setChecked(false);
}
private void animateCheck(CheckBox checkBox) {
checkBox.setScaleX(0);
checkBox.setScaleY(0);
checkBox.animate().scaleX(1).scaleY(1).start();
}
public class ViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.address)
TextView address;
@BindView(R.id.check_fav)
CheckBox favCheck;
@BindView(R.id.date)
TextView date;
@OnClick(R.id.card_view)
public void createDestination() {
if (!preferences.getBoolean(SharedPrefsKeys.HAS_TRACKING_STARTED)) {
new AlertDialog.Builder(context)
.setTitle("Start Trip")
.setMessage("Are you sure you want set this destination?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
EventBus.getDefault().post(new AppEvents.SelectStartTripEvent(getData().get(getAdapterPosition())));
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setIcon(R.drawable.ic_room_red_500_24dp)
.show();
} else {
EventBus.getDefault().post(new AppEvents.StartTripError(true));
}
}
public ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
favCheck.setOnClickListener(v -> {
CheckBox checkBox = (CheckBox) v;
if (checkBox.isChecked()) {
RealmManager.destinationDAO().saveToFavorites(getData().get(getAdapterPosition()));
animateCheck(checkBox);
EventBus.getDefault().post(new AppEvents.UpdateFavDestinationEvent(true));
} else {
RealmManager.destinationDAO().removeFromFavorites(getData().get(getAdapterPosition()));
EventBus.getDefault().post(new AppEvents.UpdateFavDestinationEvent(true));
}
});
}
}
}
活动巴士(活动)
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
public void onStop() {
EventBus.getDefault().unregister(this);
super.onStop();
}
@Subscribe
public void validateRecyclerView(AppEvents.UpdateFavDestinationEvent event) {
if (event.isMustUpdate()) {
validateRecyclerView();
}
}
public void validateRecyclerView() {
if (adapter.getData() != null && adapter.getData().size() == 0) {
loadingLayout.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.GONE);
} else {
loadingLayout.setVisibility(View.GONE);
recyclerView.setVisibility(View.VISIBLE);
}
}