Run background task in Android using MVVM pattern - mvvm

I working on android app using MVVM pattern. I need to perform some background math calculations in my ViewModel class. This task will read some data from file A and then save results in few other files. And return an Int result when it's done to the MainActivity. My question is how can I achieve this? Searched a lot in the internet but with no success. Any help will be appreciated. Thanks!

A background task in Android means that you want that task to execute when the application is in the foreground/background state. It also means that the task will be executed if the device has been rebooted, or the application has been closed/minimized. Work Manager class is perfect for such a purpose.

Related

RestorableBool vs bool in Flutter

According to documentation, RestorableBool class is a RestorableProperty that knows how to store and restore a bool. One example of RestorableBool is given by flutter itself in the flutter gallery project.
The same result can be achieved by using simple boolean. Click here to get the code of same screen using simple bool.
Is there any special benefits for RestorableBool over usual bool or is there any special context at which we have to use RestorableBool?
Anyone to help?Thanks in advance.
Mobile operating systems fool you into thinking all the apps you have open are actually running. But they are not. The topmost is running, all others are killed. Their last screenshot is kept to uphold the illusion of being able to hold multiple apps open.
When you "switch apps", what happens is that the app that wasn't in the foreground is opened and it's state is restored from memory.
If you don't do anything, your app cannot handle this and will just start as if it was started from scratch.
In Flutter, the Restoration Manager can help you store your state and restore your state. Other options are available, I think hydrated_bloc is probably useful if you are working with bloc patterns.
The example in your code doesn't do the same thing, well it does in your code, but it wouldn't in all cases.
The difference is that RestorableBool is reset to it's initial state, even if it is already in that state, meanwhile your code flips the bool to the opposite value.

Stop Firebase downloading task returned by GetFileAsync in Unity3D

I met a case when I need to stop already started downloading task using GetFileAsync method. Unfortunately I didn't find any info about it and how to implement it in Unity3D (c#).
I know there is a way to stop thread, but don't see the way to find thread by that task.
The idea is to stop downloading in some cases, for example if user change the scene and downloading content is not actual anymore.
Please, advice, is there a solution to implement it?
Given that the download methods all return a C# Task, which doesn't contain a pause or similar method, I don't think there's a way to do this at present.

How to use JavaFX Preloader with stand-alone application in Eclipse?

My IDE is Eclipse and my project is a stand-alone JavaFX application (pure CS architecture with OSGI framework).
How to use preloader thus the preloader would be started before my main application and hid later?
I found some code here:
http://docs.oracle.com/javafx/2/deployment/preloaders.htm#BABGGDJG
But I still don't know how to deploy the preloader with my startup application in an OSGI framework.
I give some code of my startup application below:
public class MyPrjMain extends Application {
private static Stage primaryStage;
public void start(final Stage stage) throws BusinessException {
primaryStage = stage;
init(primaryStage);
primaryStage.show();
}
}
This is a long answer, the quick answer for the impatient is to download this sample code for displaying a splash page for an intensive startup task and see if it is adaptable to your situation.
My answer provides general information about Preloader style functionality in JavaFX. Your question specifically mentions Preloader usage in an Eclipse and OSGI environment, but I won't directly address that scenario as I don't use those technologies. Hopefully the general information is still applicable to your scenario.
1. Java has native support for displaying a splash page when Java is started.
This works using the -splash:<image> VM switch.
Advantages and disadvantages:
+ The simplest way to get your standalone application to show a splash image.
+ Can be displayed very quickly
=> it's an argument input to the VM process, so (presumably) it can be displayed even before the VM itself has fully initialized.
- Has limited features
=> only allows display of an image, not other preloader features such as reporting of initialization progress, animation, login prompts etc (unless you make use of AWT APIs)
- Won't work on all platforms until Java 8 (see issue Mac: Impossible to use -splash: with JavaFX 2.2 and JDK 7).
2. Preloaders may be used for standalone applications.
The JavaFX Preloader tutorial has an example in the section 9.3.4 Using a Preloader to Display the Application Initialization Progress. The tutorial provides executable sample code in the LongInitAppPreloader and LongInitApp classes (use the class names I provide in this answer as one name in the tutorial is currently wrong).
The sample standalone application has a long initialization time and a custom Preloader provides feedback on the progress of the initialization. The sample simulates the long initialization through a Task with a Thread.sleep call, but a real application would be doing something like establishing network connections, retrieving and parsing network data and setting up the initial application Scene.
Preloaders are not specific to applets and WebStart, but are primarily targeted to those deployment types. The applet and WebStart initialization process is more complex than standalone application initialization, so much of the Preloader documentation is devoted to those more complex scenarios.
3. You don't need to place a Preloader in a separate JAR.
You can place the Preloader in the same JAR as your Application class. For large applications dynamically deployed and updated over network loading protocols such as WebStart, placing the Preloader in a seperate JAR makes sense. For standalone applications performing network based initialization, it probably doesn't make much difference and the separate packaging step could be skipped to simplify the build and deployment process.
4. You can achieve Preloader style functionality without using a Preloader.
Much (not all) of the Preloader functionality can be achieved without subclassing Preloader.
You can:
Create a startup Stage in your application's start method.
Place a splash image and ProgressBar in the startup stage.
Have a background task for lengthy application initialization processes.
Report initialization progress back to your startup stage from your background task.
On initialization completion, either:
a. Replace the startup stage with a newly created application stage OR
b. Replace the contents of the scene in the startup stage with a new scene for your application.
5b is probably preferred so that you don't need to create multiple windows.
For examples of this strategy, see my answers to the following questions:
Designing a splash screen (java)
How to create Splash screen with transparent background in JavaFX
The related sample code for displaying Progress Monitoring splash screens in JavaFX without using a Preloader is:
Code to displays a JavaFX splash page for an intensive startup task with progress monitoring
JavaFX standalone application Splash Page for a great website
The above code could be refactored to use a Preloader subclass instead, in which case there is a well defined framework for notification of application initialization events and more flexible deployment models (e.g. preloader in seperate jar) are available. However use of a Preloader may be a little complicated. For some implementations, it may not be worth the time to understand the Preloader framework.
5. WebStart Apps have JNLP support for Splash Images
(this point is pretty irrelevant and just included for completeness).
I believe that webstart applications can have a flag in their jnlp file to show the startup image as the webstart application launches, but I've never been able to get that flag to work in a JavaFX 2 application, only in a Swing application and even then it wasn't all that reliable as it would only display the second time the application was launched.
IMHO a Preloader only makes sense when you are running as an applet or webstart because the preloader can be packaged as an extra Jar which is downloaded first and executed while the rest of your application is downloaded in the background.
So my suggestion would be to open a stage at the first point in time when you get a Stage and e.g. display a splash.

Auto Redial using iphone app

Two questions actually.
First : I know iPhone is missing auto-redialing functionality but is there any other way to achieve it by iPhone application, as I can call by my application but facing problem cannot auto redial.
Second : Before calling I want to implement functionality of loud-speaker on a button action.
Is there any way to achieve the above 2 functionalities?
I spent 4-5 hours on googgling about it and the result is only this
I go through to apples doc and found some code hereand also tried this but cant get the right way to implement above functions ...
Any help would be greatly appreciated!
Thanks!!!
Neither of those actions are possible with the SDK.
For the Second case it's definitely NO.
But for the First case, I have no sure about this, but can be tried:
Subscribe with the CTCallCenter for call states notifications and use some background application type (voip, location or audio) or use some waiting block, that will allow to prevent going to suspended state as long as possible
Open URL using tel://
If the call notification about call failure comes in, try to repeat opening an URL
Once again, just an idea, may not work at all :/

how can I improve iPhone UI Automation?

I was googling a lot in order to find a solution for my problems with UI Automation. I found a post that nice summarizes the issues:
There's no way to run tests from the command line.(...)
There's no way to set up or reset state. (...)
Part of the previous problem is that UI Automation has no concept of discrete tests. (...)
There's no way to programmatically retrieve the results of the test run. (...)
source: https://content.pivotal.io/blog/iphone-ui-automation-tests-a-decent-start
Problem no. 3 can be solved with jasmine (https://github.com/pivotal/jasmine-iphone)
How about other problems? Have there been any improvements introduced since that post (July 20, 2010)?
And one more problem: is it true that the only existing method for selecting a particular UI element is adding an accessibility label in the application source code?
While UI Automation has improved since that post was made, the improvements that I've seen have all been related to reliability rather than new functionality.
He brings up good points about some of the issues with using UI Automation for more serious testing. If you read the comments later on, there's a significant amount of discussion about ways to address these issues.
The topic of running tests from the command line is discussed in this question, where a potential solution is hinted at in the Apple Developer Forums. I've not tried this myself.
You can export the results of a test after it is run, which you could parse offline.
Finally, in regards to your last question, you can address UI elements without assigning them an accessibility label. Many common UIKit controls are accessible by default, so you can already target them by name. Otherwise, you can pick out views from their location in the display hierarchy, like in the following example:
var tableView = mainWindow.tableViews()[0];
As always, if there's something missing from the UI Automation tool that is important to you, file an enhancement request so that it might find its way into the next version of the SDK.
Have you tried IMAT? https://code.intuit.com/sf/sfmain/do/viewProject/projects.ginsu . It uses the native javascript sdk that Apple provides and can be triggered via command line or Instruments.
In response to each of your questions:
There's no way to run tests from the command line.(...)
Apple now provides this. With IMAT, you can kick off tests via command line or via Instruments. Before Apple provided the command line interface, we were using AppleScript to bring up Instruments and then kick off the tests - nasty.
There's no way to set up or reset state. (...)
Check out this state diagram: https://code.intuit.com/sf/wiki/do/viewPage/projects.ginsu/wiki/RecoveringFromTestFailures
Part of the previous problem is that UI Automation has no concept of discrete tests. (...)
Agreed. Both IMAT and tuneup.js (https://github.com/alexvollmer/tuneup_js#readme) allow for this.
There's no way to programmatically retrieve the results of the test run. (...)
Reading the trailing plist file is not trivial. IMAT provides a jUnit like report after a test run by reading the plist file and this is picked up by my CI Tool (Teamcity, Jenkins, CruiseControl)
Check out http://lemonjar.com/blog/?p=69
It talks about how to run UIA from the command line
Try to check the element hierarchy, the table can be placed over a UIScrollView.
var tableV = mainWindowTarget.scrollViews()[0].tableViews()[0].scrollToElementWithName("Name of element inside the cell");
the above script will work even the element is in 12th cell(but the name should be exactly the same as mentioned inside the cell)