Android MongoDB 境界:跳过28帧!应用程序可能在其主线程上做了太多工作
Android MongoDB Realm: Skipped 28 frames! The application may be doing too much work on its main thread
即使我在 Realm 中使用异步,我也遇到了这个问题。滞后非常明显。我的编码方式有问题吗?这是我的代码:
RealmConfiguration configuration = new RealmConfiguration.Builder()
.deleteRealmIfMigrationNeeded()
.build();
realm = Realm.getInstance(configuration);
itemCategoryName = getIntent().getExtras().getString("category", "");
itemCategoryName = itemCategoryName.toLowerCase();
shopItemList = new ArrayList<>();
shopItemAdapter = new ShopItemAdapter(this, shopItemList);
recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
recyclerView.setAdapter(shopItemAdapter);
realm.where(ShopItem.class).contains("category", itemCategoryName, Case.INSENSITIVE).findAllAsync()
.addChangeListener(new RealmChangeListener<RealmResults<ShopItem>>() {
@Override
public void onChange(RealmResults<ShopItem> shopItems) {
shopItemList.addAll(shopItems);
shopItemAdapter.notifyDataSetChanged();
}
});
您的代码看起来不错,请记住,网络、数据库操作等不应该 运行 在 UI 线程上。
为这个任务使用异步任务、其他线程等,这样你的应用程序就不会延迟加载或执行这个任务。
Check this post for more information.
似乎 onChange() 在 UI 线程上,因为当我评论 notifyDataSetChanged() 时,适配器显示了列表。我刚刚在 onChange() 中创建了一个新的 runnable,然后它解决了延迟问题。
@Override
public void onChange(final RealmResults<ShopItem> shopItems) {
Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
shopItemList.addAll(shopItems);
shopItemAdapter.notifyDataSetChanged();
}
});
}
即使我在 Realm 中使用异步,我也遇到了这个问题。滞后非常明显。我的编码方式有问题吗?这是我的代码:
RealmConfiguration configuration = new RealmConfiguration.Builder()
.deleteRealmIfMigrationNeeded()
.build();
realm = Realm.getInstance(configuration);
itemCategoryName = getIntent().getExtras().getString("category", "");
itemCategoryName = itemCategoryName.toLowerCase();
shopItemList = new ArrayList<>();
shopItemAdapter = new ShopItemAdapter(this, shopItemList);
recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
recyclerView.setAdapter(shopItemAdapter);
realm.where(ShopItem.class).contains("category", itemCategoryName, Case.INSENSITIVE).findAllAsync()
.addChangeListener(new RealmChangeListener<RealmResults<ShopItem>>() {
@Override
public void onChange(RealmResults<ShopItem> shopItems) {
shopItemList.addAll(shopItems);
shopItemAdapter.notifyDataSetChanged();
}
});
您的代码看起来不错,请记住,网络、数据库操作等不应该 运行 在 UI 线程上。 为这个任务使用异步任务、其他线程等,这样你的应用程序就不会延迟加载或执行这个任务。
Check this post for more information.
似乎 onChange() 在 UI 线程上,因为当我评论 notifyDataSetChanged() 时,适配器显示了列表。我刚刚在 onChange() 中创建了一个新的 runnable,然后它解决了延迟问题。
@Override
public void onChange(final RealmResults<ShopItem> shopItems) {
Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
shopItemList.addAll(shopItems);
shopItemAdapter.notifyDataSetChanged();
}
});
}