我正在使用MacRuby中的GUI应用程序,我需要使用FSEvents。我正在为不同的目录注册几个流。任何这些目录的更改都会导致运行回调,但是存在一个很大的问题:无论哪个目录发生更改,都会执行上次注册的回调。
下面是孤立的测试脚本:
framework 'Cocoa'
framework 'CoreServices'
class Monitor
def initialize(dir)
@dir = dir
end
def start(&block)
callback = Proc.new do |stream, context, count, paths, flags, ids|
p @dir
block.call
end
flags = KFSEventStreamCreateFlagUseCFTypes
@stream = FSEventStreamCreate(KCFAllocatorDefault, callback, nil, [@dir], KFSEventStreamEventIdSinceNow, 0.0, flags)
FSEventStreamScheduleWithRunLoop(@stream, CFRunLoopGetCurrent(), KCFRunLoopDefaultMode)
FSEventStreamStart(@stream)
end
end
Monitor.new(Dir.pwd + "/dir1").start { p "dir1" }
Monitor.new(Dir.pwd + "/dir2").start { p "dir2" }
Monitor.new(Dir.pwd + "/dir3").start { p "dir3" }
app = NSApplication.sharedApplication
app.run
当我运行它并开始修改这些目录时:
~/tmp/fsevents $ touch dir1/test
~/tmp/fsevents $ touch dir2/test
~/tmp/fsevents $ touch dir3/test
输出是:
"/Users/janek/tmp/fsevents/dir3"
"dir3"
"/Users/janek/tmp/fsevents/dir3"
"dir3"
"/Users/janek/tmp/fsevents/dir3"
"dir3"
我更期待的是:
"/Users/janek/tmp/fsevents/dir1"
"dir1"
"/Users/janek/tmp/fsevents/dir2"
"dir2"
"/Users/janek/tmp/fsevents/dir3"
"dir3"
也许我可以通过上下文参数提供我需要的数据来解决这个问题(因为在回调中检查paths
会显示实际更改的目录),但是,当前的行为对我来说完全出乎意料。
我正在使用OS X 10.8.2(12C60)和MacRuby 0.12(ruby 1.9.2)[universal-darwin10.0,x86_64]。
答案 0 :(得分:0)
我不是一个红宝石,但这段代码对我有用。
framework 'Cocoa'
framework 'CoreServices'
class Monitor
@@registry = {}
def self.register(dir, other_data)
@@registry[dir] = other_data
end
def initialize(dir, other_data)
@dir = dir
self.class.register(dir, other_data)
callback = Proc.new do |stream, context, count, paths, flags, ids|
paths.cast!('*')
p "the callback that triggered has closure variable @dir=#{@dir}"
p "but the actual callback said the dir was #{paths[0]}"
p "the metadata that I stored associated with that directory is #{@@registry[paths[0]]}"
end
@stream = FSEventStreamCreate(KCFAllocatorDefault, callback, nil, [@dir], KFSEventStreamEventIdSinceNow, 0.0, 0)
FSEventStreamScheduleWithRunLoop(@stream, CFRunLoopGetCurrent(), KCFRunLoopDefaultMode)
FSEventStreamStart(@stream)
end
end
Monitor.new(Dir.pwd + "/dir1/", 'dir1 data')
Monitor.new(Dir.pwd + "/dir2/", 'dir2 data')
Monitor.new(Dir.pwd + "/dir3/", 'dir3 data')
app = NSApplication.sharedApplication
app.run
这是我看到的输出:
rmcgibbo@Roberts-MacBook-Pro-2 ~/local/fsync
$ macruby fsevents.rb &
[1] 14638
rmcgibbo@Roberts-MacBook-Pro-2 ~/local/fsync
$ touch dir1/mao
rmcgibbo@Roberts-MacBook-Pro-2 ~/local/fsync
$ "the callback that triggered has closure variable @dir=/Users/rmcgibbo/local/fsync/dir3/"
"but the actual callback said the dir was /Users/rmcgibbo/local/fsync/dir1/"
"the metadata that I stored associated with that directory is dir1 data"