UIAlertView inside an NSOperation is not modal in iPhone - 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.

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.

can any one tell me the name of alert view that should do the works of **C# show dialog**

can any one tell me the name of alert view that should do the works of C# show dialog .
other actions will not work without dismissing it(EVEN DELEGATES ALSO).
Modal dialogs in Windows (in any language) and in iOS are fundamentally different.
On Windows, a modal dialog (and in particular the ShowDialog method) behaves like a function call that will only return when the dialog is closed. Your app will naturally wait until the your has made his decision.
On iOS, presentModalViewController (or [UIAlertView show]) almost immediately return. You can registered a delegate that will be notified when the dialog is closed. But if your app is supposed to wait only the user has chosen something in the dialog, then you have to implement the waiting yourself.
As far as know, You have to do this manually. You can lock your task when the alert is shown and then unlock it again when it is dismissed getting help from these posts.
is there a way to pause an NSTHread indefinitely and have it resumed from another thread?
How to pause an NSThread until notified?

UIActivityIndicatorView not displayed despite of threading

I've been trying to implement this for a long time and I have gotten no favorable results.
Say I have a method in the which an HTTP request is performed (specifically, a twitter update), and say I want to display a UIActivityIndicatorView while the HTTP request is in progress (I know when it is done because there are delegate methods that are called when the request is done, either with positive results or negative ones).
I've seen many answers that say that threading is necessary for the implementation of this class. At first I tried calling the startAnimating method in a different thread and the stopAnimating method directly (without starting a new thread). After that I saw how this guy does it and I thought this was safer as I was starting and stopping the indicator in two different methods (the delegate methods for the twitter update).
However, none of this two ways of doing this have given me the results I want (the activity indicator does not show up at all). Is there anything I'm missing?
Thank you in advance and I apologize if my question is too long.
Your help is very much appreciated.
Threading is absolutely forbidden when working with UIKit subclasses. You may have seen reports that UIActivityIndicatorView uses threading internally, but in no way does that mean you can access the object from multiple threads. All UIView subclasses (including UIActivityIndicatorView) must only be accessed from the main thread. This includes calling -startAnimating and -stopAnimating.
If you rewrite your code such that you're only ever accessing the activity view on the main thread, and it still isn't working, then I would guess that the view was either not added to a visible view, is covered up by another view, or has a frame that puts itself outside of the visible area of its superview.
You cannot perform UI stuff in a secondary thread.
You should perform your HTTP request in a secondary thread, while calling the activity view from the main thread.
I recommend using DSActivityView which is so much easier to use. Just 1 line to show an activity view, 1 line to hide.
MBProgressHUD it's also easy and shows and hides itself when the secondary thread has started/finished. Something like this:
[HUD showhileexecuting:"yourstuff" animated:YES]
"Yourstuff" will run on a separate thread.
You should not perform UI activities in a secondary thread.

subview is not added immediately (iphone)

When the return button on the keyboard for a textfield is tapped I want to add a UIView, then connect to a website with NSURlConnection sendsynchronousrequest and I have the code in that order
But when I run in the simulator (I can't run on device) the connection is run first then the subview is added (ie the opposite of the order of the code)
Why is this and how can stop it, because I want the view to added, then the connection done and then the view removed.
The subview is being added, but views are drawn by the runloop. By making a synchronous request on the main thread, you are blocking the runloop, so the view won't be drawn until after the request completes. Do the request asynchronously, either by using the async API or by doing a synchronous request in a background thread.
Many actions happen on the run loop, rather than in the order you code. If you really want to code the way you have, then performSelector:withObject:afterDelay: with a delay of 0 might work to trigger your NSURlConnection (you will need to move that code to a method).
As JK suggests, an asynchronous request might solve it anyway, and improve the UI. I'm a great fan of ASIHPPTRequest library, which makes async trivial.

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];