Abhinav
Abhinav

Reputation: 38142

Calling loadView explicitly -- good/bad?

Is there any harm in calling loadView explicitly?

I have to hit the server and get the data for display and this data will be changed based on some user action on this view. I am making the server call in my loadView method and passing the appropriate parameters. Now when user condition changes, I call [self loadView] with modified parameters. Do you see some issue here?

Upvotes: 5

Views: 2278

Answers (4)

Abhinav
Abhinav

Reputation: 38142

I have made another server call, fetched the data, updated the modal and then refreshed the view. NO CALL TO loadView!!!

Upvotes: 0

Anomie
Anomie

Reputation: 94794

Call the UIViewController's view method instead. That will call loadView if necessary.

Upvotes: 5

Tommy
Tommy

Reputation: 100612

Bad. Because:

  1. loadView is provided for you to add things to the view programmatically; it's designed explicitly to be synchronous;
  2. calling loadView repeatedly may cause memory leaks as objects that have already been loaded are loaded again.

Set up your view to have two configurations, communicating to the user either that it is loading from the server or that it is available for input, even it's just having a spinner visible or not having a spinner visible. Create a means to switch between them. Use it to communicate with your user.

Trying to subvert intended patterns is always going to lead you down a road where, at the absolute best of times most things appear to work and you fool yourself that one more hack will resolve the final problem, and at the worst of times nothing much works any more and you've spent so long digging yourself into the whole that getting out means starting again anyway.

Upvotes: 1

Ken Pespisa
Ken Pespisa

Reputation: 22266

Well I think the Apple documentation says it all:

loadView Creates the view that the controller manages.

-(void)loadView You should never call this method directly.

Upvotes: 13

Related Questions