开发者

Lazy loading in Objective-C -- too lazy?

I have a UIViewController that needs to set a few labels when it receives a new (object) value for one of its properties:

-(void)setCurrentEvent:(Event *)e {
    [currentEvent release];
    currentEvent = [e retain];
    self.dateLabel.text = currentEvent.subtitle;
    self.summaryTe开发者_如何学PythonxtView.text = currentEvent.summary;
    self.avgRatingLabel.text = [NSString stringWithFormat:@"%.1f",currentEvent.avgRating];
    [self setTitle:currentEvent.title];
    [self.view setNeedsDisplay];
}

I found that when the values are set for the first time, the label and text view objects are not initialized yet and thus their new values are not set. After the initial call of setCurrentEvent all goes well, but I think I am relying on lazy loading a bit too much here?


Assuming it is being awaken from a nib or loaded from one:

Until your view controller's viewDidLoad method is called, there is no guarantee any of your IBOutlets are set.

What you are seeing I suspect is that the first time the values are set your view hasn't loaded yet, and by the time the values are set for the second, they have.

You should defer processing until the view is loaded, or create the views yourself as required.

EDIT

In light of your comment, it seems you are loading from a nib. In this case I would perhaps retain the instance variable (currentEvent say), then call a method like updateUI which sets the properties correctly. I would then also put updateUI in viewDidLoad so when the labels become available they are updated against the current event.


It is fine (good) to be very lazy, but you need to be careful about initialization ordering if you do not have everything set to init on demand. What I generally do is something like this

- (UILabel) dateLabel {
  if (!dateLabel) {
    dateLabel = [[UILabel alloc] initWithFrame:FRAME_POSITION];
  }

  return dateLabel;
}

That way you know with certainty that field is initialized when you access it, since the accessor is the lazy initializer. The one catch you need to be careful about recursive dependencies, but I have used this for large complicated initialization chain.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜