河南省建設(shè)工程一體化平臺常州seo招聘
一、瘦身技術(shù)大圖
二、On-Demand Resources 簡介
?將其保存管理在蘋果的服務(wù)器,按需使用資源、優(yōu)化包體積,實現(xiàn)更小的應(yīng)用程序。ODR?的好處:
- 應(yīng)用體積更小,下載更快,提升初次啟動速度
- 資源會在后臺下載
- 操作系統(tǒng)將會在磁盤資源不夠的時候清理 ODR
三、實現(xiàn)
3.1、創(chuàng)建標(biāo)簽
標(biāo)簽的理想大小小于或等于64 MB。這種尺寸在下載速度和本地存儲空間之間提供了良好的平衡,以便在設(shè)備的本地存儲空間不足時進(jìn)行清理。
- Initial install tags:初始安裝標(biāo)簽,資源與應(yīng)用程序同時下載;
- Prefetch tag order.:預(yù)取標(biāo)簽順序,安裝應(yīng)用程序后,資源開始下載;
- Dowloaded only on demand:僅按需下載。當(dāng)應(yīng)用程序要求時,標(biāo)簽會下載;
?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]; ? ? /// 檢查設(shè)備上是否已有標(biāo)簽 ? ? [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 對象只能用于一個請求訪問/結(jié)束訪問周期。 @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]; ? ? ? ? // 設(shè)置優(yōu)先級 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、一些額外方法
/// 暫停當(dāng)前下載 - (void)pauseOdr { ? ? [self.request.progress pause]; } /// 恢復(fù)當(dāng)前下載 - (void)resumeOdr { ? ? [self.request.progress resume]; } /// 取消當(dāng)前下載 - (void)cancelOdr { ? ? [self.request.progress cancel]; } |
3.5、運行后的效果
3.6、通常的設(shè)計原則
- 必要時可用
- 下載期間影響最小
- 對應(yīng)用程序內(nèi)存的影響最小
四、參考文獻(xiàn)
- On-Demand Resources Guide
- NSBundleResourceRequest