Double callback from Disk Arbitration when mounting disk image
I have a problem using DiskArbitration framework, to catch disk image mounting I register for DARegisterDiskMountApprovalC开发者_运维百科allback
. The problem is that each time a disk image is mounted, the callback is called twice. Why is that and how can I solve this?
I ended up coding something to detect the 2nd mount and ignore it.
When a disk is mounted you often see an event for the whole disk and then events for distinct partitions on that disk. You'll need to distinguish.
static void got_disk(DADiskRef disk, void *context)
{
CFDictionaryRef dict = DADiskCopyDescription(disk);
NSNumber *whole = CFDictionaryGetValue(dict, kDADiskDescriptionMediaWholeKey);
if (![whole boolValue]) {
// Handle your event only with the partition, not the "whole" disk
...
}
}
It's very handy to put a CFShow(dict)
in your event handler and see what you get.
Did you put a breakpoint in your callback to see what are the call-stack when it is called ? It can gives you some hints on what is going on.
I use these the catch that. I'm not sure of the difference these are to what you're doing but they work.
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(mediaMounted:) name:NSWorkspaceDidMountNotification object:[NSWorkspace sharedWorkspace]];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(mediaUnmounted:) name:NSWorkspaceWillUnmountNotification object:[NSWorkspace sharedWorkspace]];
精彩评论