简体   繁体   English

在 FSEvent 回调中访问 app delegate function

[英]Access app delegate function in FSEvent callback

I have created this function in app delegate file.我在应用程序委托文件中创建了这个 function。 I want to call a function of app delegate in callback.我想在回调中调用 function 的应用程序委托。

Is there any way please suggest.请问有什么办法吗。

-(void) monitor{
    FSEventStreamRef stream = FSEventStreamCreate(NULL, 
                                                  &feCallback,
                                                  &cntxt, 
                                                  pathsToWatch, 
                                                  kFSEventStreamEventIdSinceNow, 
                                                  1,
                                                  kFSEventStreamCreateFlagWatchRoot );
} 

static void feCallback(ConstFSEventStreamRef streamRef,
                       void* pClientCallBackInfo,
                       size_t numEvents,
                       void* pEventPaths,
                       const FSEventStreamEventFlags eventFlags[],
                       const FSEventStreamEventId eventIds[]) 
{
    NSLog(@"The file changed!"); 
    // need to to call app delegate function
}

As FSEventStreamCreate is only available on macOS, you may do it the following way:由于 FSEventStreamCreate 仅在 macOS 上可用,您可以通过以下方式进行:

    // call this in init of your app delegate
    FSEventStreamContext  cntxt = {0, (__bridge void *)(self), NULL, NULL, NULL};
    // call FSEventStreamCreate as in your code
    // keep a reference to the stream so you can stop and start it later

Then in your callback the AppDelegate will be in:然后在您的回调中,AppDelegate 将位于:

    YourAppDelegateClass* appDele = (__bridge YourAppDelegateClass*)pClientCallBackInfo; 

The whole mechanism for FSEvents is quite complex, we had to schedule it on the right run loop and work with ARC and type casting. FSEvents 的整个机制非常复杂,我们必须在正确的运行循环中安排它并使用 ARC 和类型转换。 The callback is outside of ObjectiveC, you won't be able to use Cocoa but only CFString and other Core Foundation types.回调在 ObjectiveC 之外,您将无法使用 Cocoa,而只能使用 CFString 和其他 Core Foundation 类型。 It's also helpful to not only read the documentation but look at the FSEvents.h within the Framework (context menu on any of the functions and then Jump to definition: There is more to read than in the documentation).不仅阅读文档而且查看框架中的 FSEvents.h 也很有帮助(任何函数的上下文菜单,然后跳转到定义:要阅读的内容比文档中的内容更多)。 And depending on your distribution, also work with the App Store Sandbox and security scoped bookmarks.根据您的发行版,还可以使用 App Store 沙盒和安全范围内的书签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM