LiveData onChanged空结果

时间:2019-03-06 08:57:50

标签: android observable rx-java dao android-room

EventDao

@Dao
public interface EventDao {    
    @Query("SELECT * FROM events WHERE date_start = :date")
    LiveData<List<EventPatientLocation>> test(Date date);
}

EventRepository

public class EventRepository {
    private EventDao eventDao;

    public EventRepository(Application application) {
        MyDatabase db = MyDatabase.getDatabase(application);
        eventDao = db.eventDao();
    }

    public LiveData<List<EventPatientLocation>> test(Date date) {
        return eventDao.test(date);
    }

EventViewModel

public class EventViewModel extends AndroidViewModel {

    private EventRepository repository;

    public EventViewModel(@NonNull Application application) {
        super(application);
        repository = new EventRepository(application);
    }

    public LiveData<List<EventPatientLocation>> test(Date date) {
        return repository.test(date);
    }
}

片段(onActivityCreated)

eventViewModel = ViewModelProviders.of(activity).get(EventViewModel.class);

LiveData<List<EventPatientLocation>> testEvent = eventViewModel.test(new Date());

testEvent.observe(activity, events -> {
    // events is always null!
});

尽管片段上的onChanged回调被多次调用,但是事件列表始终为空...我可以肯定的是,在数据库中有查询的候选行,所以我在哪里做错了?请帮助...

0 个答案:

没有答案