[我正在intentservice中创建roomdatabase对象,该对象对我来说有其工作困难,Room也会给我一个错误提示

问题描述 投票:0回答:1

“无法访问主线程上的数据库,因为它可能长时间锁定UI”,但当我有空间在主线程上执行查询时,它不会给出错误

// MyIntentService.java:
public class MyIntentService extends IntentService {

    String URL = "http://192.168.1.102/android/Document%20Sharing/getServerData.php";
    String course;
    AppDatabase appDatabase;

    public MyIntentService() { super("MyIntentService"); }

    @Override
    public void onCreate() {
        Log.i("tagg","onCreate");
        super.onCreate();
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        appDatabase = AppDatabase.getAppDatabase(this);
        course = intent.getStringExtra("course");
        getServerData();
        Log.i("tagg","onHandleIntentBefore");
    }

    private void getServerData() {
        Log.i("tagg","here");
        final RequestQueue requestQueue = Volley.newRequestQueue(this);
        final StringRequest stringRequest = new StringRequest(Request.Method.POST, URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        IT_Subjects[] itSubjects = gson.fromJson(response, IT_Subjects[].class);
                        appDatabase.itSubjectsDao().insertAll(itSubjects);
                        //Log.i("tagg", response);
                        Log.i("tagg","here");
                        requestQueue.stop();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                        Toast.makeText(MyIntentService.this, error.toString(), Toast.LENGTH_LONG).show();
                        requestQueue.stop();
                    }
                }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> prams = new HashMap<>();
                prams.put("course", course);

                return prams;
            }
        };

        requestQueue.add(stringRequest);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
        Log.i("tagg","onStartCommand");
        return super.onStartCommand(intent,flags,startId);
    }

    @Override
    public void onDestroy() {
        Log.i("tagg","onDestroy");
        AppDatabase.destroyInstance();
        super.onDestroy();
    }

}

//AppDatabase.java
@Database(entities = IT_Subjects.class, version = 1 )
public abstract class AppDatabase extends RoomDatabase {

    public abstract IT_SubjectsDao itSubjectsDao();

    private static AppDatabase INSTANCE;

    public static AppDatabase getAppDatabase(Context context) {
        if (INSTANCE == null) {
            INSTANCE =
                    Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, "IT_Students")
                            .build();
        }
        return INSTANCE;
    }

    public static void destroyInstance() {
        INSTANCE = null;
    }
}
android intentservice android-room
1个回答
0
投票

您排队一个请求-请求是异步处理的,因此onHandleIntent立即返回。 (然后onDestroy被调用,并且您缓存的静态数据库实例丢失!)然后在主线程上调用onResponse,并将数据写入appDatabase

© www.soinside.com 2019 - 2024. All rights reserved.