FSEventStreamCreate监视文件系统改变通知

相关的api:

1:应用程序通过FSEventStreamCreate or FSEventStreamCreateRelativeToDevice注册。

2:使用循环来获取FSEventStreamScheduleWithRunLoop

3:告诉后台进程开始文件监控FSEventStreamStart

4:如果事件产生,然后就回调函数FSEventStreamCallback

5:停止后台进程发送通知FSEventStreamStop.

6:作废事件 FSEventStreamInvalidate.

7:释放事件FSEventStreamRelease.


代码例子:

int main(int argc, const char * argv[])

{

    CFStringRef path = CFSTR("/USERS/username/downloads");

    CFArrayRef  paths= CFArrayCreate(NULL, (const void **)&path, 1, NULL);

    FSEventStreamRef stream = FSEventStreamCreate(kCFAllocatorDefault,

                                                  &eventStreamCallback,

                                                  NULL,

                                                  paths,

                                                  kFSEventStreamEventIdSinceNow,

                                                  1.0, // 事件发生后延迟多少秒调用回调,如果时间设长则有更高的效率,会一次性通知多个事件

                                                  kFSEventStreamCreateFlagFileEvents) ;

    

    FSEventStreamScheduleWithRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);

    FSEventStreamStart(stream);

    CFRunLoopRun();    

    return 0;

}


void eventStreamCallback(

                ConstFSEventStreamRef streamRef,

                void *clientCallBackInfo,

                size_t numEvents,

                void *eventPaths,

                const FSEventStreamEventFlags eventFlags[],

                const FSEventStreamEventId eventIds[])

{

    int i;


    /* eventPaths

    An array of paths to the directories in which event(s) occurred.

    The type of this parameter depends on the flags passed to FSEventStreamCreate...(). If kFSEventStreamCreateFlagUseCFTypes was set, then this will be a CFArrayRef containing CFStringRef objects (per CFStringCreateWithFileSystemRepresentation()). Ownership follows the Get rule, and they will be released by the framework after your callback returns. If kFSEventStreamCreateFlagUseCFTypes was not set, then the framework will pass your callback a raw C array of raw C strings that will be deallocated by the framework after your callback returns.

    

    A path might be '/' if ether of these flags is set for the event: kFSEventStreamEventFlagUserDropped, kFSEventStreamEventFlagKernelDropped.

     */

    char **paths = eventPaths;

    

    for (i=0; i<numEvents; i++)

    {

        NSLog(@"path=%s", paths[i]);

        

        if ((eventFlags[i] &kFSEventStreamEventFlagRootChanged) !=0)

        {

            //do something you want

        }

    }

}



函数文档

FSEventStreamCreate

extern FSEventStreamRef FSEventStreamCreate(

   CFAllocatorRef allocator,
   FSEventStreamCallback callback,
   FSEventStreamContext *context,
   CFArrayRef pathsToWatch,
   FSEventStreamEventId sinceWhen,
   CFTimeInterval latency,
   FSEventStreamCreateFlags flags);
Parameters
allocator

The CFAllocator to be used to allocate memory for the stream. Pass NULL or kCFAllocatorDefault to use the current default allocator.

callback

An FSEventStreamCallback which will be called when FS events occur.

context

A pointer to the FSEventStreamContext structure the client wants to associate with this stream. Its fields are copied out into the stream itself so its memory can be released after the stream is created. Passing NULL is allowed and has the same effect as passing a structure whose fields are all set to zero.

pathsToWatch

A CFArray of CFStringRefs, each specifying a path to a directory, signifying the root of a filesystem hierarchy to be watched for modifications.

sinceWhen

The service will supply events that have happened after the given event ID. To ask for events "since now" pass the constant kFSEventStreamEventIdSinceNow. Often, clients will supply the highest-numbered FSEventStreamEventId they have received in a callback, which they can obtain via the FSEventStreamGetLatestEventId() accessor. Do not pass zero for sinceWhen, unless you want to receive events for every directory modified since "the beginning of time" -- an unlikely scenario.

latency

The number of seconds the service should wait after hearing about an event from the kernel before passing it along to the client via its callback. Specifying a larger value may result in more effective temporal coalescing, resulting in fewer callbacks and greater overall efficiency.

flags

Flags that modify the behavior of the stream being created. See FSEventStreamCreateFlags.

Return Value

A valid FSEventStreamRef.


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值