我正在应用程序中设置房间数据库,我想从在自定义表面视图上绘图的用户那里获取数据。
应用程序使用MVVM模式在Android 9上运行,我尝试了不带Room的应用程序,并且该应用程序正常运行。实施Room后,应用不再观察数据更改。我使用google codelabs作为我的应用程序的模板。用户通过单击视图在表面视图上绘制图形的节点,我想将place(节点)的数据存储在数据库中。
地点:
@Entity(tableName = "places")
data class Place(
@Embedded
var mPosition: Point,
@PrimaryKey(autoGenerate = false)
@ColumnInfo(name = "place_id")
var mID: Int = -1,
@ColumnInfo(name = "name")
override var mName: String = "",
@ColumnInfo(name = "tokens")
var mTokens: Int = 0
) : Node() {
override fun isValid(): Boolean {
return mID == -1 && mPosition.x != 0f && mPosition.y != 0f && mTokens >= 0
}
}
PlaceDao:
@Dao
interface PlaceDao {
@Insert
fun addPlace(place: Place)
@Query("SELECT * FROM places ORDER BY place_id ASC")
fun getPlaces(): LiveData<List<Place>>
...
}
数据库:
@Database(entities = [Place::class, Transition::class, Arc::class], version = 1)
@TypeConverters(Converter::class)
abstract class AppRoomDatabase : RoomDatabase() {
abstract fun placeDao(): PlaceDao
companion object {
@Volatile
private var INSTANCE: AppRoomDatabase? = null
fun getDatabase(
context: Context,
scope: CoroutineScope
): AppRoomDatabase {
return INSTANCE ?: synchronized(this) {
val instance = Room.databaseBuilder(
context.applicationContext,
AppRoomDatabase::class.java,
"word_database"
)
.fallbackToDestructiveMigration()
.addCallback(AppDatabaseCallback(scope))
.build()
INSTANCE = instance
instance
}
}
private class AppDatabaseCallback(
private val scope: CoroutineScope
) : RoomDatabase.Callback() {
override fun onOpen(db: SupportSQLiteDatabase) {
super.onOpen(db)
INSTANCE?.let { database ->
scope.launch(Dispatchers.IO) {
populateDatabase(database.placeDao(),database.transitionDao(),database.arcDao())
}
}
}
}
}
}
存储库:
class NetRepositoryImpl(
private val placeDao: PlaceDao,
private val transitionDao: TransitionDao,
private val arcDao: ArcDao
) : NetRepository {
@WorkerThread
override suspend fun addPlace(place: Place) {
placeDao.addPlace(place)
}
override fun getPlaces() = placeDao.getPlaces()
}
ViewModel:
class NetViewModel(application: Application) : AndroidViewModel(application) {
private var parentJob = Job()
private val coroutineContext: CoroutineContext
get() = parentJob + Dispatchers.Main
private val scope = CoroutineScope(coroutineContext)
private val netRepository: NetRepositoryImpl
val allPlaces: LiveData<List<Place>>
...
init {
val placeDao = AppRoomDatabase.getDatabase(application, scope).placeDao()
...
netRepository = NetRepositoryImpl(placeDao, transitionDao, arcDao)
allPlaces = netRepository.getPlaces()
...
}
fun addPlace(place: Place) {
if (place.isValid())
scope.launch(Dispatchers.IO) { netRepository.addPlace(place) }
}
活动:
class EditorActivity : FragmentActivity() {
private lateinit var viewModel: NetViewModel
override fun onCreate(savedInstanceState: Bundle?) {
...
viewModel = ViewModelProviders.of(this).get(NetViewModel::class.java)
viewModel.apply {
allPlaces.observe(this@EditorActivity, Observer { places ->
editorDrawer.mPlaces = places
Log.d("Editor activity", "Places observed")
})
...
editorDrawer.setOnTouchListener { v, event ->
when (event?.action) {
MotionEvent.ACTION_DOWN -> {
...
viewModel.addPlace(Point(event.x, event.y))
...
}
}
...
}
editorDrawer是活动布局中的自定义表面视图。
当用户单击表面视图时,我希望创建一个新的位置并将其存储在数据库中,然后在更改数据时在表面视图上绘制它,创建了新的位置实例,但未存储并且未观察到任何更改,因此没有位置画。我可以在控制台日志中找到
"Places observed"
仅在应用启动时,就像数据完全不变一样……
答案 0 :(得分:0)
好问题与Room或MVVM模式无关,这是我粗心大意的副产品...如果地方实体类的isValid()函数中的条件存在错误,则发生错误...
return mID == -1 && mPosition.x != 0f && mPosition.y != 0f && mTokens >= 0
到
return mID != -1 && mPosition.x != 0f && mPosition.y != 0f && mTokens >= 0