7

i find that there are some libraries to monitor file changes on mac,for example:https://github.com/bdkjones/VDKQueue but i failed to find a library to monitor file changes on ios platform.

could anybody tell me how to monitor file changes in objc wrapper.

thanks

TinyMonk
  • 204
  • 2
  • 6
  • What are you trying to monitor and why? Possible duplicate: http://stackoverflow.com/questions/19632106/file-system-watching-in-ios – Wain Feb 17 '14 at 11:33
  • Wain,because i want to get a notification when same data are written in a file. i ever see the link you attached, but it seems that link is useful for dictionary watch. – TinyMonk Feb 17 '14 at 17:03
  • possible duplicate of [Monitoring a directory in Cocoa/Cocoa Touch](http://stackoverflow.com/questions/7720246/monitoring-a-directory-in-cocoa-cocoa-touch) – eonil Nov 12 '14 at 02:42

2 Answers2

9

File monitoring on iOS is achieved by creating what GCD calls a "dispatch source" for whatever file or folder you want to monitor. When creating a dispatch source, you provide three interesting things:

  1. A file descriptor that points to the file or folder
  2. Flags to describe what kind of events you want to be notified about (file was modified, file was written to, etc.)
  3. The queue on which to send these event notifications (the main queue, a background queue, etc.)

After creating a dispatch source, you then set blocks of code to be executed when an event occurs or when the source is canceled (destroyed). In the block you set for when an event occurs, you can determine which event occurred (if you registered for more than one type), and proceed accordingly with if...else...then or switch...case statements.

I'm in the process of creating a portable Objective-C class that will simplify the process, but in the mean time, you should take a look at a demo project I've put on GitHub. It shows how exactly to do what I've described.

EDIT:

iMonitorMyFiles is now available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'iMonitorMyFiles', '~> 0.1.0'
T Blank
  • 1,408
  • 1
  • 16
  • 21
2

Have a look into `dispatch_sources' - this is a GCD based way of monitoring for example the file system: https://developer.apple.com/library/ios/documentation/General/Conceptual/ConcurrencyProgrammingGuide/GCDWorkQueues/GCDWorkQueues.html

I am using this instead of kqueue on Mac OS and it should be available on iOS as well, according to the linked documentation.

Volker
  • 4,640
  • 1
  • 23
  • 31