gpt4 book ai didi

java - 房间数据库 - AsyncTask 未执行

转载 作者:行者123 更新时间:2023-12-01 16:53:34 25 4
gpt4 key购买 nike

我已经实现了一个房间数据库,并且有许多查询事务在与 UI 不同的线程中运行。此特定查询出现无法访问主线程上的数据库的异常,因为它可能会长时间锁定 UI。我不明白为什么这个特定的事务试图在 UI 线程上运行。 附件为非法状态异常。

2020-05-06 06:40:33.365 23022-23022/com.grgapps.checkingin E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.grgapps.checkingin, PID: 23022
java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time.
at androidx.room.RoomDatabase.assertNotMainThread(RoomDatabase.java:267)
at androidx.room.RoomDatabase.query(RoomDatabase.java:323)
at androidx.room.util.DBUtil.query(DBUtil.java:83)
at com.grgapps.checkingin.ContactTableDao_Impl.findContactByContactTablePrimaryKey(ContactTableDao_Impl.java:350)
at com.grgapps.checkingin.CheckInContactsFilterAndDisplayAdapter.deleteItemFromContactListDisplay(CheckInContactsFilterAndDisplayAdapter.java:116)
at com.grgapps.checkingin.CheckInContactsFilterAndDisplayAdapter$1.onClick(CheckInContactsFilterAndDisplayAdapter.java:178)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

这是联系表。

package com.grgapps.checkingin;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.ContactsContract;
import android.widget.QuickContactBadge;

import androidx.annotation.NonNull;
import androidx.room.Entity;
import androidx.room.Index;
import androidx.room.PrimaryKey;

import java.sql.Blob;
import java.util.BitSet;

@Entity(tableName = "contact_table",
indices = {@Index("contactTablePrimaryKey"),
@Index("contactContractsKey")})


public class ContactTable {

@PrimaryKey(autoGenerate = true)
@NonNull
private int contactTablePrimaryKey;

@NonNull
private String contactContractsKey;

@NonNull

private String contactDisplayName;

@NonNull
private String contactLastName;

@NonNull
private String contactFirstName;

private String contactMiddleName;

这是启动查询的代码

ContactTable contactFindContactByContactTablePrimaryKeyQuery = contactTableDao
.findContactByContactTablePrimaryKey(mContactTablePrimaryKey);

这是房间数据库 DataAccessObject

   @Query("SELECT * FROM contact_table WHERE contactTablePrimaryKey = :contactTablePrimaryKey")
public abstract ContactTable findContactByContactTablePrimaryKey(int contactTablePrimaryKey);

这是带有异步任务代码的房间数据库表存储库

private static class FindContactByContactTablePrimaryKeyQueryAsyncTask extends
AsyncTask<Integer, Void, ContactTable> {

private ContactTableDao asyncTaskDao;
private ContactTableRepository delegate = null;

FindContactByContactTablePrimaryKeyQueryAsyncTask(ContactTableDao dao) {
asyncTaskDao = dao;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}

@Override
protected ContactTable doInBackground(final Integer... params) {
Log.i(TAG, "ContactTableRepository doInBackground: ");
return asyncTaskDao.findContactByContactTablePrimaryKey(params[0]);
}
@Override
protected void onPostExecute(ContactTable result) {
delegate.findContactByContactTablePrimaryKeyAsyncFinished(result);
}
}
private void findContactByContactTablePrimaryKeyAsyncFinished(ContactTable results) {
Log.i(TAG, "ContactTableRepository asyncFinished: ");
foundContactByContactTablePrimaryKey = results;
}
'''


最佳答案

在使用 room 时使用 Asynctask 并不是一个好主意,所以我向您推荐这个来自 Google 的项目。 https://github.com/googlecodelabs/android-room-with-a-view

关于java - 房间数据库 - AsyncTask 未执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61634498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com