1. // 当Documents内文件发生改变时,启动计时器,每秒计算一次大小,当大小不发生改变时说明传输完毕,就开始刷新。
  2. @property (nonatomic, strong) NSTimer *timer;
  3. // 原Documents内文件大小
  4. @property (nonatomic, assign) NSInteger filesSize;
  5. // Documents内文件改变后的大小
  6. @property (nonatomic, assign) NSInteger foundSize;
  1. - (NSTimer *)timer {
  2. if (!_timer) {
  3. _timer = [[NSTimer alloc] init];
  4. [_timer setFireDate:[NSDate distantFuture]];
  5. }
  6. return _timer;
  7. }
  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3. self.view.backgroundColor = [UIColor whiteColor];
  4.  
  5. _fileManager = [NSFileManager defaultManager];
  6. self.timer = [NSTimer scheduledTimerWithTimeInterval:.f target:self selector:@selector(compareSize) userInfo:nil repeats:YES];
  7. [self.timer setFireDate:[NSDate distantFuture]];
  8.  
  9. //** 监听Documents文件夹内的文件是否变化
  10. int const folderdescriptor = open([kDocumentPath fileSystemRepresentation], O_EVTONLY);
  11. dispatch_source_t _directorySource = dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE, folderdescriptor, DISPATCH_VNODE_WRITE, DISPATCH_TARGET_QUEUE_DEFAULT);
  12. dispatch_source_set_event_handler(_directorySource, ^{
  13. unsigned long const data = dispatch_source_get_data(_directorySource);
  14. if (data & DISPATCH_VNODE_WRITE) {
  15. // Do all the work on the main thread,
  16. // including timer scheduling, notifications delivering
  17. dispatch_async(dispatch_get_main_queue(), ^{
  18. [self directoryDidChanger];
  19. });
  20. }
  21. });
  22.  
  23. dispatch_source_set_cancel_handler(_directorySource, ^{
  24. close(folderdescriptor);
  25. });
  26.  
  27. dispatch_resume(_directorySource);
  28. }
  1. // 当Documengs文件夹内文件发送改变时
  2. - (void)directoryDidChanger {
  3.  
  4. _filesSize = [self getSizeOfFilePath:kDocumentPath];
  5.  
  6. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  7. [self.timer setFireDate:[NSDate date]];
  8. });
  9.  
  10. }
  11.  
  12. - (void)dealloc {
  13. // 移除计时器
  14. [self.timer invalidate];
  15. }
  16. // 获取所有文件
  17. - (NSArray *)directoryFiles {
  18. NSArray *files = [_fileManager subpathsAtPath:kDocumentPath];
  19. return files;
  20. }
  21. // 比较文件大小,以此监听是否还在传输文件
  22. - (void)compareSize {
  23. _foundSize = [self getSizeOfFilePath:kDocumentPath];
  24.  
  25. if (_foundSize == _filesSize) { // 如果大小没有再发生改变则刷新数据
  26. [self.timer setFireDate:[NSDate distantFuture]];
  27. [self selectDate]; // 开始刷新数据
  28. }
  29.  
  30. _filesSize = _foundSize;
  31. }

监听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. 项目中使用better-scroll实现移动端滚动,报错:Cannot read property 'children' of undefined better-scroll

    就是外面的盒子和要滚动的元素之间要有一层div, 插件挂载的元素是menuWrapper,可以滚动的元素是ul,在这两个元素之间加一个div元素即可解决问题.

  2. 转换数据库连接池为hikaricp

      hikaricp号称是最快的,今天转换过来试试 1.修改配置文件 <!-- Hikari Datasource --> <bean id="dataSource&quo ...

  3. Maven的简介

    1 What? 1.1 Maven 简介Maven 是 Apache 软件基金会组织维护的一款自动化构建工具,专注服务于 Java 平台的项目构建和依赖管理.Maven 这个单词的本意是:专家,内行. ...

  4. CentOS7/6 关闭防火墙

    CentOS6关闭防火墙使用以下命令, //临时关闭 service iptables stop //禁止开机启动 chkconfig iptables off CentOS7中若使用同样的命令会报错 ...

  5. SpringBoot2.0整合Sharding-Jdbc

    maven: <parent> <groupId>org.springframework.boot</groupId> <artifactId>spri ...

  6. 【bzoj5452】[Hnoi2016]大数(莫队)

    题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=4542 首先若p=2,5则这题就是道傻逼题,前缀和搞一下没了.如果p为其他质数,那么可以 ...

  7. idea调节字体大小

    这是调节前的 这是调节后的 步骤

  8. 一个问题:C#引用类型传参,说出你的答案

    namespace ConsoleApplication1 { class Program { static void Main(string[] args) { People p = new Peo ...

  9. JavaWeb -- Struts1 多文件上传与下载 DownloadAction, DispatchAction

    1. 多文件上传与下载 上传下载jsp: <%@ page language="java" import="java.util.*" pageEncodi ...

  10. 用Java编程计算出所有的"水仙花数"

    题目:打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身.例如:153是一个 "水仙花数 ",因 ...