开发者

Three20 TTURLRequest Synchronous Cache (make it async or thread it)?

I'm currently working with the Three20 library to implement some caching for an iPhone project. It has been a huge help for speeding up my table views and page loads, however I'm running into a small issue:

I have subclassed TTURLImageResponse to resize/crop images that are retrieved remotely before caching them so that I don't have to resize them each time I get them back from the server/cache. This works very well and the images load quite quickly, however when images are returned from the cache in the Three20 library they are returned synchronously. This results in a noticeable delay when firing off 10-20 image requests that come back from the cache. Since they are coming back synchronously, they are blocking my UI thread and the user is left waiting for a couple seconds before they see the images that came back from the cache.

I have attempted to thread the operation by doing the following:

- (void) setupImages
{   

    if(imageList != nil &&
       [imageList count] > 0)
    {
        [NSThread detachNewThreadSelector:@selector(fillImages) toTarget:self withObject:nil];
    }
    else
    {
        lblMessage.hidden = NO;
        lblMessage.text = @"There are no images. Try refreshing the current list.";
        lblTitle.text = @"";
        lblAuthor.text = @"";
    }
}

- (void)fillImages
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    int i=0;

    TTURLRequest *request;

    for (MyImage *imageObj in imageList)
    {
        NSString *imageUrl = [[[imageObj thumbnail_lg] image_url] absoluteString];
        if(imageUrl != nil)
        {           
            request = [TTURLRequest requestWithURL:imageUrl delegate:self];
            MyTTURLImageResponse *responseObj = [[[MyTTURLImageResponse alloc] init] autorelease];
            responseObj.index = i;

            request.cachePolicy = TTURLRequestCachePolicyDisk;
            request.response = responseObj;
            request.httpMethod = @"GET";
            [request send];

            i++;
        }
    }

    [pool release];
}

//When the fillImages selector is spawned on its own thread it does not callback to this method, therefore images are never loaded!!
- (void)requestDidFinishLoad:(TTURLRequest*)request 
{
    MyTTURLImageResponse *response = request.response;

    UIImage *loadedImage = response.image;
    int imageIndex = response.index;

    //this now happens as the image is cached :)
    //loadedImage = [loadedImage cropCenterAndScaleImageToSize:CGSizeMake(200, 200)];

    [myImageDisplay setImage:loadedImage forIndex:imageIndex];
}

However the threading doesn't appear to be working (the callback is on the main thread...). Does anyone know of a way to retrieve from the Three20 Cache either ASYNCHRONOUSLY or a way of threading the Three20 TTURLRequest开发者_开发百科 so that it's at least doing this in the background?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜