IPhone SDK - Leaking Memory with performSelectorInBackground
seems to suggest you can actually pushViewController in background thread.
But I get a warning from stackoverflow people that I shouldn't do this.
eg. (iphone) start timer from background thread?
is pushViewController considered "UI updating"?
(because I know that UI updating should be done in main thread only)
If it is so,
when a viewController takes a while to load, and want to show an indicator while loading.
What is my option?
Couple of strategies here.
1) BEFORE you do the push, but at the point you know you are going to do it, bring up a suitable activity view on the current view. I do this in some apps where you click on a row in a table but the pushed view has to do some web comms that takes time, so I leave the table cell highlighted in blue and add a white spinner to the left of the disclosure indicator. Works well.
2) Use lazy loading to get the new view controller on screen quickly, but defer the heavy code until after it has loaded so that the new controller can look after it's own activity view. By lazy loading I mean you should do as little as possible in the init method and make careful use of viewdidload / viewwillappear / viewdidappear etc to spread the work out and get an activity view on screen as soon as you can.
Related
I get used to put either of viewWillAppear and viewDidLoad, it's OK until know. However I'm thinking there should be some rules that guide when to put into viewWillAppear and when to put into viewDidLoad?
Simple rule I use is this. viewDidLoad is when the view's resources are loaded. The view is not drawn onscreen yet. So calculations and code dealing with the geometry and visuals of the view should not be put here. They should be in the viewWillAppear or viewDidAppear method.
Also viewWillAppear can be called multiple times
When a popover/modal view is displayed and remove
When an alert view/actionsheet/uiactivityController's view is displayed and removed.
For these reason, viewWillAppear should not contain codes that takes longer to finish. (at least the code running on the main thread). Neither should codes that only needs to be run once per view display.
There are more I am sure but these are simple to remember and I hope it helps.
viewDidLoad: Alerts you that a view has finished loading
viewWillAppear: Runs just before the view loads
viewDidLoad is things you have to do once. viewWillAppear gets called every time the view appears. You should do things that you only have to do once in viewDidLoad - like setting your UILabel texts. However, you may want to modify a specific part of the view every time the user gets to view it, e.g. the iPod application scrolls the lyrics back to the top every time you go to the "Now Playing" view.
However, when you are loading things from a server, you also have to think about latency. If you pack all of your network communication into viewDidLoad or viewWillAppear, they will be executed before the user gets to see the view - possibly resulting a short freeze of your app. It may be good idea to first show the user an unpopulated view with an activity indicator of some sort. When you are done with your networking, which may take a second or two (or may even fail - who knows?), you can populate the view with your data. Good examples on how this could be done can be seen in various twitter clients. For example, when you view the author detail page in Twitterrific, the view only says "Loading..." until the network queries have completed.
I have the need for an activity indicator view in my app when different views are loading and when data is being retrieved. The problem is the mainVC (where I would place the indicator) is not always aware of when processing is happening so it can start the indicator but it cannot stop it.
e.g. the mainVC loads and then programatically adds a new VC - this VC in turn asks a model to retrieve - it displays data etc. So this newly added VC actually knows when processing is finished and it does not have access to the indicator view (although the indicator is visible at the top).
I was thinking of using notifications - is this the best way of handling this situation?
I'd recommend looking at the brilliant MBProgressHUD library:
https://github.com/matej/MBProgressHUD
It's a very simple set of classes you can use to display loading and progress views that can be accessed by all view controllers in your app. Basically, you can set it up in your app delegate and add it to your app window.
Every view controller can then access the progress view from the delegate and show/hide it when required. It comes with an example project and code - it's very easy to use and customise.
Notifications are one half of the solution. You have to combine them with a persistent object so that you can also get the current state at all times. E.g., when a view controller is about to appear, it needs to read the initial "downloading" state from somewhere, because the VC might have been created after the "start" or "end" notification was sent.
Then, while the VC is alive, it can simply respond to notifications to update the indicator.
This design is particulary important for views, which run the risk of getting unloaded/reloaded all the time.
I tried to show activity indicator while heavy UI update.
My initial failed attempt was
show activity indicator at view1
pushViewController (creating view of this viewController takes a long time)
when view1's viewDidDisappear(when pushViewController's push animation
occurs), hide activity indicator
Problem was even though I did call "show activity indicator" before pushViewController, activity indicator wasn't showing because of heavy UI which occurred in creating the view of viewController which was being pushed.
I changed strategy as bellow and it works and I don't get why.
pushViewController
show activity indicator at viewDidLoad of viewController
do the heavy UI loading at viewDidAppear and hide indicator
I was under impression the reason that the indicator didn't show up with the first method was due to heavy UI process.
Please refer to the discussion at link.
(iphone) showing activity indicator before heavy ui updating
If too much UI update on queue caused delayed UI update(indicator not showing up), why the 2nd method makes a difference?
I know that viewDidLoad gets called only when view gets created.
And viewDidAppear gets called when view actually shows up on screen.(hence can be called multiple times)
But is there a fundamental difference which would explain why the 2nd approach of using viewDidAppear works in my situation?
And is there a UI update thread apart from the so-called main thread?
Or main thread is the UI update thread?
Thank you
First I want to make sure that you're updating UIKit UI on the main thread. This is a must, you shouldn't have to specify a thread when working with UIKit, unless you say to use the main thread, but I think that would be redundant.
I would start the activity indicator in viewWillAppear. Then load everything else in viewDidLoad.
Suppose I have a navigation controller where the next action is to take a picture or select an image from the library.
If I initialize UIImagePickerController during didSelectRowAtIndexPath:, (I believe) the response will be a little slower as the controller needs to initialized. Also, if the user cancels and opens again, it would reinstantiate that controller every time.
However, if I create the controller during viewDidLoad: of the navigation controller, it takes up memory while the user is on that view. Side questions: Does this, however, slow down the loading time of the navigation?
Or should it be done in an NSOperation when the view is loaded?
Overall, what would be the best place to load the ImagePicker?
I'd lazy load the controller when it first gets called (in didSelectRowAtIndexPath) so it wouldn't have to be reloaded every time, and not worry about initialization time.
It seems that in your case you will always need an image picker on didSelectRowAtIndexPath. You can load it on the view and customize (camera, cameraroll, etc.) and present on the row selection. Probably it doesn't matter that much. UIImagePickerController presentation is very slow anyway, especially with the camera.
I don't recommend an NSOperation for this task.
I have a set number of ViewControllers in my app. They are allocated and initialized at application launch and released at exit. They are used by a NavigationController to be pushed/popped. In these ViewControllers there are WebViews (actually there is nothing else).
My problem is :
When I want to change de content (URL) of a WebView that is not on the current TopViewController, the content isn't loaded until I push/pop the associated ViewController.
And the transition us "ugly". The pushed/popped Viewcontroller shows old content at worst or blank page at best, before the ViewController is in full view THEN the new content is shown.
I tried lots of things (even putting the "loadRequest" in a different thread with looks stupid).
Do you know any way to make things go smooth?
In general you should plan your UI for slow UIWebView loads, because you can't plan ahead to know how long things will take. What I often do is have a UIView that has a spinner and a message such as "Loading..." that is layered on top of the UIWebView. Here's how I use it:
In viewWillAppear: I unhide the loading view and start the spinner.
In webViewDidFinishLoad I hide the loading view.
If webViewDidFail gets called, then you're not showing some interim or invalid content while the page loads. You can present a UIAlertView in that case.
If you're curious, you can see this in the high score page of my game (Lexitect, free)