Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone Three20 TTURLRequest同步缓存(使其异步还是线程化)?_Iphone_Three20_Synchronous - Fatal编程技术网

Iphone Three20 TTURLRequest同步缓存(使其异步还是线程化)?

Iphone Three20 TTURLRequest同步缓存(使其异步还是线程化)?,iphone,three20,synchronous,Iphone,Three20,Synchronous,我目前正在与该库合作,为一个iPhone项目实现一些缓存。这对加快我的表视图和页面加载有很大帮助,但我遇到了一个小问题: 我将tturlmageresponse子类化,以便在缓存远程检索的图像之前调整/裁剪它们,这样我就不必在每次从服务器/缓存取回它们时调整它们的大小。这非常有效,图像加载速度非常快,但是当图像从Three20库中的缓存返回时,它们会同步返回。这会导致在触发从缓存返回的10-20个映像请求时出现明显的延迟。因为它们是同步返回的,所以它们阻塞了我的UI线程,用户需要等待几秒钟才能看

我目前正在与该库合作,为一个iPhone项目实现一些缓存。这对加快我的表视图和页面加载有很大帮助,但我遇到了一个小问题:

我将
tturlmageresponse
子类化,以便在缓存远程检索的图像之前调整/裁剪它们,这样我就不必在每次从服务器/缓存取回它们时调整它们的大小。这非常有效,图像加载速度非常快,但是当图像从Three20库中的缓存返回时,它们会同步返回。这会导致在触发从缓存返回的10-20个映像请求时出现明显的延迟。因为它们是同步返回的,所以它们阻塞了我的UI线程,用户需要等待几秒钟才能看到从缓存返回的图像

我已尝试通过执行以下操作来执行该操作:

- (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];
}

但是线程似乎不工作(回调在主线程上…)。有人知道从Three20缓存中异步检索的方法吗?或者知道线程化Three20的方法吗?

[request send]应该是异步的。您是否尝试过从主线程调用send?根据三个20文档:“如果请求可以通过缓存解决,那么它将同步发生。否则,请求将异步响应其委托。”因此,您希望在后台执行setImage调用。为什么不在requestDidFinishLoad方法中生成一个线程呢?不,我想使请求(一次生成10-50个)异步或线程化,因为许多调用都是同步进行的,因为它们从缓存返回。setImage是一个非常小的事务。来自Three20缓存的多个同步响应出现延迟。您尝试过TTURLRequestQueue吗?