Terminating asynchronous NSURLConnection loading prematurely - ios

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

Related

Does firebase functions keep running after dissiming a view-controller

I am a newbie in iOS development/firebase.
I have two view controller,
the first one does nothing expect performing a segue to the second view-controller
The second the view controller I have two buttons,the firstone is (back) which dismiss a view controller and the second button(up) have the following code
Database.database().child(“posts”).setValue(1)
1- If I have an extremely bad internet connection, and I pressed on up button then immediately pressed on back,
Does the code for uploading data for firebase stop on continue?
2- After pressing up, I immediately go to background and dismiss the app, will it continue setting the data?
All database operations are handled on another thread that's not related to any UI elements in your app. As long as your app is running, database operations will continue until complete.
If the app is no longer running, database operations will stop. If database persistence is not enabled, all of that pending work will never complete. If database persistence is enabled, then the SDK will try again to synchronize the writes that didn't complete previously.

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.

iOS - How to use synchronous requests with loading indicator

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.

NSOperationQueue blocking tableview refresh

I have been using NSOperationQueue to download some data from server on background.
i have multiple data to be downloaded .So multiple operation is added to queue for each data download
there UITableview which i need to refresh once single data is received.
I have used KVO approache to track "isFinished" key for each operation finish.
But when i refresh tableview all delgates for tableview is called but table isnt refreshed.
i also see from my console logs that even when tableview isnt refreshed completely,i see logs from worker thread (is used for background download of data).
So thats why my table isnt refresh completely?
What could be work around for it?
Would be nice to see some code, but one of the possible reasons could be that you are running reloadData from a background queue.
Try this:
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});

Resources