Room database not require new Thread for long query operation












0















Everywhere the same information, long time operation need to run in other thread, different than UI thread, for example - operation with Database.

...and its true, if I try to save something into DB I got:




Cannot access database on the main thread since it may potentially
lock the UI for a long period of time.




Here is my question, why I'm able get all data from database from UI ? :)

(my database didn't have .allowMainThreadQueries()"



This is a little modified code from google documentation:



ViewModel:



public class LocationsViewModel extends AndroidViewModel {

private LocationRepository locationRepository;
private LiveData<List<LocationModel>> allLocationsLiveData;

public LiveData<List<LocationModel>> getAllLocationsLiveData() {
return allLocationsLiveData;
}

public LocationsViewModel(@NonNull Application application) {
super(application);
locationRepository = new LocationRepository(application);
allLocationsLiveData = locationRepository.getAllLocation();
}
}


Repository:



public class LocationRepository {

private final LocationDao locationDao;
private LiveData<List<LocationModel>> allLocation;

LiveData<List<LocationModel>> getAllLocation() {
return allLocation;
}

LocationRepository(Application application){
LocationsDatabase db = LocationsDatabase.getDatabase(application);
locationDao = db.locationDao();
allLocation = locationDao.getAllLocations();
}

}


init method from Fragment:



private void initData() {
locationsViewModel = ViewModelProviders.of(this).get(LocationsViewModel.class);
locationsViewModel.getAllLocationsLiveData().observe(this, new Observer<List<LocationModel>>() {
@Override
public void onChanged(@Nullable List<LocationModel> locations) {
mAdapter.setLocationList(locations);
}
});
}


Dao:



@Dao
public interface LocationDao {
@Insert(onConflict = OnConflictStrategy.IGNORE)
void insert(LocationModel... locations);

@Query("SELECT * FROM LocationModel")
LiveData<List<LocationModel>> getAllLocations();
}


So why I'm able to getFromDatabse from UI, but need other thread for save ?










share|improve this question


















  • 1





    see generated LocationDao#getAllLocations method sources

    – pskink
    Nov 25 '18 at 11:51











  • what I should saw there ?

    – purcha
    Nov 25 '18 at 11:56






  • 1





    the method implementation

    – pskink
    Nov 25 '18 at 11:57








  • 1





    does getAllLocations() method implementation return ComputableLiveData?

    – pskink
    Nov 25 '18 at 12:49






  • 1





    see what parameter is passed to ComputableLiveData constructor

    – pskink
    Nov 25 '18 at 12:52
















0















Everywhere the same information, long time operation need to run in other thread, different than UI thread, for example - operation with Database.

...and its true, if I try to save something into DB I got:




Cannot access database on the main thread since it may potentially
lock the UI for a long period of time.




Here is my question, why I'm able get all data from database from UI ? :)

(my database didn't have .allowMainThreadQueries()"



This is a little modified code from google documentation:



ViewModel:



public class LocationsViewModel extends AndroidViewModel {

private LocationRepository locationRepository;
private LiveData<List<LocationModel>> allLocationsLiveData;

public LiveData<List<LocationModel>> getAllLocationsLiveData() {
return allLocationsLiveData;
}

public LocationsViewModel(@NonNull Application application) {
super(application);
locationRepository = new LocationRepository(application);
allLocationsLiveData = locationRepository.getAllLocation();
}
}


Repository:



public class LocationRepository {

private final LocationDao locationDao;
private LiveData<List<LocationModel>> allLocation;

LiveData<List<LocationModel>> getAllLocation() {
return allLocation;
}

LocationRepository(Application application){
LocationsDatabase db = LocationsDatabase.getDatabase(application);
locationDao = db.locationDao();
allLocation = locationDao.getAllLocations();
}

}


init method from Fragment:



private void initData() {
locationsViewModel = ViewModelProviders.of(this).get(LocationsViewModel.class);
locationsViewModel.getAllLocationsLiveData().observe(this, new Observer<List<LocationModel>>() {
@Override
public void onChanged(@Nullable List<LocationModel> locations) {
mAdapter.setLocationList(locations);
}
});
}


Dao:



@Dao
public interface LocationDao {
@Insert(onConflict = OnConflictStrategy.IGNORE)
void insert(LocationModel... locations);

@Query("SELECT * FROM LocationModel")
LiveData<List<LocationModel>> getAllLocations();
}


So why I'm able to getFromDatabse from UI, but need other thread for save ?










share|improve this question


















  • 1





    see generated LocationDao#getAllLocations method sources

    – pskink
    Nov 25 '18 at 11:51











  • what I should saw there ?

    – purcha
    Nov 25 '18 at 11:56






  • 1





    the method implementation

    – pskink
    Nov 25 '18 at 11:57








  • 1





    does getAllLocations() method implementation return ComputableLiveData?

    – pskink
    Nov 25 '18 at 12:49






  • 1





    see what parameter is passed to ComputableLiveData constructor

    – pskink
    Nov 25 '18 at 12:52














0












0








0








Everywhere the same information, long time operation need to run in other thread, different than UI thread, for example - operation with Database.

...and its true, if I try to save something into DB I got:




Cannot access database on the main thread since it may potentially
lock the UI for a long period of time.




Here is my question, why I'm able get all data from database from UI ? :)

(my database didn't have .allowMainThreadQueries()"



This is a little modified code from google documentation:



ViewModel:



public class LocationsViewModel extends AndroidViewModel {

private LocationRepository locationRepository;
private LiveData<List<LocationModel>> allLocationsLiveData;

public LiveData<List<LocationModel>> getAllLocationsLiveData() {
return allLocationsLiveData;
}

public LocationsViewModel(@NonNull Application application) {
super(application);
locationRepository = new LocationRepository(application);
allLocationsLiveData = locationRepository.getAllLocation();
}
}


Repository:



public class LocationRepository {

private final LocationDao locationDao;
private LiveData<List<LocationModel>> allLocation;

LiveData<List<LocationModel>> getAllLocation() {
return allLocation;
}

LocationRepository(Application application){
LocationsDatabase db = LocationsDatabase.getDatabase(application);
locationDao = db.locationDao();
allLocation = locationDao.getAllLocations();
}

}


init method from Fragment:



private void initData() {
locationsViewModel = ViewModelProviders.of(this).get(LocationsViewModel.class);
locationsViewModel.getAllLocationsLiveData().observe(this, new Observer<List<LocationModel>>() {
@Override
public void onChanged(@Nullable List<LocationModel> locations) {
mAdapter.setLocationList(locations);
}
});
}


Dao:



@Dao
public interface LocationDao {
@Insert(onConflict = OnConflictStrategy.IGNORE)
void insert(LocationModel... locations);

@Query("SELECT * FROM LocationModel")
LiveData<List<LocationModel>> getAllLocations();
}


So why I'm able to getFromDatabse from UI, but need other thread for save ?










share|improve this question














Everywhere the same information, long time operation need to run in other thread, different than UI thread, for example - operation with Database.

...and its true, if I try to save something into DB I got:




Cannot access database on the main thread since it may potentially
lock the UI for a long period of time.




Here is my question, why I'm able get all data from database from UI ? :)

(my database didn't have .allowMainThreadQueries()"



This is a little modified code from google documentation:



ViewModel:



public class LocationsViewModel extends AndroidViewModel {

private LocationRepository locationRepository;
private LiveData<List<LocationModel>> allLocationsLiveData;

public LiveData<List<LocationModel>> getAllLocationsLiveData() {
return allLocationsLiveData;
}

public LocationsViewModel(@NonNull Application application) {
super(application);
locationRepository = new LocationRepository(application);
allLocationsLiveData = locationRepository.getAllLocation();
}
}


Repository:



public class LocationRepository {

private final LocationDao locationDao;
private LiveData<List<LocationModel>> allLocation;

LiveData<List<LocationModel>> getAllLocation() {
return allLocation;
}

LocationRepository(Application application){
LocationsDatabase db = LocationsDatabase.getDatabase(application);
locationDao = db.locationDao();
allLocation = locationDao.getAllLocations();
}

}


init method from Fragment:



private void initData() {
locationsViewModel = ViewModelProviders.of(this).get(LocationsViewModel.class);
locationsViewModel.getAllLocationsLiveData().observe(this, new Observer<List<LocationModel>>() {
@Override
public void onChanged(@Nullable List<LocationModel> locations) {
mAdapter.setLocationList(locations);
}
});
}


Dao:



@Dao
public interface LocationDao {
@Insert(onConflict = OnConflictStrategy.IGNORE)
void insert(LocationModel... locations);

@Query("SELECT * FROM LocationModel")
LiveData<List<LocationModel>> getAllLocations();
}


So why I'm able to getFromDatabse from UI, but need other thread for save ?







java android multithreading android-room android-jetpack






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 11:46









purchapurcha

707




707








  • 1





    see generated LocationDao#getAllLocations method sources

    – pskink
    Nov 25 '18 at 11:51











  • what I should saw there ?

    – purcha
    Nov 25 '18 at 11:56






  • 1





    the method implementation

    – pskink
    Nov 25 '18 at 11:57








  • 1





    does getAllLocations() method implementation return ComputableLiveData?

    – pskink
    Nov 25 '18 at 12:49






  • 1





    see what parameter is passed to ComputableLiveData constructor

    – pskink
    Nov 25 '18 at 12:52














  • 1





    see generated LocationDao#getAllLocations method sources

    – pskink
    Nov 25 '18 at 11:51











  • what I should saw there ?

    – purcha
    Nov 25 '18 at 11:56






  • 1





    the method implementation

    – pskink
    Nov 25 '18 at 11:57








  • 1





    does getAllLocations() method implementation return ComputableLiveData?

    – pskink
    Nov 25 '18 at 12:49






  • 1





    see what parameter is passed to ComputableLiveData constructor

    – pskink
    Nov 25 '18 at 12:52








1




1





see generated LocationDao#getAllLocations method sources

– pskink
Nov 25 '18 at 11:51





see generated LocationDao#getAllLocations method sources

– pskink
Nov 25 '18 at 11:51













what I should saw there ?

– purcha
Nov 25 '18 at 11:56





what I should saw there ?

– purcha
Nov 25 '18 at 11:56




1




1





the method implementation

– pskink
Nov 25 '18 at 11:57







the method implementation

– pskink
Nov 25 '18 at 11:57






1




1





does getAllLocations() method implementation return ComputableLiveData?

– pskink
Nov 25 '18 at 12:49





does getAllLocations() method implementation return ComputableLiveData?

– pskink
Nov 25 '18 at 12:49




1




1





see what parameter is passed to ComputableLiveData constructor

– pskink
Nov 25 '18 at 12:52





see what parameter is passed to ComputableLiveData constructor

– pskink
Nov 25 '18 at 12:52












1 Answer
1






active

oldest

votes


















1














You use LiveData, which literally runs on a background thread.



It does not happen synchronously, so it does not happen on the main thread.



You subscribe for changes, and those changes are calculated on a background thread, and then only given to your Activity/Fragment on the main thread.



When you try to save something, you don't use LiveData (which you aren't supposed to either, so its all good), so it runs on the main thread. Which it shouldn't.

So you'll need to run it in a new thread, create an AsyncTask, or similar.






share|improve this answer
























  • thanks, but could you show me, where exactly is starting new Thread ?

    – purcha
    Nov 25 '18 at 12:17











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467104%2froom-database-not-require-new-thread-for-long-query-operation%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You use LiveData, which literally runs on a background thread.



It does not happen synchronously, so it does not happen on the main thread.



You subscribe for changes, and those changes are calculated on a background thread, and then only given to your Activity/Fragment on the main thread.



When you try to save something, you don't use LiveData (which you aren't supposed to either, so its all good), so it runs on the main thread. Which it shouldn't.

So you'll need to run it in a new thread, create an AsyncTask, or similar.






share|improve this answer
























  • thanks, but could you show me, where exactly is starting new Thread ?

    – purcha
    Nov 25 '18 at 12:17
















1














You use LiveData, which literally runs on a background thread.



It does not happen synchronously, so it does not happen on the main thread.



You subscribe for changes, and those changes are calculated on a background thread, and then only given to your Activity/Fragment on the main thread.



When you try to save something, you don't use LiveData (which you aren't supposed to either, so its all good), so it runs on the main thread. Which it shouldn't.

So you'll need to run it in a new thread, create an AsyncTask, or similar.






share|improve this answer
























  • thanks, but could you show me, where exactly is starting new Thread ?

    – purcha
    Nov 25 '18 at 12:17














1












1








1







You use LiveData, which literally runs on a background thread.



It does not happen synchronously, so it does not happen on the main thread.



You subscribe for changes, and those changes are calculated on a background thread, and then only given to your Activity/Fragment on the main thread.



When you try to save something, you don't use LiveData (which you aren't supposed to either, so its all good), so it runs on the main thread. Which it shouldn't.

So you'll need to run it in a new thread, create an AsyncTask, or similar.






share|improve this answer













You use LiveData, which literally runs on a background thread.



It does not happen synchronously, so it does not happen on the main thread.



You subscribe for changes, and those changes are calculated on a background thread, and then only given to your Activity/Fragment on the main thread.



When you try to save something, you don't use LiveData (which you aren't supposed to either, so its all good), so it runs on the main thread. Which it shouldn't.

So you'll need to run it in a new thread, create an AsyncTask, or similar.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 25 '18 at 11:52









MoonbloomMoonbloom

4,87821528




4,87821528













  • thanks, but could you show me, where exactly is starting new Thread ?

    – purcha
    Nov 25 '18 at 12:17



















  • thanks, but could you show me, where exactly is starting new Thread ?

    – purcha
    Nov 25 '18 at 12:17

















thanks, but could you show me, where exactly is starting new Thread ?

– purcha
Nov 25 '18 at 12:17





thanks, but could you show me, where exactly is starting new Thread ?

– purcha
Nov 25 '18 at 12:17


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467104%2froom-database-not-require-new-thread-for-long-query-operation%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Lallio

Unable to find Lightning Node

Futebolista