开发者

Lazy loading in objective C

开发者 https://www.devze.com 2022-12-15 23:28 出处:网络
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 an

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 general pattern for lazy loading is always more or less the same:

- (Whatever *)instance
{
    if (_ivar == nil)
    {
        _ivar = [[Whatever alloc] init];
    }
    return _ivar;
}
  1. In your class, add an ivar of the type you need, and initialize that to nil in the constructor;
  2. Create a getter method for that ivar;
  3. In the getter, test for nil. If so, create the object. Otherwise, just return the reference to it.


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).


This would be the appropriate way according to Apple. I agree with them for multiple reasons:

  • An static variable inside a method will persist over multiple calls.
  • The dispatch_once function of GDC will guarantee that the given code block will only run once.
  • It's thread-safe.

Objective-C:

- (AnyClass*)instance {

    static AnyClass *shared = nil;
    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{
        shared = [[AnyClass alloc] init];
    });

    return shared;
}


In your *.h class isDragging_msg and isDecliring_msg these 2 is BOOL value. and Dict_name NSMutableDictionary.

In view did load

Dict_name = [[NSMutableDictionary alloc] init];

In cell for row at index path

if ([dicImages_msg valueForKey:[[msg_array objectAtIndex:indexPath.row] valueForKey:@"image name or image link"]]) 
{ 
    cell.image_profile.image=[dicImages_msg valueForKey:[[msg_array objectAtIndex:indexPath.row] valueForKey:@"image name or image link"]];
}
else
{
    if (!isDragging_msg && !isDecliring_msg)
    {
        [dicImages_msg setObject:[UIImage imageNamed:@"Placeholder.png"] forKey:[[msg_array objectAtIndex:indexPath.row] valueForKey:@"image name or image link"]];
        [self performSelectorInBackground:@selector(downloadImage_3:) withObject:indexPath];
    }
    else
    {
        cell.image_profile.image=[UIImage imageNamed:@"Placeholder.png"];
    }
}

and for download image the function is:-

-(void)downloadImage_3:(NSIndexPath *)path
{
    NSAutoreleasePool *pl = [[NSAutoreleasePool alloc] init];

    NSString *str=[here Your image link for download];

    UIImage *img = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:str]]]; 

    [dicImages_msg setObject:img forKey:[[msg_array objectAtIndex:path.row] valueForKey:@"image name or image link same as cell for row"]];

    [tableview performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];

    [pl release];
}

and at last put these methods in your class

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    isDragging_msg = FALSE;     
    [tableview reloadData];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    isDecliring_msg = FALSE;
    [tableview reloadData]; 
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    isDragging_msg = TRUE;
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    isDecliring_msg = TRUE; 
}
0

精彩评论

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