Display loading view after login in ios 3 seconds - ios

I am trying to display a new view in IOS after login in my application. I also want to display this view when I start the application if the user is already logged in this app.
Now, if I start the application the first time I have the login view and I can write my user name and my password. when I press send button(login) the next controller starts. I would like to set for 2 or 3 seconds one view while I am downloading the information from the web service. I don't care if the information comes faster than the two seconds because I want to wait this time. I save the user information in NSUserdefaults.
Next time I start the app I already have the information and I go directly to the next viewcontroller without see any login view. So I want to watch the loading view.
I use AFNetworking to consume my web services.

You have two options, it seems a little arbitrary to show it for 2 seconds, but you can present the view and then:
[myView performSelector:#selector(setHidden:) withObject:[NSNumber numberWithBool:YES] afterDelay:3.0];
Or what you probably should do is hide the view or set it to nil within the completion block of your AFNetworking Call, that way if the call takes longer than 2-3 seconds, the view won't disappear

You can call the method for hiding view after delay using this:
[self performSelector:#selector(hideView) withObject:nil afterDelay:3.0];

Here is an idea, using GCD:
Store the login information into keychain (NSUserDefaults are just plain unsafe for passwords!) and dismiss the login window immediately after it is saved.
When the loding view is shown, create a dispatch group and submit your login and download logic there. (com.example.login thread)
Use NSTimer (or any appropriate logic) to wait for 3 seconds, then spawn another thread in GCD (com.example.login-wait thread), call dispatch_group_wait to block that thread until the com.example.login thread is done, and submit a block back to the main queue to switch views.
I used this login in multiple applications and it looked fantastic.

Related

How to manage background tasks iOS

Lets say a user starts async request in PageViewController flow then dismisses. If they quickly bring back PageViewController it won't navigate to the correct page because an async request (that determines the next logical page to show) is underway. Is it standard to prevent the user from dismissing controller or asking them not to? If not, how do I manage background tasks, so I know to keep loading screen up and wait to navigate pages until last request completes and changes state/server side data?

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.

Load entire application at start up

I have a tabbed iOS application with each of the tabs having some sort of json request so the view loads ever so slightly slowly. I want to remove that lag completely so I'm wondering if there is a way to call the ViewDidLoad function from each of the classes during the login phase of the application.
if (login == "Success") {
UserDefaults.standard.set(true, forKey: "isUserLoggedIn");
UserDefaults.standard.synchronize();
DispatchQueue.main.async {
// Load all resources here
}
}
I can understand this can be bad practice if the app is very large, but I think in this scenario the app isn't huge, but the lag between the view controllers is enough to be annoying. I would rather have the user wait at the start for 3-5 seconds whilst everything loads, and have a smooth experience once inside. Is there a way to do this?
You shouldn't call the lifecycle functions of the viewcontroller for yourself. The viewDidLoad function will be called when the view has been loaded.
Apple: This method is called after the view controller has loaded its view hierarchy into memory.
So calling let _ = viewController.view will trigger the view creation and the call of this function.
But i think it's much better to have a startup phase instead. Think about a 'startup'-screen that downloads everything you need (with maybe a spinner and a text) and moves automatically to the content (the tabbar controller) when done. That may also fix the problem of a low network connectivity (think about a download that take a minute for example). You may display that screen modally above or as screen before the tabbbar controller.
If you don't like the idea of a startup phase you may also design your ui responsive. Start the download whenever needed / regularly and update your ui according to the results when ready. The ui will be fast then, but uses last known data. The meaningfulness and implementation depends on your content.
Update
On second thought: If you already have a server login screen, why not download the content directly after the successful download as part of the login? Users do not know if you are still checking the login or downloading some necessary data. You may say that login is only successful if server login AND download are finished successfully.

background methods are not stopping after viewWillDisappear/ Dealloc

First of all Check This to understand what i am doing
I did not get my answer in above question & still waiting for it.
Now new porblem is that when i click on back button using following code, methods of last ViewController are still running. It will use memory & keep processing untill it gets response(that`s what i want to do but if user press back then i want to stop all methods)
[self.navigationController popViewControllerAnimated:YES];
How do i stop it?
You need to do [getTryAgainTask cancel] (assuming getTryAgainTask is of type NSURLSessionDownloadTask) before you pop this controller. The download task is asynchronous and runs irrespective of the controller (that fired it) being deallocated. This might cause retain loops, leading the app to eventually crash. The code as of now, will go into an infinite loop. A better solution would be to keep a tab on the number of retries (say 3 times) and then prompt the user about the problem, asking if he/she would like to try again.

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.

Resources