Ios NSURLConnection返回错误的缓存数据
在AppDelegate方法中,我创建缓存Ios NSURLConnection返回错误的缓存数据,ios,objective-c,nsurlconnection,Ios,Objective C,Nsurlconnection,在AppDelegate方法中,我创建缓存 NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:(10 * 1024 * 1024) diskCapacity:(100 * 1024 * 1024) diskPath:nil]; [NSURLCache setSharedURLCache:URLCache]; 我有下一节课 @implementation ImageDownloader { NSURLCon
NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:(10 * 1024 * 1024) diskCapacity:(100 * 1024 * 1024) diskPath:nil];
[NSURLCache setSharedURLCache:URLCache];
我有下一节课
@implementation ImageDownloader {
NSURLConnection *serverConnection;
NSMutableData *imageData;
}
- (void)startDownloading
{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.link] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:10];
imageData = [NSMutableData new];
serverConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
[serverConnection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
[serverConnection start];
}
- (void)cancelDownloading
{
[serverConnection cancel];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[imageData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
UIImage *image = [[UIImage alloc] initWithData:imageData];
[self sendDelegateImage:image];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[self sendDelegateImage:nil];
}
- (void)sendDelegateImage:(UIImage *)image
{
[self.delegate imageDownloader:self didLoadAtIndexPath:self.indexPath image:image];
}
@end
当我的tableView单元格出现时使用它。在第一次加载时都很好,在第一次使用缓存时都很好,但是当我在第三次加载我的tableView时,缓存数据返回的非常小,并且我没有图像。为什么NSURLConnection返回错误的缓存数据?您可以尝试实现
连接:didReceiverResponse:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
self.dataReceived = [[NSMutableData alloc] init];
}
从文档中:
在极少数情况下,例如在HTTP加载的情况下
加载数据的内容类型为multipart/x-mixed-replace,即
代表将收到多个连接:didReceiveResponse:
消息如果发生这种情况,学员应放弃所有数据
以前由connection:didReceiveData:传递,并且应该是
准备好处理报告的可能不同的MIME类型
新报告的URL响应
编辑:
另外,刚刚注意到您正在使用[NSMutableData new]
初始化数据;您应该使用[NSMutableData alloc]init]