The code below is used to get the list of disks
(void)da_tools
{
DASessionRef session;
session = DASessionCreate(kCFAllocatorDefault);
DARegisterDiskAppearedCallback(session, NULL, disk_appeared_callback, (void *)NULL);
DASessionScheduleWithRunLoop(session, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
CFRunLoopRun();
CFRelease(session);
}
The IBAction for the OK button is: [self da_tools]
;
The callback function disk_appeared_callback works perfectly and give the right information.
The problem happens at CFRunLoopRun() when the OK button is hit:
- the CFRunLoopRun() take many seconds to execute or never end.
- The callback function disk_appeared_callback itself is executed in milli-seconds time.
However any key press to the keyboard (or a mouse click everywhere (and sometime just a mouse move)) force CFRunLoopRun() to exit and the callback function disk_appeared_callback is promptly executed.
When CFRunLoopRun() is removed da_tools
does not return any disk information at the first OK button hit
but subsequent OK button hit returns the correct disk information.
I have tried to include da_tools
in an another thread:
[NSThread detachNewThreadSelector:@selector(da_tools) toTarget:self withObject:nil]
but this doesn't help.
I also tried to post a keydown event but this fails too.
How to use CFRunLoopRun correctly?
Please do not to use call
CFRunLoopRun();
method separately and please not release sessionCFRelease(session);
because you will not received any callback later on.How to established session with DiskArbitration and listen via callbacks?
Creating DA session and schedule session run loop:
If you like to listen for all disk events including internal one. Then Pass
NULL
while registering callbacks.Else create a
matching condition
for your callbacks. For example: following condition for external hard disk, memory disk connects via USB.Look at
DADisk.h
, we add more conditions for your need.Registering Callbacks with DiskArbitration, more info on callbacks here-
... Application things goes around...**
Finally Unregister and release everything. Typical place is application terminate or dealloc.
I hope it helps!
BTW, for DA Approval Session like this
Finally unregister and release.
For more info on above code snippets here.