I heard lazy loading technique quite helpful to increase the performance of the programme. I am developing games for iPhone. I am not sure how is the way to apply lazy loading in objective C. Could anyone show me the example please?
Thanks in advance
The first time the managedObjectModel is asked for, it is created by the code. Any time after that, it already exists ( != nil ) and is just returned. That's one example of lazy loading.
What is lazy loading: The phrase “lazy loading” is used to describe the act of downloading pictures in layman's terms. As a result, the software does not become unresponsive as images are downloaded.
The general pattern for lazy loading is always more or less the same:
- (Whatever *)instance
{
if (_ivar == nil)
{
_ivar = [[Whatever alloc] init];
}
return _ivar;
}
Here's an example of lazy loading from the Core Data template:
- (NSManagedObjectModel *)managedObjectModel
{
if (managedObjectModel != nil) {
return managedObjectModel;
}
managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];
return managedObjectModel;
}
The first time the managedObjectModel
is asked for, it is created by the code. Any time after that, it already exists (!= nil
) and is just returned. That's one example of lazy loading. There are other kinds, such as lazy loading of NIB files (loading them into memory only when they're needed).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With