How to Execute a Controller Method Every Time It Loads? - sapui5

I'm struggling to find a way to execute a function in the main controller when it loads. When the main controller is loaded the first time, I can get that function executed inside onInit. But the issue is when user logs out and logs back in the main controller, the method onInit does not get executed again. Is there a way to execute a function every time when controller loads?

Below code will help you to achieve what your are looking for
onInit: function() {
this.getRouter().getRoute("routeName").attachPatternMatched(this._onObjectMatched, this);
}
_onObjectMatched: function() {
//this function executes every time you navigate to this page
}
Demokit link for detailed information

What do you mean by "when user logout and log back in the Main Controller"?? When your app loads the view the first time, it executes the onInit, onBeforeRendering, onAfterRendering and other lifecycle events. But if you don't destroy the controller instance you never 'log out' of it. It remains there as an object in your DOM and their functions can be called whenever is needed.
Now, if you are using the UI5 Router to navigate back and forth to other views, then I suggest you to set 'PatternMatched' events in your router. This events will be fired whenever the given pattern is match, no matter if it is the 1st or the n-th time.
Check out:
read optional url parameters
Step 32: Routing with Parameters

Related

ionic Loading Controller Cancel and Return

This seems like a simple thing but I wasn't able to implement it and didn't find information on it.
I have an ionic Loading Controller that initiates when processing some data. This might take time so the user might decide to cancel. So I would like to simply add a cancel button in the loading element which dismisses the loading controller and returns to the previous page.
this.loading = this.loadingCtrl.create({
content: 'Loading ... (cancel button here?)'
});
this.loading.present();
As far s I know it is not supported. I did it with a modal, it works nearly the same and has the same syntax, but you don't have the transparent background.
See: https://www.techiediaries.com/ionic-modals/ for implementation.

Reload application in SAPUI5

Is there any way by which we can reload the whole SAPUI5 application?
When a SAPUI5 app is called, onInit() function is called and we are initializing some settings for view here. Say if some change (like selection of a checkbox in master view) requires the whole application to be reset, can we reload the application and call onInit() function again to correct our initial settings? Is there any trigger mechanism by which we can call onInit() function explicitly?
Thank you in advance for your suggestions.
Regards,
Raja
If you have the need to reload or reset your application, then I would think your app suffers from a major design flaw... This is definitely something I would look into first if I were you!
However, to answer your question, why not move the initialization code into its own function, and call that function from the onInit() event handler and whenever you need to reset your app?
I suggest you to use some kind of intercommunication (e.g.: EventBus) between your Controller instances, from the master view notify your Controllers to rerun initialization logic like follows:
move you initialization logic to separate functions on each Controller and call this method upon onInit
in onInit subscribe to reinitialize event
in you master view add a listener function to the mentioned checkbox selection event, and publish the reinitialize event to notify your Controllers to reinitialize
Controller.js:
function onInit() {
this._initialize();
EventBus.subscribe('reinitialize', this._initialize);
}
funcion _initialize() {
// init logic
}
MasterController.js:
function onSelectionChange() {
EventBus.publish('reinitialize');
}
Sometimes you cannot directly use the window global variable in sapui5.
Refer to this page:
https://help.sap.com/viewer/825270ffffe74d9f988a0f0066ad59f0/CF/en-US/7d0dba71a2cb45ebb1959daba904ca99.html?q=location%20reload
var myLocation = location;
myLocation.reload();

Pressing button holding till the action perform in ios

I have called a web service method to insert some data and uploading some images to web url.
It is working fine but the button remain pressed till the method perform.I have used threading concept here and created one another thread and perform that action but that thread is not taking me back to the main thread.
Please till me what would be the appropriate way of doing this.
The web service calling mechanism place in one seperate method and call that method from the IBAction of the button using the performSelector:withObject:andDelay () method. This will solve your problem.
Create one another function in which you can put the code for the webservice call so that your button press issue will be solved.
Also you can implement this using GCD.
- (IBAction)buttonPress:(id)sender
{
dispatch_async(dispatch_get_global_queue(0,0), ^{
//place your web service code here
});
}

viewWillAppear / viewDidAppear firing multiple times

I am using TabBarKit, and I want to execute a request to pull a new peice of content from a webservice each time a user goes back to a tab.
I can't put the request code in viewDidLoad as its not fired when coming back to the tab. With that said, I've noticed viewWillAppear / viewDidAppear are called multiple times when returning back to a tabs view controller.
If I put the requesting code in there, it is fired multiple times resulting in the webservice being pinged needlessly.
How can I solve this problem? Which method should I place my HTTP request call in so it executes once per view?
You could try setting/checking a downloadInProgress flag before submitting the asynchronous download, then resetting that flag when the request completes.
If you're using something like the ASIHTTPRequest, that calls a delegate method when the request completes or fails, which is the point where you could reset the flag. It allows you to tag each request individually so you can track the success or failure of each one, so this wouldn't restrict you from running one background request at a time.
Turn's out there was an extra call to viewWillAppear in the controller code. If you're interested in following the changes, there is a thread on the issues section of the Git project.

Block UITabBarController while contents of a view controller not been charged

I'm doing an app that uses a TabBarController and each Tab uses its own navigation controller.
The app has dynamic content and I use viewDidDisappear viewDidAppear methods to create or destroy the objects that I need each time I enter or exit into the ViewController.
My problem is when I start to sail very fast and I don't give time to load the Threads that I use for uploading content such as XML peta app or destroy objects when I leave the ViewController.
How I could control the tabs of the navigationbar or tabbarviewcontroller for not respond until the viewcontroller has loaded all contents?
Excuse me if I'm not well expressed. Thanks!
No matter you use synchronous request or asynchronous request, just show an UIAlertView while loading the data. This will both serve as a notification to the user that something is being loaded, and the it will block the interactions with all the other views on the screen.
As others have suggested in comments, I believe that what you want to do is rearrange the order in which things are triggered. Perhaps something like this:
On viewWillAppear:, clear (or disable or whatever is appropriate) your objects that are no longer valid and begin the load-new-content thread. Perhaps display a UIActivityIndicator or similar.
On viewWillDisappear:, tell the load-new-content thread that it can stop, its results are no longer needed. If you put up an activity indicator, take it down.
At the end of the load-new-content thread, take down any activity indicator, update the UI with the new contents and activate.
I don't really see any way around this -- if the UI is not valid until the new content is loaded, then you have to wait for it.
Another solution might be to cache the contents from the previous fetch, and always display those on viewDidLoad. Then, at the end of your new-content-thread, cache the new contents, and update the UI.