iOS - How to use synchronous requests with loading indicator - ios

So I am trying to display an animated loading view (custom built) that will continue to animate while making a synchronous request to a web service.
I am familiar with synchronous and asynchrous requests using NSURLConnections and delegates, but my problem is that I want to ensure that my thread WAITS for the request to finish BUT still animates my loading indicator.
Are there any suggestions as to what the best way to go about this is?

Doing this with a synchronous request is not the way to go -- if you want your app to "wait" for the request to finish, you just put whatever code you want to resume after the request finishes in the connectionDidFinishLoading: method. That way, your UI will not be stalled and you can animate your loading indicator.

Related

Synch call on willFinishLaunchingWithOptions (AppDelegate)

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.

How To Update UIProgressView after UITextField DidEndOnExit

I have a UITextField, which checks a password and then my app loads data from a remote server. While this is happening I would like a progress view to display the progress of the download. My issue is that the UITextField seems to lock up the display until it returns and therefore I cannot update the progress view on the main thread. I also would like an answer without GCD or any other kind of threading as I am using core data and that would probably overcomplicate the app. Is there a way to have the UITextField not lock up the view so that I can update my progressView on the main thread?
If your app is loading data from a remote server, then you will have to use multi-threading(GCD, etc). Otherwise it just locks up the main thread till the download is finished which makes your app unresponsive.
To keep it simple, use GCD to fetch data(raw NSData) and give it to the main thread. Do all your processing on the main thread(core data, etc) as usual.
EDIT: One more thing, it is not the textfield locking up your UI, it is the download. So I don't think you can do anything other than multi-threading to help you here.

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.

Terminating asynchronous NSURLConnection loading prematurely

I'm making an app that loads data asynchronously for a data picker, then reloads the data picker, and everything's fine.
The problem is that if the user taps on the text field, the app makes the network request for the info, but if it hasn't loaded and then the user taps the back button, then the view disappears, but the data is still loading in the background.
I've tried running this loading method on a custom thread and then calling [myThread cancel]; on viewWillDisappear, but the data still loads. I want to avoid the user using data when he cleary doesn't want to load it anymore. Any help?
I was thinking of killing com.apple.nsurlconnectionloader but I have no idea how to do that.
Few things to note here, canceling an NSThread will not terminate it immediately, it will only mark it for termination.
What I would do instead is implement your NSURLConnection delegates within an NSOperation, and when you want to cancel your download you can call cancel on the operation, which will set its isCanceled property to YES. You can then use this as a flag to terminate the operation's runloop, and the download will not continue.
Apple has an excellent technote on using NSOperation here https://developer.apple.com/library/ios/#technotes/tn2109/_index.html

UIActivityview indicator help?

I would like the UIActivityindicator to spin as long as a method isn't finshed loading like one of my methods parses data from a website so I would uiactivity indicator to spin as long as the method isn't finished doing its job.
thanks,
Tushar Chutani
the obvious answer is [activityView startAnimating]
however i suspect you are running a synchronous retrieval of the data and you are not getting back control of the UI until you have finished your method.
if that is so you will need to run your method in the background and then do the UI updates on the main thread.
post some code .....

Resources