mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-24 04:16:00 +08:00
Cache policy control for image source
Summary: In the context of an app an image exists in three resolutions on the server: `thumb` (30px) `feed` (300px) `full` (900px). When looking at an individual item a user can come either from the feed, via a permalink or from other parts of the app. This allows a situation where the `feed` image might or might not already be loaded somewhere in the app. In the detail view I want to render `thumb` with a blur (to quickly display something), then the `feed` image if it exists to have something decent to display until `full` loads. However it is quite a waste to load the `feed` image if it isn't already in cache, and will slow down the time until `full` is loaded. It is possible to track the navigation from feed->detail and that the `feed` image has actually completed loading by the feed component however as component hierarchies grow this turns into quite a lot of prop passing and bad separation of concerns. NSURLRequests accepts a [Cache Policy](https://developer.apple.com/reference/fo Closes https://github.com/facebook/react-native/pull/10844 Differential Revision: D4425959 Pulled By: lacker fbshipit-source-id: 679835439c761a2fc894f56eb6d744c036cf0b49
This commit is contained in:
committed by
Facebook Github Bot
parent
a6844bdf75
commit
52d8851fc8
@@ -47,6 +47,7 @@
|
||||
+ (NSData *)NSData:(id)json;
|
||||
+ (NSIndexSet *)NSIndexSet:(id)json;
|
||||
|
||||
+ (NSURLRequestCachePolicy)NSURLRequestCachePolicy:(id)json;
|
||||
+ (NSURL *)NSURL:(id)json;
|
||||
+ (NSURLRequest *)NSURLRequest:(id)json;
|
||||
|
||||
|
||||
@@ -118,6 +118,14 @@ RCT_CUSTOM_CONVERTER(NSData *, NSData, [json dataUsingEncoding:NSUTF8StringEncod
|
||||
}
|
||||
}
|
||||
|
||||
RCT_ENUM_CONVERTER(NSURLRequestCachePolicy, (@{
|
||||
@"default": @(NSURLRequestUseProtocolCachePolicy),
|
||||
@"reload": @(NSURLRequestReloadIgnoringLocalCacheData),
|
||||
@"force-cache": @(NSURLRequestReturnCacheDataElseLoad),
|
||||
@"only-if-cached": @(NSURLRequestReturnCacheDataDontLoad),
|
||||
}), NSURLRequestUseProtocolCachePolicy, integerValue)
|
||||
|
||||
|
||||
+ (NSURLRequest *)NSURLRequest:(id)json
|
||||
{
|
||||
if ([json isKindOfClass:[NSString class]]) {
|
||||
@@ -140,8 +148,9 @@ RCT_CUSTOM_CONVERTER(NSData *, NSData, [json dataUsingEncoding:NSUTF8StringEncod
|
||||
|
||||
NSData *body = [self NSData:json[@"body"]];
|
||||
NSString *method = [self NSString:json[@"method"]].uppercaseString ?: @"GET";
|
||||
NSURLRequestCachePolicy cachePolicy = [self NSURLRequestCachePolicy:json[@"cache"]];
|
||||
NSDictionary *headers = [self NSDictionary:json[@"headers"]];
|
||||
if ([method isEqualToString:@"GET"] && headers == nil && body == nil) {
|
||||
if ([method isEqualToString:@"GET"] && headers == nil && body == nil && cachePolicy == NSURLRequestUseProtocolCachePolicy) {
|
||||
return [NSURLRequest requestWithURL:URL];
|
||||
}
|
||||
|
||||
@@ -164,6 +173,7 @@ RCT_CUSTOM_CONVERTER(NSData *, NSData, [json dataUsingEncoding:NSUTF8StringEncod
|
||||
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
|
||||
request.HTTPBody = body;
|
||||
request.HTTPMethod = method;
|
||||
request.cachePolicy = cachePolicy;
|
||||
request.allHTTPHeaderFields = headers;
|
||||
return [request copy];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user