- (NSCachedURLResponse *)localCacheResponeWithRequest:(NSURLRequest *)request
if (isExist == nil) {
[self.responseDic setValue:[NSNumber numberWithBool:TRUE] forKey:request.URL.absoluteString];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (error) {
cachedResponse = nil;
} else {
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%f",[date timeIntervalSince1970]],@"time",response.MIMEType,@"MIMEType",response.textEncodingName,@"textEncodingName", nil];
BOOL resultO = [dic writeToFile:otherInfoPath atomically:YES];
BOOL result = [data writeToFile:filePath atomically:YES];
if (resultO == NO || result == NO) {
} else {
}
cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:response data:data];
}
}];
[task resume];
return cachedResponse;
}
return nil;
因为 [session dataTaskWithRequest:request completionHandler:^]是异步请求,所以 cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:response data:data]; 其实是没什么效果?
展开