插入服务室而不更新活动中的LiveData [英] Insert to room in service not updating LiveData in activity

查看:67
本文介绍了插入服务室而不更新活动中的LiveData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用聊天室与活动进行通信,以交流由前台位置服务获取的数据.该服务连接到ViewModel并插入数据,但是该活动不会从ViewModel接收更新的LiveData,但是它可以在开始时以正确的大小获取LiveData>对象,然后重新启动应用程序.我在这里想念什么?我需要向数据库中插入新数据,所以如果我确实需要在服务和postValue中使用MutableLiveData,那么我每次都必须发布整个列表...

I'm using room to communicate data fetched by a foreground Location service with the an activity. The service connects to the viewmodel and does insert data, the activity however does not receive updated LiveData from the viewmodel, it is however able to fetch a LiveData> object at the begining, with accurate size when restarting the app. What am I missing here? I need to insert new data into the db, so if I do need to use MutableLiveData in the service and postValue, then I would have to post the entire list everytime...

Activity.java

Activity.java


@Override
protected void onCreate( Bundle savedInstanceState ) {
    super.onCreate( savedInstanceState );

    setContentView( R.layout.track_activity );

    mViewModel = ViewModelProviders.of( this ).get( ViewModel.class );

    mViewModel.getAllData().observe( this, ( @Nullable final <List<eData>> data ) -> {

        if ( data!= null )
            Log.d("DATACOUNT", String.valueOf(data.size()) );

    } );
}

Service.java

Service.java

@Override
public void onCreate() {
    super.onCreate();

    AppDatabase mDB = AppDatabase.getDatabase( this.getApplication() );
    mDataDao = mDB.dataDao();

    mExecutor = Executors.newSingleThreadExecutor();

}

...

private void receiveLocation( LocationResult locationResult ) {

    ...
    mExecutor.execute( () -> mDataDao.insertData( new eData( ... ) ) );

}

DataDao.java

DataDao.java

@Dao
public interface DataDao {

    @Query( "SELECT * FROM eData" )
    LiveData<List<eData>> getAllData();

    @Insert
    long insertData( eData data );
}

AppDatabase.java

AppDatabase.java

@Database(entities = { eData.class }, version = 1 )
public abstract class AppDatabase extends RoomDatabase {

    public abstract DataDao dataDao();

    private static AppDatabase INSTANCE;

    public static AppDatabase getDatabase( final Context context ) {
        if ( INSTANCE == null ) {
            synchronized ( AppDatabase.class ) {
                if ( INSTANCE == null ) {
                    INSTANCE = Room.databaseBuilder( context.getApplicationContext(),
                            AppDatabase.class, "locationapp" )
                                       .build();
                }
            }
        }
        return INSTANCE;
    }

存储库和数据库为单例.但是,以某种方式在活动中观察到的LiveData不会在将实体插入服务中时更新,而是将其插入数据库中,因为当我重新启动应用程序时,计数就会增加.

The Repository and Database are Singletons. But somewhow the LiveData observed in my activity does not update when inserting entities in the service, which it does insert into the database, as when I restart the app, the count goes up.

推荐答案

最终根本没有使用服务中的空间.第一,我不知道它是否会更快,第二,如果我只坚持活动并且只创建一次ViewModel和ExecutorService,它会使用更少的内存.

Ended up not using room in the service at all. One, I didn't know whether it would be faster, and two, it uses less memory if I just stick with the activity and only create ViewModel and ExecutorService once.

这篇关于插入服务室而不更新活动中的LiveData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆