// 当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文件夹内文件发生改变的更多相关文章

  1. python对文件夹内文件去重

    昨天无聊写了一个百度图片爬虫,测试了一下搜索"斗图".一下给我下了3000多个图片,关键是有一半以上重复的.what a fuck program ! 好吧,今天写一个文件去重功能 ...

  2. 个人永久性免费-Excel催化剂功能第83波-遍历文件夹内文件信息特别是图像、音视频等特有信息

    在过往的功能中,有体现出在Excel上管理文件的极大优势,在文件的信息元数据中,有图片和音视频这两类特有的属性数据,此篇对过往功能的一个补充,特别增加了图片和音视频信息的遍历功能. 使用场景 在文件管 ...

  3. C#获取文件夹内文件包括子文件(递归)实例

    这个在我工作上,写了一次工具项目,用上的. static ArrayList FileList = new ArrayList();//这个必须写在方法外, static ArrayList GetA ...

  4. Linux得到某个文件夹内文件的个数

    panonum=$(find . -type f | wc -l)

  5. Python win32com模块 合并文件夹内多个docx文件为一个docx

    Python win32com模块 合并文件夹内多个docx文件为一个docx #!/usr/bin/env python # -*- coding: utf-8 -*- from win32com. ...

  6. ftp下载目录下所有文件及文件夹内(递归)

    ftp下载目录下所有文件及文件夹内(递归)   /// <summary> /// ftp文件上传.下载操作类 /// </summary> public class FTPH ...

  7. Linux如何修改文件/文件夹内所有文件的权限

    一.修改文件权限 修改文件权限前,需要了解一下权限中的”rwx”与数字的对应关系,其中r=4,w=2,x=1. 例如:”drwxr-xr-x”,第一个”d”是代表文件夹,这里不用考虑,后面九个字符,每 ...

  8. Linux批量替换文本,文件夹内所有文本内容

    1.替换文件夹内所有文件匹配的字符串 sed -i "s/旧内容/新内容/g" `grep 旧内容 -rl 文件夹路径` 例如将/var/www/test文件夹下的所有文件内容中的 ...

  9. 用Python来实现列举某个文件夹内所有的文件列表

    用Python来实现列举某个文件夹内所有的文件列表.吾八哥我动手写代码之前分析了下,遍历一个文件夹,肯定是需要用到os模块了,查阅模块帮助信息,可知os.listdir()方法可以列举某个文件夹内的所 ...

随机推荐

  1. 【Head First Servlets and JSP】笔记1

    1.把Java放到HTML中,JSP应运而生. 2.Servlet本身并没有main()方法,所以必须要有其他Java程序去调用它,这个Java程序就是Web容器(Container).Tomcat就 ...

  2. PHP memcache扩展模块安装

    安装php扩展模块memcache memcache 的工作就是在专门的机器的内存里维护一张巨大的hash表,来存储经常被读写的一些数组与文件,从而极大的提高网站的运行效率,减轻后端数据库的读写压力. ...

  3. 基于Bootstrap的日历控件和日期选择插件

    在线演示 本地下载

  4. 华为S5700系列交换机AR配置静态IP双链路负载分担

    适用于:有多个以太WAN口的机型. 业务需求: 运营商1分配的接口IP为100.100.1.2,子网掩码为255.255.255.252,网关IP为100.100.1.1. 运营商2分配的接口IP为2 ...

  5. Android震动vibrator(马达)--系统到驱动的流程【转】

    本文转载自:https://blog.csdn.net/tianshiyalin/article/details/17136723 一.前言 本人刚学习安卓驱动开发,水平不能说菜,是根本没有水平,在这 ...

  6. Java 获取路径的几种方法 - 转载

    1.获取当前类所在的“项目名路径” String rootPath = System.getProperty("user.dir"); 2.获取编译文件“jar包路径”(反射) S ...

  7. 有意思的随机数 Random

    今天看到stackoverflow上一篇关于随机数的提问,关于如何用随机数生成 "hello world"的有趣问题 : 大家都知道JDK里面的Random类生成随机数其实并不是真 ...

  8. HashMap和ConcurrentHashMap和HashTable的底层原理与剖析

    HashMap  可以允许key为null,value为null,但HashMap的是线程不安全的  HashMap 底层是数组 + 链表的数据结构 在jdk 1.7 中 map集合中的每一项都是一个 ...

  9. angular 图片懒加载(延迟加载)

    github 原文 https://github.com/Treri/me-lazyload me-lazyload angular 的图像资源延迟加载指令 例子(Demo) 演示网站(Demo Si ...

  10. Object有哪些方法?

    有9个方法 1 clone 2 toString() 3 getClass 4 finalize 5 equals 6 hascode 7 notify 8 notifall 9 wait