Synch call on willFinishLaunchingWithOptions (AppDelegate) - ios

I would to do a remote call before the app is started (in AppDelegate, when the splash screen is showed). Then i would to choose which view controller to load based on url response.
Is right to do this on AppDelegate? Or I need a different approach?

I think the best approach is creating a ViewController where you make this choice. Once this VC is loaded you make your remote call while showing in the UI that your app is actually working and waiting for a network response - the best approach is probably showing a message with a UIActivityIndicatorView spinning.
Once you get the response you load the VC that you need. You should also handle errors - what are you going to show if the network request fails? Are you showing an error message?

You should not do any synchronous network calls from willFinishLaunchingWithOptions. If you take more than a few seconds to return that method or (didFinishLaunchingWithOptions, or the other app delegate methods that the system calls in the process of launching your app) then the springboard will terminate your app as unresponsive.
#Tanzolone has the right idea. Have your app display a view controller that shows your app's UI, THEN invoke the network request and decide what second screen to switch to based on the response.

Related

Having trouble with AppDelegate didFinishLoadingWithOptions execution order/timing

I'm having an issue with this code (I have it in didFinishLaunchingWithOptions). I need it to finish executing the code inside before the app continues because it sets up some critical things in core data. It sends this to the background though and starts running the rest of the app and it doesn't finish quick enough for the data to be usable.
DataManager.getDataWithSuccess { (data) -> Void in
//code to execute here
}
How can I force the app to wait for this code to finish before moving on?
You shouldn't block the didFinishLaunchingWithOptions method from returning so that it can wait on an asynchronous task. It's crucially important to note that iOS applications are only given a limited amount of time to complete launching before the application is killed by the operating system.
An approach I have used in the past when waiting on asynchronous things to happen that need to happen before I really launch my app is to create a LaunchViewController. The interface for this view controller matches perfectly to the app's splash screen. From an end-user perspective, you can't even tell we've left the splash screen.
Here, we do any set up code such as asking your DataManager to get data. Then, when it (and any other set up actions) completes, you simply present the next view controller in much the same way you'd move between any other view controllers.
A huge positive side effect here is that you can have much nice looking animations from your splash screen into the first screen of your application.

How to remove all callbacks of a popped screen in Blackberry java

I have a blackberry application in which i want to show a "please wait" modal screen(which is a FullScreen push as modal screen) while sending a server request and if the user hitting the device back button , pop the modal screen and current active screen.This works fine.
My problem is that : I used a callback for server request in the active screen.But the callback executed even after popped up the screen.
Exactly whats happening while calling popScreen()? How can i remove all callbacks and refresh the screen if the user pressing back button while server request happens?
Thanks in Advance
There are several solutions to this problem of course. I guess the server request is sent asynchronously.
The simplest way I think out is having a flag and when the callback
is triggered check whether the user cancelled the action (pressing
the back button).
Another solution, perhaps not that nice is to check whether the
Loading Screen is still in the display stack.
What I think would be a proper solution is to have a stack of
cancelled http operations, so you can stop requests at any time. Then
if the request to the server has been sent, before calling the
callbacks you can check if the operation has been cancelled.
Otherwise, you just avoid sending the request to the server.
When you call popScreen, the screen which is on top of the display stack (the screen in the foreground) is removed from the stack and the Screen is refreshed (a paint event is triggered) to reflect the changes. Make sure you execute Screen.pop() on the UiThread:
UiApplication.getUiApplication().invokeLater(runnable)
In your scenario, how is the callback handled? Is it a delegate you passed along with the request or is a listener you register?
When I refer to delegate I mean something like the following:
Server.sendRequest(request, objectWithCallbacks)
and callbacks of the objectWithCallbacks (and only objectWithCallbacks) will be called accordingly. On the other hand, a listener would be something like:
Server.addListener(objectWithCallbacks, request)
Server.sendRequest(request);
this way, all the objects listening to "eventName" will get their callbacks triggered accordingly.
As far as I see, your callback will be always executed but in the callback itself you can check if the screen is currently displayed.
if( this.isDisplayed() ) {
// Do the magic
}else{
// Do nothing
}
Good Luck

Asynchronous (non-blocking) display of alert in iPhone App

In my iPad App, am connecting to a web service. Whilst connecting to it, am displaying the progress activity indicator and a corresponding message to the user in a label (the label is in a subview and am adding the subview to the current view).
After this line of code (which calls a method to add the subview to the view), am invoking the method to call the web service. However, the web service call is getting executed first, and then only the user-information subview is displayed.
Is there any way to say that I want to 'continue displaying' the alert view even while the execution continues to the next line of code?
// Calling method to add info/alert subview to current view [self displayUserMessage];
// Connect to Web Service [self connectToWebService];
I'm not sure if I totally understand your question. Also it's far more easy to understand if you provide some code after your explanation... Anyway what I understand is that you are connecting to a web service and showing some info while the connection is on going?
Remember that if you don't want to hang your user interface you need to send the webService Connection in another thread, so you can keep the main thread free. You can do so using GCD.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
[self connectWithWebService];
});
Then depending on the architecture of the web service, you can use a delegate o maybe a completion block to show some messages (info/alert) to the user. In that case remember that anything related to UI should run on the main thread. So as I said before depending on your architecture you should do something like this
dispatch_async(dispatch_get_main_queue(), ^{
// Show UI Changes
});
The UI should update properly while the webService method is running on background.
If you want asynchronous connections its easier to go with NSURLConnection's sendAsynchronousRequest:queue:completionHandler:..
you can display your alert before calling it and dismiss it in the completion handler.

Background request with delegate in iOS

In my app i am uploading image(to AWS S3 server) in background with delegates. I do not want to wait till it reaches delegates callback methods. So after 1 second I go back to previous screen with
[popToViewController:myPreviosuScreenController].
Problem is when request is complete it doesn't find the delegates. And throws error.
How to handle this scenario?
Make the request's delegate an object that doesn't get deallocated when you pop the view controller.

iPhone UILocalNotification load specific view

I have my local notification working, but when I click View to come back to the app, in the app delegate I am trying to load a specific view, but it wont take. Is this possible or not? It always just comes back to the last view viewed.
When you tap "View" on the notification, it takes you back to your application, and your application shows whatever it was showing before (if it was backgrounded) or is launched.
If you need to show a particular UI in response to the notification, consider implementing the <UIApplicationDelegate> method -[<UIApplicationDelegate> application:didReceiveLocalNotification:]. In that method you can inspect the notification and transition to the appropriate interface.

Resources