监听Documents文件夹内文件发生改变
- // 当Documents内文件发生改变时,启动计时器,每秒计算一次大小,当大小不发生改变时说明传输完毕,就开始刷新。
- @property (nonatomic, strong) NSTimer *timer;
- // 原Documents内文件大小
- @property (nonatomic, assign) NSInteger filesSize;
- // Documents内文件改变后的大小
- @property (nonatomic, assign) NSInteger foundSize;
- - (NSTimer *)timer {
- if (!_timer) {
- _timer = [[NSTimer alloc] init];
- [_timer setFireDate:[NSDate distantFuture]];
- }
- return _timer;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.view.backgroundColor = [UIColor whiteColor];
- _fileManager = [NSFileManager defaultManager];
- self.timer = [NSTimer scheduledTimerWithTimeInterval:.f target:self selector:@selector(compareSize) userInfo:nil repeats:YES];
- [self.timer setFireDate:[NSDate distantFuture]];
- //** 监听Documents文件夹内的文件是否变化
- int const folderdescriptor = open([kDocumentPath fileSystemRepresentation], O_EVTONLY);
- dispatch_source_t _directorySource = dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE, folderdescriptor, DISPATCH_VNODE_WRITE, DISPATCH_TARGET_QUEUE_DEFAULT);
- dispatch_source_set_event_handler(_directorySource, ^{
- unsigned long const data = dispatch_source_get_data(_directorySource);
- if (data & DISPATCH_VNODE_WRITE) {
- // Do all the work on the main thread,
- // including timer scheduling, notifications delivering
- dispatch_async(dispatch_get_main_queue(), ^{
- [self directoryDidChanger];
- });
- }
- });
- dispatch_source_set_cancel_handler(_directorySource, ^{
- close(folderdescriptor);
- });
- dispatch_resume(_directorySource);
- }
- // 当Documengs文件夹内文件发送改变时
- - (void)directoryDidChanger {
- _filesSize = [self getSizeOfFilePath:kDocumentPath];
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- [self.timer setFireDate:[NSDate date]];
- });
- }
- - (void)dealloc {
- // 移除计时器
- [self.timer invalidate];
- }
- // 获取所有文件
- - (NSArray *)directoryFiles {
- NSArray *files = [_fileManager subpathsAtPath:kDocumentPath];
- return files;
- }
- // 比较文件大小,以此监听是否还在传输文件
- - (void)compareSize {
- _foundSize = [self getSizeOfFilePath:kDocumentPath];
- if (_foundSize == _filesSize) { // 如果大小没有再发生改变则刷新数据
- [self.timer setFireDate:[NSDate distantFuture]];
- [self selectDate]; // 开始刷新数据
- }
- _filesSize = _foundSize;
- }
监听Documents文件夹内文件发生改变的更多相关文章
- python对文件夹内文件去重
昨天无聊写了一个百度图片爬虫,测试了一下搜索"斗图".一下给我下了3000多个图片,关键是有一半以上重复的.what a fuck program ! 好吧,今天写一个文件去重功能 ...
- 个人永久性免费-Excel催化剂功能第83波-遍历文件夹内文件信息特别是图像、音视频等特有信息
在过往的功能中,有体现出在Excel上管理文件的极大优势,在文件的信息元数据中,有图片和音视频这两类特有的属性数据,此篇对过往功能的一个补充,特别增加了图片和音视频信息的遍历功能. 使用场景 在文件管 ...
- C#获取文件夹内文件包括子文件(递归)实例
这个在我工作上,写了一次工具项目,用上的. static ArrayList FileList = new ArrayList();//这个必须写在方法外, static ArrayList GetA ...
- Linux得到某个文件夹内文件的个数
panonum=$(find . -type f | wc -l)
- Python win32com模块 合并文件夹内多个docx文件为一个docx
Python win32com模块 合并文件夹内多个docx文件为一个docx #!/usr/bin/env python # -*- coding: utf-8 -*- from win32com. ...
- ftp下载目录下所有文件及文件夹内(递归)
ftp下载目录下所有文件及文件夹内(递归) /// <summary> /// ftp文件上传.下载操作类 /// </summary> public class FTPH ...
- Linux如何修改文件/文件夹内所有文件的权限
一.修改文件权限 修改文件权限前,需要了解一下权限中的”rwx”与数字的对应关系,其中r=4,w=2,x=1. 例如:”drwxr-xr-x”,第一个”d”是代表文件夹,这里不用考虑,后面九个字符,每 ...
- Linux批量替换文本,文件夹内所有文本内容
1.替换文件夹内所有文件匹配的字符串 sed -i "s/旧内容/新内容/g" `grep 旧内容 -rl 文件夹路径` 例如将/var/www/test文件夹下的所有文件内容中的 ...
- 用Python来实现列举某个文件夹内所有的文件列表
用Python来实现列举某个文件夹内所有的文件列表.吾八哥我动手写代码之前分析了下,遍历一个文件夹,肯定是需要用到os模块了,查阅模块帮助信息,可知os.listdir()方法可以列举某个文件夹内的所 ...
随机推荐
- 项目中使用better-scroll实现移动端滚动,报错:Cannot read property 'children' of undefined better-scroll
就是外面的盒子和要滚动的元素之间要有一层div, 插件挂载的元素是menuWrapper,可以滚动的元素是ul,在这两个元素之间加一个div元素即可解决问题.
- 转换数据库连接池为hikaricp
hikaricp号称是最快的,今天转换过来试试 1.修改配置文件 <!-- Hikari Datasource --> <bean id="dataSource&quo ...
- Maven的简介
1 What? 1.1 Maven 简介Maven 是 Apache 软件基金会组织维护的一款自动化构建工具,专注服务于 Java 平台的项目构建和依赖管理.Maven 这个单词的本意是:专家,内行. ...
- CentOS7/6 关闭防火墙
CentOS6关闭防火墙使用以下命令, //临时关闭 service iptables stop //禁止开机启动 chkconfig iptables off CentOS7中若使用同样的命令会报错 ...
- SpringBoot2.0整合Sharding-Jdbc
maven: <parent> <groupId>org.springframework.boot</groupId> <artifactId>spri ...
- 【bzoj5452】[Hnoi2016]大数(莫队)
题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=4542 首先若p=2,5则这题就是道傻逼题,前缀和搞一下没了.如果p为其他质数,那么可以 ...
- idea调节字体大小
这是调节前的 这是调节后的 步骤
- 一个问题:C#引用类型传参,说出你的答案
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { People p = new Peo ...
- JavaWeb -- Struts1 多文件上传与下载 DownloadAction, DispatchAction
1. 多文件上传与下载 上传下载jsp: <%@ page language="java" import="java.util.*" pageEncodi ...
- 用Java编程计算出所有的"水仙花数"
题目:打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身.例如:153是一个 "水仙花数 ",因 ...