PFFileController
文件控制器,上传,下载,缓存文件控制。
// 串行队列, 下载,文件访问
_downloadDataAccessQueue = dispatch_queue_create("com.parse.fileController.download", DISPATCH_QUEUE_SERIAL);
_fileStagingControllerAccessQueue = dispatch_queue_create("com.parse.filestaging.controller.access", DISPATCH_QUEUE_SERIAL);
// 串行访问
- (PFFileStagingController *)fileStagingController {
__block PFFileStagingController *result = nil;
dispatch_sync(_fileStagingControllerAccessQueue, ^{
if (!self->_fileStagingController) {
self->_fileStagingController = [PFFileStagingController controllerWithDataSource:self.dataSource];
}
result = self->_fileStagingController;
});
return result;
}
// async to UI线程, other sync excute block
- (PFProgressBlock)_fileDownloadUnifyingProgressBlockForFileState:(PFFileState *)fileState {
return ^(int progress) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
__block NSArray *blocks = nil;
dispatch_sync(self->_downloadDataAccessQueue, ^{
blocks = [self->_downloadProgressBlocks[fileState.secureURLString] copy];
});
if (blocks.count != 0) {
dispatch_async(dispatch_get_main_queue(), ^{
for (PFProgressBlock block in blocks) {
block(progress);
}
});
}
});
};
}
// dispatch_barrier_async 与 dispatch_async 是一样的, 串行线程
- (void)_addFileDownloadProgressBlock:(PFProgressBlock)block forFileWithState:(PFFileState *)state {
if (!block) {
return;
}
dispatch_barrier_async(self->_downloadDataAccessQueue, ^{
NSMutableArray *progressBlocks = self->_downloadProgressBlocks[state.secureURLString];
if (!progressBlocks) {
progressBlocks = [NSMutableArray arrayWithObject:block];
self->_downloadProgressBlocks[state.secureURLString] = progressBlocks;
} else {
[progressBlocks addObject:block];
}
});
}
同步队列,只是线程安全使用。
PFConfigController
配置管理
// 串行队列
_dataAccessQueue = dispatch_queue_create("com.parse.config.access", DISPATCH_QUEUE_SERIAL);
_networkQueue = dispatch_queue_create("com.parse.config.network", DISPATCH_QUEUE_SERIAL);
// 同步 write
- (PFCurrentConfigController *)currentConfigController {
__block PFCurrentConfigController *controller = nil;
dispatch_sync(_dataAccessQueue, ^{
if (!self->_currentConfigController) {
self->_currentConfigController = [[PFCurrentConfigController alloc] initWithDataSource:self.dataSource];
}
controller = self->_currentConfigController;
});
return controller;
}
总结:串行队列,安全读写,设定 __block 来设置变量。
PFApplication
只是保证在主线程执行,有线程切换尽量选择 async
- (void)setIconBadgeNumber:(NSInteger)iconBadgeNumber {
if (self.iconBadgeNumber != iconBadgeNumber) {
#if TARGET_OS_IOS || TARGET_OS_TV
_iconBadgeNumber = iconBadgeNumber;
dispatch_block_t block = ^{
if (@available(iOS 1.0, tvOS 10.0, *)) {
self.systemApplication.applicationIconBadgeNumber = iconBadgeNumber;
}
};
if ([NSThread currentThread].isMainThread) {
block();
} else {
dispatch_async(dispatch_get_main_queue(), block);
}
#elif PF_TARGET_OS_OSX
[[NSApplication sharedApplication] dockTile].badgeLabel = [@(iconBadgeNumber) stringValue];
#endif
}
}
PFKeychainStore
串行线程,保证安全
PFCurrentInstallationController
// 并行队列
_dataQueue = dispatch_queue_create("com.parse.installation.current", DISPATCH_QUEUE_CONCURRENT);
// dispatch_barrier_sync 同步 读唯一
- (BFTask *)clearCurrentInstallationAsync {
@weakify(self);
return [_dataTaskQueue enqueue:^BFTask *(BFTask *unused) {
@strongify(self);
dispatch_barrier_sync(self->_dataQueue, ^{
self->_currentInstallation = nil;
self->_currentInstallationMatchesDisk = NO;
});
...
return [BFTask taskForCompletionOfAllTasks:tasks];
}];
}
- (void)setCurrentInstallation:(PFInstallation *)currentInstallation {
dispatch_barrier_sync(_dataQueue, ^{
if (self->_currentInstallation != currentInstallation) {
self->_currentInstallation = currentInstallation;
}
});
}
// 同步都并行队列执行
- (PFInstallation *)currentInstallation {
__block PFInstallation *installation = nil;
dispatch_sync(_dataQueue, ^{
installation = self->_currentInstallation;
});
return installation;
}
网友评论