iOS - 资源按需加载 - ODR

一、瘦身技术大图

二、On-Demand Resources 简介

将其保存管理在苹果的服务器,按需使用资源、优化包体积,实现更小的应用程序。ODR 的好处:

  • 应用体积更小,下载更快,提升初次启动速度
  • 资源会在后台下载
  • 操作系统将会在磁盘资源不够的时候清理 ODR

三、实现

3.1、创建标签

标签的理想大小小于或等于64 MB。这种尺寸在下载速度和本地存储空间之间提供了良好的平衡,以便在设备的本地存储空间不足时进行清理。

  • Initial install tags:初始安装标签,资源与应用程序同时下载;
  • Prefetch tag order.:预取标签顺序,安装应用程序后,资源开始下载;
  • Dowloaded only on demand:仅按需下载。当应用程序要求时,标签会下载;

3.2、pod组建引用

|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| s.on_demand_resources = { 'Tag1' => 'file1.png' } s.on_demand_resources = { 'Tag1' => ['file1.png', 'file2.png'] } s.on_demand_resources = { 'Tag1' => { :paths => ['file1.png', 'file2.png'], :category => :download_on_demand } } s.on_demand_resources = { 'Tag1' => { :paths => ['file1.png', 'file2.png'], :category => :initial_install } } |

3.3、访问和下载资源

|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| - (NSBundleResourceRequest *)requestOdrWithTags:(NSSet<NSString *> *)tags { NSBundleResourceRequest *request = [[NSBundleResourceRequest alloc] initWithTags:tags]; /// 检查设备上是否已有标签 [request conditionallyBeginAccessingResourcesWithCompletionHandler:^(BOOL resourcesAvailable) { if (!resourcesAvailable) { /// 资源不在本地 [request beginAccessingResourcesWithCompletionHandler:^(NSError * _Nullable error) { if (error) { /// 下载失败 [self failRetryWithTags:tags]; } else { /// 下载成功 self.retryDelay = 0; } }]; } else { /// 资源已存在 self.retryDelay = 0; } }]; return request; } |

|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| @interface IMYOdrManager () /// 持有 NSBundleResourceRequest,资源才会被使用,要不然会被释放 /// 每个NSBundleResourceRequest 对象只能用于一个请求访问/结束访问周期。 @property (nonatomic, strong) NSBundleResourceRequest *request; @end @implementation IMYOdrManager @synthesize tagName; IMY_KYLIN_FUNC_LAUNCHED_ASYNC { NSSet *set = [NSSet setWithObject:@"IMYLevel1"]; [[IMYOdrManager sharedInstance] reloadOdrWithTags:set]; } + (instancetype)sharedInstance { static id instance; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ instance = [self new]; }); return instance; } - (instancetype)init { if (self = [super init]) { [self addObserver]; } return self; } #pragma mark - public - (void)reloadOdrWithTags:(NSSet *)set { self.tagName = set; [self reloadOdr]; } - (void)reloadOdr { if (self.tagName.count > 0) { self.request = [[IMYOdrDownloadManager new] requestOdrWithTags:self.tagName]; // 设置优先级 between 0.0 and 1.0 self.request.loadingPriority = 1.0; } } #pragma mark - private - (void)addObserver { /// 低空位警告 @weakify(self); [[[[[NSNotificationCenter defaultCenter] rac_addObserverForName:NSBundleResourceRequestLowDiskSpaceNotification object:nil] deliverOnMainThread] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(NSNotification *notify) { @strongify(self); [self.request endAccessingResources]; }]; } @end |

3.4、一些额外方法

|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| /// 暂停当前下载 - (void)pauseOdr { [self.request.progress pause]; } /// 恢复当前下载 - (void)resumeOdr { [self.request.progress resume]; } /// 取消当前下载 - (void)cancelOdr { [self.request.progress cancel]; } |

3.5、运行后的效果

3.6、通常的设计原则

  • 必要时可用
  • 下载期间影响最小
  • 对应用程序内存的影响最小

四、参考文献

相关推荐
游戏开发爱好者84 小时前
日常开发与测试的 App 测试方法、查看设备状态、实时日志、应用数据
android·ios·小程序·https·uni-app·iphone·webview
黑码哥4 小时前
ViewHolder设计模式深度剖析:iOS开发者掌握Android列表性能优化的实战指南
android·ios·性能优化·跨平台开发·viewholder
2501_915106326 小时前
app 上架过程,安装包准备、证书与描述文件管理、安装测试、上传
android·ios·小程序·https·uni-app·iphone·webview
2501_915106326 小时前
使用 Sniffmaster TCP 抓包和 Wireshark 网络分析
网络协议·tcp/ip·ios·小程序·uni-app·wireshark·iphone
熊猫钓鱼>_>6 小时前
移动端开发技术选型报告:三足鼎立时代的开发者指南(2026年2月)
android·人工智能·ios·app·鸿蒙·cpu·移动端
徐同保1 天前
通过ip访问nginx的服务时,被第一个server重定向了,通过设置default_server解决这个问题
ios·iphone
2501_915918411 天前
在 iOS 环境下查看 App 详细信息与文件目录
android·ios·小程序·https·uni-app·iphone·webview
2501_916007471 天前
没有 Mac 用户如何上架 App Store,IPA生成、证书与描述文件管理、跨平台上传
android·macos·ios·小程序·uni-app·iphone·webview
夏幻灵2 天前
HTTPS全面解析:原理、加密机制与证书体
ios·iphone
TheNextByte12 天前
如何在iPhone上恢复已删除的笔记的综合指南
笔记·ios·iphone