Please Wait iPhone - No NSTimer - iphone

I am currently using NSTimer to load a "Please Wait" alert view in my application while it contacts a server. I want to get rid of this NSTimer and change it so that it runs the "Please Wait" alert view in a loop until it gets a response from the server.
How would I do this?

A loop would be bad (blocking the main thread), but if you use NSURLConnection object you get a connectionDidFinishLoading: message in the delegate.
You pop up the alert when you start the connection.
You dismiss the alert in the connectionDidFinishLoading: of your delegate.
And also when an error occurs.
Though I agree with #Radu when it's about user experience.

Set up a delegate for the NSURLConnection. Your delegate will then receive messages at interesting points in the connection, such as when data is received and when the connection is finished.

try this page. It helps manage cases where there might be more than one network event going on.

Related

When to show Alert for Startup and coming out of background?

In my project I show an Alert to the user to indicate an 'empty list'.
Right now, I show it in AppDelegate>applicationDidBecomeActive.
I'm doing this because I want the alert to show if the list is empty
at app startup and when coming out of the background (iOS 4.2 through 5.x).
EDIT:
I use a method in the AppDelegate, and call it with a slight delay, and I still get this notice.
[self performSelector:#selector(checkForNoMessages) withObject:nil afterDelay:1.0];
However, this causes a "wait_fences" notice in the debugger and I'd prefer not to submit to Apple with this notice.
Where is the proper place to put a popup Alert so that it appears:
1) At App startup
AND
2) When the App is coming out of the background?
Do I need the Alert in more than one place?
I recommend writing a method in your AppDelegate or better in your root view controller which shows the message. Maybe with some arguments, so you can reuse it but that's up to you.
If you are following the MVC architecture ask your model about existing entries and trigger the Alert message if necessary. But encapsulate this behavior in a controller as well.
application:didFinishLaunchingWithOptions: and applicationDidBecomeActive: are the places where you want to delegate this task to your controller.
More about iOS Multitasking: https://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html
Edit:
Don't forget that you have to call the methods from the main thread.
And do all startup stuff first.
OK - the problem wasn't where I called the alert, it was because it was in a method. Once I moved the code from a method into applicationDidBecomeActive, all is well.

IBAction - do something before connect to server

I have an IBAction, triggered by a UIButton.
In this IBAction I connect my app to server, returning a Plist.
During this time (between my apps connect to server, and the server returns that Plist) I want to show a UIActivityIndicator.
The problem is, when my IBAction is triggered, the apps connect first to server, an then shows the UIActivityIndicator.
My IBAction in pseudocode
- (IBAction) loginMe: (id) sender
{
// show activity indicator
// connect to server, and catch data
}
Your "connect to server" action is likely synchronous. The action "show activity monitor" is incorrectly labeled. What you're really doing is "mark activity monitor for display next time drawing occurs."
What you need to do is to make sure that loginMe: returns so that the UI can update. The server connection logic needs to continue asynchronously. Generally this is done with NSURLConnection. You can find explanation in the URL Loading System Programming Guide.
Also check out MBProgressHUD here:
https://github.com/jdg/MBProgressHUD
It wraps the indicator and allows for additional customization based on how you connect synchronously or asynchronously.

Three20: cancel network request

I am using three20 and implement the model like the example of TTRemoteExamples. Now problem is: when I click and open a page, the TTURLRequest sent out, during fetch data from remote, I click to open another page. But the previous network request is still there messed my loaded data. So I want to know the way to cancel previous network request when I switch to another page. Or when I click button to do a new request in the same page.
Thanks~
To cancel a TTURLRequest, keep a reference to it (typically in an instance variable) then send it a "cancel" message. Like so:
[self.myRequest cancel];
If you don't want the delegate to be notified of the request being cancelled, do:
// I'm assuming self is the delegate here, that may not be true
[[self.myRequest delegates] removeObject:self];
[self.myRequest cancel];
You'll typically also want to do this in your view controller dealloc method. If a request continues after the viewController has been deallocated, it will try to send delegate messages to it, and you'll get a bad access crash.
As for the timing of when you cancel it, that's up to you. If you need it to stop when a user leaves your view controller, then implement UIViewController's viewWillDisappear: or viewDidDisappear: methods (don't forget to call super!).

UIAlertView inside an NSOperation is not modal in iPhone

So I am trying to create a check that tries to connect to the WWW. when it fails it needs to then retry several times before the application gives up and quits. Each time it retries the user is propted with an UIAlertView with the options to Retry or Cancel.
So here is the problem.
I have a chain of actions in an NSOperationQueue, all the operations should fail with no connection. I"m using the NSoperation Queue so that the UI dosn't lock and the data is being processed in the background.
inside an NSInvocationOperation my method will hit [AlertView show], however this is not truly modal.
My operation then returns and continues through the chain of NSOperations, as there seems to be no way to return them with an Error value to stop additional processing. Eventually the UI catches up, displays the Modal AlertView, but i have no context of what has happened.
I am sure this is a common requirement. any ideas how to achieve this?
If I understand you correctly, you want a modal version of UIAlertView, but only modal within the calling thread/NSOperation? A few problems with this:
You should probably only call interface operations from the main thread (easily addressed using performSelectorOnMainThread:)
Modal dialogs are not really part of the OS; you'll need to address this programatically.

Updating UIAlertView title

I am uploading a bunch of files from the iPhone to a web service. I want to have a UIAlertView on screen with a UIActivityIndicatorView inside. I got that to work fine. However, I want to update the title of the UIAlertView as each file gets uploaded. ("Uploading file 1...", "Uploading file 2...", etc.)
I know that you can't just set the title in a loop with synchronous web requests as the UI run loop will never get called. I tried using NSTimer to fire off the web requests but since each request duration is unpredictable, that doesn't work. (The message could update before the request is actually finished.)
I really want to upload each file synchronously, one at a time, as the iPhone bandwidth is pretty limited. I just can't figure out a mechanism to say 'once this synchronous operation finishes, let the UI update for a tick, then do another synchronous operation.'
OK here's the approach I took to solve this:
Create the UIAlertView
Start an asynchronous web request to post the item, set the delegate to self
In the connectionDidFinishLoading: method, check to see if there are more items to post. If there are, update the title and kick off another async request. If not, dismiss the alert.
It means you have to maintain a few class variables to track which request is uploading but it works perfectly and doesn't involve any complicated threading or anything else.
did you try this?
alertView.title = #"new title";
[alertView setNeedsDisplay];