为什么会议室数据库数据加载非常长而缓慢

时间:2019-04-30 09:41:20

标签: android database caching

我使用会议室数据库在应用程序中缓存一些数据。但是,当我在模拟器或真实手机中运行该应用程序时,通常需要5秒钟才能从数据库中加载数据并显示...这很长时间...甚至我的数据都很少 谁能帮我解决这个问题? 谢谢

ordeeDao.java:

  @Query("SELECT * FROM tbl_orders")
        List<Orders> getOrders();

和我的CacheDatabase类:

 @Database(entities = {Orders.class}, version = 1,exportSchema = false)
       public abstract class CacheDatabase extends RoomDatabase {

            private static CacheDatabase INSTANCE;

            public abstract OrdersDao ordersDao();

            public static CacheDatabase getInMemoryDatabase(Context context) {
                if (INSTANCE == null) {
                    INSTANCE =
                            Room.databaseBuilder(context.getApplicationContext(), CacheDatabase.class, "CacheDb.sql")
                                    .allowMainThreadQueries()
                                    .build();
                }
                return INSTANCE;
            }

            public static void destroyInstance() {
                INSTANCE = null;
            }
        DataBase.java:
          public JSONArray getOrders(){
                try {
                    return new JSONArray(new GsonBuilder().create().toJson(db.ordersDao().getOrders()));
                } catch (JSONException e) {
                    e.printStackTrace();
                    return null;
                }
            }
             private void SetOrdersDB() {
                JSONArray ordersArray=db.getOrders();
                List<MainPojo.Order> ordersList=new ArrayList<>();
                for (int i=0;i<ordersArray.length();i++){
                    JSONObject object;
                    MainPojo.Order order=new MainPojo.Order();
                    try {
                        object=ordersArray.getJSONObject(i);
                        order.setId(object.getInt("id"));
                        order.setTicket_id(object.getString("ticket_id"));
                        order.setService(object.getString("service"));
                        order.setStatus(object.getString("status"));
                        order.setType(object.getString("type"));
                        order.setFromCurrency(object.getString("from_currency"));
                        order.setFromAmount(object.getString("from_amount"));
                        order.setToCurrency(object.getString("to_currency"));
                        order.setToAmount(object.getString("to_amount"));
                        order.setPayableAmount(object.getString("payable_amount"));
                        order.setFee(object.getString("fee"));
                        order.setDiscount(object.getString("discount"));
                        order.setPaymentMethod(object.getString("payment_method"));
                        order.setPaymentGateway(object.getString("payment_gateway"));
                        order.setPaymentRef(object.getString("payment_ref"));
                        order.setPayedAt(object.getString("payed_at"));
                        order.setExpiresAt(object.getString("expires_at"));
                        order.setHandledAt(object.getString("handled_at"));
                        order.setCreatedAt(object.getString("created_at"));
                        order.setUpdatedAt(object.getString("updated_at"));
                        order.setIcon(object.getString("icon"));
                        order.setAmount(object.getString("amount"));
                        ordersList.add(order);
                    }catch (Exception e){
                        e.getMessage();
                    }
                }
                ordersFragment=new OrdersFragment(ordersList,this);
            }

0 个答案:

没有答案