简体   繁体   English

插入服务室而不更新活动中的LiveData

[英]Insert to room in service not updating LiveData in activity

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. 该服务连接到ViewModel并插入数据,但是该活动未从ViewModel接收更新的LiveData,但是该服务能够在开始时以正确的大小获取LiveData>对象,然后重新启动应用程序。 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... 我需要向数据库中插入新数据,因此,如果确实需要在服务和postValue中使用MutableLiveData,则每次都必须发布整个列表...

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;
    }

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. 但是以某种方式在活动中观察到的LiveData在服务中插入实体时不会更新,但确实会插入数据库中,因为当我重新启动应用程序时,计数增加了。

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. 一,我不知道是否会更快,二,如果我只坚持活动并且只创建一次ViewModel和ExecutorService,它会使用更少的内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM