文件下載是app中常用的一個需求場景。需求在下載進程中顯示下載進度,並在下載完成時停止處置。可以運用察看者形式來停止處置。
本文包括下述三個局部:
- 察看者形式簡介
- 文件下載監控中的運用
- 其它留意事項
察看者形式是行為形式中的一種,察看者監聽被察看者的屬性等變化。它允許定義一對多的依賴關系,讓多個察看者同時監聽一個對象。蘋果Cocoa框架中經過3種方式完成了該形式:告訴,KVO(Key value observing)及Delegate。
1 告訴 NSNotificationCenter
[NSNotificationCenter defaultCenter]
獲取一個NSNotificationCenter實例。
1.1 添加察看者:
- (void)addObserver:(id)observer selector:(SEL)aSelector name:(nullable NSString *)aName object:(nullable id)anObject;
- (id <NSObject>)addObserverForName:(nullable NSString *)name object:(nullable id)obj queue:(nullable NSOperationQueue *)queue usingBlock:(void (^)(NSNotification *note))block NS_AVAILABLE(10_6, 4_0);
// The return value is retained by the system, and should be held onto by the caller in
// order to remove the observer with removeObserver: later, to stop observation.
1.2 收回告訴
- (void)postNotification:(NSNotification *)notification;
- (void)postNotificationName:(NSString *)aName object:(nullable id)anObject;
- (void)postNotificationName:(NSString *)aName object:(nullable id)anObject userInfo:(nullable NSDictionary *)aUserInfo;
1.3 移除告訴
- (void)removeObserver:(id)observer;
- (void)removeObserver:(id)observer name:(nullable NSString *)aName object:(nullable id)anObject;
2 KVO
2.1 添加察看屬性
- (void)addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(nullable void *)context;
- (void)addObserver:(NSObject *)observer toObjectsAtIndexes:(NSIndexSet *)indexes forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(nullable void *)context;
2.2 完成察看呼應函數
- (void)observeValueForKeyPath:(nullable NSString *)keyPath ofObject:(nullable id)object change:(nullable NSDictionary<NSString*, id> *)change context:(nullable void *)context;
2.3 移除察看者
- (void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath context:(nullable void *)context NS_AVAILABLE(10_7, 5_0);
- (void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath;
3 Delegate
3.1 設置delegate
需求留意為防止循環援用(Retain cycle),需求設置delegate為weak屬性,更多關於防止循環援用的留意事項,參考這篇文章。
rules-to-avoid-retain-cycles
3.2 完成delegate辦法,可以自定義。
二 文件下載監控中的運用依據文件下載的場景,次要是監控下載形態(下載中,下載失敗[比方網絡問題],下載完成)fileDownloadState,下載文件大小fileDownloadedSize。
因而添加的察看屬性函數語句為:
[observer addObserver:self forKeyPath:@"fileDownloadState" options:NSKeyValueObservingOptionNew context:nil];
[observer addObserver:self forKeyPath:@"fileDownloadedSize" options:NSKeyValueObservingOptionNew context:nil];
然後在函數中處置:
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
if (![NSThread isMainThread]) {
dispatch_async(dispatch_get_main_queue(), ^{
[self observeValueForKeyPath:keyPath
ofObject:object
change:change
context:context];
});
return;
}
if ([keyPath isEqualToString:@"fileDownloadedSize"]){
[self.fileView renderProgress];
//handle codes
}else if ([keyPath isEqualToString:@"fileDownloadState"]) {
//handle codes
}
}
最後移除察看者:
[observer removeObserver:self forKeyPath:@"fileDownloadState"];
[observer removeObserver:self forKeyPath:@"fileDownloadedSize"];
三 其它留意事項
需求在下載前檢測文件零碎的剩余空間,檢測函數參考
how-to-detect-total-available-free-disk-space-on-the-iphone-ipad-device
我封裝了一個函數來失掉剩余的設備磁盤空間:
- (uint64_t)freeDiskspace {
uint64_t totalFreeSpace = 0;
__autoreleasing NSError *error = nil;
NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:&error];
if (dictionary) {
NSNumber *freeFileSystemSizeInBytes = [dictionary objectForKey:NSFileSystemFreeSize];
totalFreeSpace = [freeFileSystemSizeInBytes unsignedLongLongValue];
} else {
QZLOG_ERROR(@"Error Obtaining System Memory Info: Domain = %@, Code = %ld",[error domain],(long)[error code]);
}
return totalFreeSpace;
}
這個函數失掉的值,並非實時數據,因而能夠與實踐以後剩余空間不分歧。
【iOS中文件下載監控及處置的小結】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!