ContentProvider insert() always runs on UI thread? - java

I have an app that needs to pull data from a server and insert it into an SQLite database in response to user input. I thought this would be pretty simple - the code that pulls the data from the server is a fairly straightforward subclass of AsyncTask, and it works exactly as I expect it to without hanging the UI thread. I implemented callback functionality for it with a simple interface and wrapped it in a static class, so my code looks like this:
MyServerCaller.getFolderContents(folderId, new OnFolderContentsResponseListener() {
#Override
public void onFolderContentsResponse(final List<FilesystemEntry> contents) {
// do something with contents
}
}
All still good. Even if the server takes an hour to retrieve the data, the UI still runs smoothly, because the code in getFolderContents is running in the doInBackground method of an AsyncTask (which is in a separate thread from the UI). At the very end of the getFolderContents method, the onFolderContentsResponse is called and passed the list of FilesystemEntry's that was received from the server. I only really say all this so that it's hopefully clear that my problem is not in the getFolderContents method or in any of my networking code, because it doesn't ever occur there.
The problem arises when I try to insert into a database via my subclass of ContentProvider within the onFolderContentsResponse method; the UI always hangs while that code is executing, leading me to believe that despite being called from the doInBackground method of an AsyncTask, the inserts are somehow still running on the UI thread. Here's what the problematic code looks like:
MyServerCaller.getFolderContents(folderId, new OnFolderContentsResponseListener() {
#Override
public void onFolderContentsResponse(final List<FilesystemEntry> contents) {
insertContentsIntoDB(contents);
}
}
And the insertContentsIntoDB method:
void insertContentsIntoDB(final List<FilesystemEntry> contents) {
for (FilesystemEntry entry : contents) {
ContentValues values = new ContentValues();
values.put(COLUMN_1, entry.attr1);
values.put(COLUMN_2, entry.attr2);
// etc.
mContentResolver.insert(MyContentProvider.CONTENT_URI, values);
}
}
where mContentResolver has been previously set to the result of the getContentResolver() method.
I've tried putting insertContentsIntoDB in its own Thread, like so:
MyServerCaller.getFolderContents(folderId, new OnFolderContentsResponseListener() {
#Override
public void onFolderContentsResponse(final List<FilesystemEntry> contents) {
new Thread(new Runnable() {
#Override
public void run() {
insertContentsIntoDB(contents);
}
}).run();
}
}
I've also tried running each individual insert in its own thread (the insert method in MyContentProvider is synchronized, so this shouldn't cause any issues there):
void insertContentsIntoDB(final List<FilesystemEntry> contents) {
for (FilesystemEntry entry : contents) {
new Thread(new Runnable() {
#Override
public void run() {
ContentValues values = new ContentValues();
values.put(COLUMN_1, entry.attr1);
values.put(COLUMN_2, entry.attr2);
// etc.
mContentResolver.insert(MyContentProvider.CONTENT_URI, values);
}
}).run();
}
}
And just for good measure, I've also tried both of those solutions with the relevant code in the doInBackground method of another AsyncTask. Finally, I've explicitly defined MyContentProvider as living in a separate process in my AndroidManifest.xml:
<provider android:name=".MyContentProvider" android:process=":remote"/>
It runs fine, but it still seems to run in the UI thread. That's the point where I really started tearing my hair out over this, because that doesn't make any sense at all to me. No matter what I do, the UI always hangs during the inserts. Is there any way to get them not to?

Instead of calling mContentResolver.insert(), use AsyncQueryHandler and its startInsert() method. AsyncQueryHandler is designed to facilitate asynchronous ContentResolver queries.

I think your original problem may have been that you are calling the run method on your new thread (which causes execution to continue on the current thread) instead of calling the start method. I think this is what Bright Great was trying to say in his/her answer. See Difference between running and starting a thread. It's a common mistake.

Man.Relax yourself.And anything would looks better.
At first,Start a Thread is Func start not Func run,if you want to start the new Thread
not only call the func run.
new Thread(Runnable runnable).start();
Then I bet use Handler sometimes would be better than AsyncTask.

You can run the query in the doInBackground(Integer int) overridden method of the AsynTask, and update the main UI on the onPostExecute(Integer int) method.

Related

The work manader's work is not controlled

I have a wok manager that I run from the Main Activity when the user logs into the application. So, I will tell you in more detail what I do in the manager: in it I start a stream in which every second there is a mining of the virtual currency of my application, that is, simply put, I just increase the variable every second.
Moving on to the problem, here's how I run the manager
Constraints constraints = new Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build();
miningWorkRequest = new
OneTimeWorkRequest.Builder(MiningManager.class)
.setConstraints(constraints)
.build();
WorkManager.getInstance(getApplicationContext()).enqueue(miningWorkRequest);
Launching the manager completely as in the documentation.
And now the manager himself is with my mining stream. Before increasing the variable, I get it every second from Firebase Realtime, and then the miningMoneyFun() method is triggered to increase it.
#NonNull
#Override
public Result doWork() {
firebaseModel.initAll();
RecentMethods.UserNickByUid(firebaseModel.getUser().getUid(), firebaseModel, new Callbacks.GetUserNickByUid() {
#Override
public void PassUserNick(String nick) {
RecentMethods.GetActiveMiner(nick, firebaseModel, new Callbacks.GetActiveMiners() {
#Override
public void GetActiveMiners(ArrayList<Miner> activeMinersFromBase) {
if(activeMinersFromBase.size()>0){
Thread thread = new Thread()
{
#Override
public void run() {
try {
RecentMethods.UserNickByUid(firebaseModel.getUser().getUid(), firebaseModel, new Callbacks.GetUserNickByUid() {
#Override
public void PassUserNick(String nick) {
RecentMethods.GetTodayMining(nick, firebaseModel, new Callbacks.GetTodayMining() {
#Override
public void GetTodayMining(double todayMiningFromBase) {
todayMining=todayMiningFromBase;
}
});
}
});
while(true) {
Thread.sleep(1000);
miningMoneyFun();
Log.d("#####", "go "+ todayMining);
}
} catch (InterruptedException e) {
}
}
};
thread.start();
}
}
});
}
});
return Result.success();
What specifically does not suit me, I see from the log that the thread can be executed 5, 10 or even 15 times per second. I thought it was a thread, but when I commented it out and put the log in the DoWork() method, the log also appeared many times per second. I want the DoWork() method to run once, and then the thread itself functions every second and as expected. I saw 2 similar questions on StackOverflow, but none had clear answers, please help and sorry for the English
Are you sure that it is only one worker?
Please check like this the number of active works:
https://developer.android.com/studio/inspect/task#view-workers
Also, you should be using unique work to be sure that there are no multiple workers:
https://developer.android.com/topic/libraries/architecture/workmanager/how-to/managing-work#unique-work
EDITED:
I am more confused by your comment. Please provide screenshots and the code the enqueue the work. At the moment you don't use periodic and unique.
Please note that you can have only 1 work in the inspector, but you can see a big list of all the executions of it.
Also, are you sure about the id? Do you generate it or it is static. It should not be possible to have multiple works when it is unique.
What flag do you use? KEEP?
Also now I saw your code in the Worker. I don't see how you block the doWork() to finish. I think you return Success, but at the same time, you have another Thread running with nothing to prevent the app to be killed.

Java: Stuck in the while loop in the background thread

I'm using LibGDX AsyncExecutor and the following function is called in a background thread. 'tasksForTheMainThread' is the static array of Runnable, which executes its not yet executed elements during each call of the update function in the main thread. The function 'createBox' of 'modelBuilder' creates and returns an object of the class 'Model'.
Briefly explaining, this code executes in the second thread and sends a piece of code (function 'run()') to be used in the first thread. After it's sent, the second thread is frozen until the moment the code in "run()" is completed and the Model object is created (or at least it's supposed to be like that).
However, it works as expected only when the while loop (which just waits until the object is created in the main thread) contains the logging bit (Gdx.app.log("TAG","2");). When it's empty, the second thread freezes forever and never reaches 'point A' even after the creation of the Model object.
Why and how logging can influence that? And why isn't the programme working without it?
void secondThreadFunction()
{
Model model = null;
ChunkManager.tasksForTheMainThread.add(new Runnable()
{
#Override
public void run()
{
model = modelBuilder.createBox(size.x, size.y, size.z, GL20.GL_LINES,
new Material(ColorAttribute.createDiffuse(Color.YELLOW)),
VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal);
}
});
while (model == null)
{
//Gdx.app.log("TAG","2");
}
//point A
}
You cannot modify a local variable that has been captured to an inner class. Since it has been "captured", you will operate on a copy of the value and it will never be non-null, causing the endless loop. Also note that you are busy-waiting in a tight loop. It might be better to use a Future of some kind.
void secondThreadFunction()
{
AtomicReference<Model> model = new AtomicReference<Model>();
ChunkManager.tasksForTheMainThread.add(new Runnable()
{
#Override
public void run()
{
model.set(modelBuilder.createBox(size.x, size.y, size.z, GL20.GL_LINES,
new Material(ColorAttribute.createDiffuse(Color.YELLOW)),
VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal));
}
});
while (model == null)
{
//Gdx.app.log("TAG","2");
}
//point A
}

if called Cancel or stop same function then start fresh if presently running in Android

How do I within Android, if a function is called which is already running, cancel/stop that function then start it from the beginning?
My code looks like the below, basically throughout my application a given function( Important(); ) is called but rather than have that function run several times or prevent users from running the function again if presently running. I'd like to Cancel/stop the currently running function, then start it fresh?
How can I go about doing this?
Code:
public void Important() {
//Do lots of stuff
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Important();
}
#Override
protected void onResume() {
super.onResume();
Important();
}
I've already attempted the below making use of booleans. But it just stops a user from running the function again if already running? (which isn't what I'm trying to do)
I want, when the function is called again. The running function is stopped. Then freshly executed
Code:
Boolean runningCode = false;
public void Important() {
if (runningCode== false) {
runningCode = true;
//Do lots of stuff
runningCode = false;
}
F.Y.I
Important() is called many times throughout my application (in addition to the above), sometimes whilst Important() is still running. Hence the need for a solution to my question :-)
Tasks like resetting variables and formatting the page are carried out within Important();, it is not running on a separate thread. These are tasks that need to be completed on the main UI thread
You can use a Handler on the UI thread and post messages or Runnables to it. Make every discrete action in the Important() function be a message or Runnable containing a function (e.g., resetVariables(), formatThePage(), etc). Whenever you call the function again, just call removeCallbacksAndMessages on the Handler and it will drop all actions that have not yet been executed.
Well, I don't understand why you want to do something like this?
There is not one simple answer to that. Probably working solution would be something like this
public void Important() {
if (runningCode == false) {
runningCode = true;
//Do lots of stuff
runningCode = false;
} else {
// stop current code from execution
runningCode = false;
Important();
}
But you must figure your own code to stop current execution as it may result in broken state of objects etc...

How to use Runnable.wait() in AsyncTask? Why does the AsyncTask NOT wait...?

I am using AsyncTask to run a background operation. Of course switching to another thread while already working in a background thread does not make a lot of sense in general, except the other thread is the UI thread. This what I would like to to: While the task is running I need to "access" the UI, e.g. to show a dialog to ask the user how to proceed.
run the background task
stop the task at some point to get user feedback
switch to the UI thread to show dialog and ask for input
switch back to background task and continue work
How can this be done? I thought I could use Runnable with myActivity.runOnUiThread(runnable) but this does not work:
private void copyFiles() {
CopyTask copyTask = new CopyTask(this);
copyTask.execute();
}
// CustomAsyncTask is a AsyncTask subclass that takes a reference to the current
// activity as parameter
private class CopyTask extends CustomAsyncTask<Void, Void, Void> {
private doCopy;
#Override
protected Boolean doInBackground(Void... params) {
// Custom code, e.g. copy files from A to B and check for conflict
for (File file : allFiles) {
doCopy = true;
if (isConflict(file)) {
// Stop current thread and ask for user feedback on UI Thread
Runnable uiRunnable = new Runnable() {
public void run() {
// Pos 1. --> Execute custom code, e.g. use AlertDialog to ask user if file should be replaced...
doCopy = false;
synchronized (this) {
this.notify();
}
}
});
synchronized(uiRunnable) {
// Execute code on UI thread
activity.runOnUiThread(uiRunnable);
// Wait until runnable finished
try {
uiRunnable.wait();
}
catch (InterruptedException e ) {
e.printStackTrace();
}
}
}
// Pos 2. --> Continue work
if (doCopy)
copyFromAToB(File);
}
return null;
}
}
Within doInBackground() (--> in a background thread) the AsyncTask calls activity.runOnUiThread(uiRunnable). Next uiRunnable.wait() is called. Regarding to the docu wait() should do the following:
Causes the calling thread to wait until another thread calls the
notify() or notifyAll() method of this object.
Thus the background thread should wait to continue its work until this.notify() (== uiRunnable.notifiy()) is called on another thread (= the UI thread), shouldn't it?
Well, id does not wait! After calling uiRunnable.wait() the background thread immediately continues by jumping to if (doCopy).... It seems that the background thread and the main thread are executed in parallel (not surprising since this is what thread do...) and thus its a race condition whether doCopy = false on the UI thread or if (doCopy) on the background thread is reached first.
How is this possible? Why doesn't wait() works as described? Or am I getting something wrong?
Thank you very much!
EDIT:
To avoid missunderstandings: Of course I know the lifecycle methodes of AsyncTask but as far as I understand them, they are not what I am looking for (see my reply to the comment blow).
Interrupting the AsyncTask as soon as a UI interaction is necessary, query the UI and start a new AsyncTask would be possible of course. However this would result in code which is very hard to read/understand/maintain.
As I understand the docu of wait() everything should work fine here. Primary question is not how to do UI interaction during the lifecycle of an AsyncTask but why wait()does not work as expected.
The Basics
When you start an AsyncTask first the onPreExecute() method runs on the UI thread. You can override this method to make changes to the UI prior to the doInBackground() method running.
After the doInBackground() method finishes, the onPostExecute() method runs on the UI thread, so you can use this to make changes to the UI from here. If you need to make regular changes to the UI Thread during the doInBackground() method you override the onProgressUpdate() method which runs on the UI Thread, and then call it from within doInBackground(), which will allow you to periodically update the UI.
You could use something like the following;
private class DoStuffTask extends AsyncTask {
#Override
protected void doInBackground(Object... args) {
// Do stuff
onProgressUpdate(x);
// Do more stuff
}
#Override
protected void onProgressUpdate(Object... args) {
// Update your UI here
}
}
Now if this doesn't quite do it and you want the AsyncTask to wait for input during the doInBackground() method it is probably worth considering using multiple AsyncTasks instead. You can then finish each AsyncTask, ask for input, and then start a new AsyncTask to continue working.
Given that AlertDialog instances are asynchronous, this is probably the preferred solution because you can start the next AsyncTask from the AlertDialog itself.
Using wait() in an AsyncTask
If you would prefer to use a single AsyncTask you can use wait from within your AsyncTask to prevent execution continuing until some condition is met. Instead of using a new Runnable we are just using two threads in this instance, the thread running doInBackground() and the main thread, and we are synchronizing on the AsycTask itself.
Example below;
public class TestTask extends AsyncTask{
private boolean notified;
private Promptable p;
public interface Promptable { public abstract void prompt(); }
public TestTask(Promptable p){
this.p = p;
}
#Override
protected Object doInBackground(Object... arg0) {
Log.d("First", "First");
onProgressUpdate(null);
synchronized(this){
while(!notified){
try{
this.wait();
}
catch(InterruptedException e){ }
}
}
Log.d("Second", "Second");
return null;
}
#Override
protected void onProgressUpdate(Object... args){
synchronized(this){
notified = true;
p.prompt();
this.notify();
}
}
}
In the example above, assume that your Activity is parsed into the AsyncTask's constructor, and that it implements an interface we create called Promptable. You'll notice that even though we're calling wait() we are putting it in a while loop. If we didn't do this, and somehow notify() got called before wait() then your thread would lock up indefinitely. Also, you can't depend on the fact that your thread will wait forever, so the while loop ensures that it doesn't continue until notify is called.
I hope this helps.

Single Android Thread for Multiple Jobs

I would like to have an application which either loads or saves data through a HTTP request, however the data must interact with the UI thread. Ideally, I would like a single thread to use an IF statement on a message to determine if the request is to "load" or "save".
What would be the simplest way of doing this with the smallest amount of code?
Also, do instances of Handlers run on individual threads?
EDIT: This is the code I am using now:
Handler doStuff = new Handler(){
#Override
public void handleMessage(Message msg){
if(msg.what == 1){
// Load all the information.
// Get the ID from sharedPrefs
SharedPreferences details= getSharedPreferences("details", 0);
String ID = patDetails.getString("id", "error");
// Load up the ID from HTTP
String patInfo = httpInc.getURLContent("info.php?no="+AES.encrypt("387gk3hjbo8sgslksjho87s", ID));
// Separate all the details
patientInfo = patInfo.split("~");
}
if(msg.what == 2){
// Save the data
}
}
};
Eclipse halts the debugging and displays, "Source not found" for StrictMode.class
I suppose it's because it's using the Main thread to access the internet although it's running in individual threads.
Any idea.
Handlers do run on individual threads. Check that link. You should also check out AsyncTask.
I would propose submitting the jobs as Runnable to a single-threaded ExecutorService:
public class SomeClass {
private ExecutorService execService = Executors.newSingleThreadExecutor();
public void doSomething() {
final String someUiData = // retrieve data from UI
execService.submit(new Runnable() {
#Override
public void run() {
// so something time-consuming, which will be executed asynchronously from the UI thread
// you can also access someUiData here...
}
});
}
}
This way, the UI thread will not block whereas you can easily submit a different Runnable for different operations and the ExecutorService will completely take care of keeping it async.
Edit: If you need to interact with the UI, do so before becoming asynchronous and keep the result in final variables.

Categories

Resources