4

我正在 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]。

4

1 回答 1

0

是的,这真的很奇怪。我也有这种行为。似乎总是调用最新注册的回调。但从好的方面来说,可以从回调的第四个参数获取到实际调用的目录的路径。我不得不使用这样的构造。

我不是一个 ruby​​ist,但这段代码对我有用。

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"
于 2012-11-18T12:46:34.227 回答