我正在尝试从后台服务中的Android Room数据库读取数据。没有例外,但没有数据返回。
我编写了一个函数,用于从DAO中的表中选择所有行。从后台服务调用该函数成功,但是不返回任何数据。
我的“联系人”类保存联系人信息(姓名,电话号码,电子邮件)并定义数据库架构。该数据库包含联系人行,其中包含姓名,电话号码和电子邮件作为列。
在DAO中返回LiveData的函数是:
@Query("SELECT * FROM contacts_table")
LiveData<List<Contact>> getAll();
其中,“ contacts_table”是保存联系信息的数据库表。
我叫getAll如下:
AppDatabase db = AppDatabase.getDatabase(messageSenderContext.getApplicationContext());
mContactDAO = db.contactDAO();
mAllContacts = mContactDAO.getAll();
其中,mContactDao是ContactDAO(Contact类的数据库访问对象),而mAllContacts是LiveData>。这些是调用getAll()的类的私有字段。
db.contactDAO()返回一个对象,与mContactDAO.getAll()一样。但是,尝试使用mAllContacts.getValue()从mAllContacts解压缩列表会返回null。
答案 0 :(得分:0)
事实证明这是对LiveData的滥用。这需要观察者实际获取数据。
答案 1 :(得分:0)
在您的房间
@Database(entities={Contact.class}, version = 1, exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {
public static final String DATABASE_NAME = "AppDatabase.db";
private static volatile AppDatabase INSTANCE;
private static final int NUMBER_OF_THREADS = 4;
public static final ExecutorService EXECUTOR_SERVICE = Executors.newFixedThreadPool(NUMBER_OF_THREADS);
public abstract ContactDAO contactDAO();
public static AppDatabase getDatabase(final Context context) {
if (INSTANCE == null) {
synchronized (AppDatabase.class) {
if (INSTANCE == null) {
INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
AppDatabase.class, DATABASE_NAME)
.build();
}
}
}
return INSTANCE;
}
}
在您的DAO中
@Dao
public interface ContactDAO{
@Query("SELECT * FROM contacts_table")
LiveData<List<Contact>> getAll();
}
在您的存储库中:
public class AppRepository {
private ContactDAO mContactDAO;
//constructor
public AppRepository(Application application) {
AppDatabase db = AppDatabase.getDatabase(application);
mContactDAO= db.contactDAO();
}
public LiveData<List<Contact>> getAllContacts(){
LiveData<List<Contact>> contactsList = null;
Future<LiveData<List<Contact>>> futureList = AppDatabase.EXECUTOR_SERVICE.submit(new Callable(){
@Override
public LiveData<List<Contact>> call() {
return contactDAO.getAll();
}
});
try {
contactsList = futureList.get();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return contactsList ;
}
}
在您的ViewModel中
public class ContactsViewModel extends AndroidViewModel {
private AppRepository appRepository;
private LiveData<List<Contact>> contactsList;
public ContactsViewModel(@NonNull Application application) {
super(application);
appRepository = new AppRepository(application);
}
public LiveData<List<Contacts>> list() {
return appRepository.getAllContacts();
}
}
在您的活动中(在onCreated看跌期权之内)
ContactsViewModel contactsViewModel = new ViewModelProvider(this).get(ContactsViewModel.class);
contactsViewModel.list().observe(getViewLifecycleOwner(), new Observer<List<Contact>>() {
@Override
public void onChanged(List<Contact> contactsList) {
//the contact list will be observed and will return data if there are changes.
//use for example to feed the adapter of a recyclerview
//below an example just to view the contacts data
for(Contact conctact : contactsList){
Log.d("TestApp>>>", "Id: + contact.getId);
Log.d("TestApp>>>", "Name: + contact.getName);
}
});