首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用DiskArbitration卸载OSX中的磁盘

用DiskArbitration卸载OSX中的磁盘
EN

Stack Overflow用户
提问于 2015-12-31 22:39:21
回答 1查看 1.2K关注 0票数 4

我正试图卸载OSX中的磁盘。代码工作正常,但只有当磁盘卸载成功时,才会出现错误时才调用回调。我读了DiskArbitrationProgGuide并遵循了这些步骤,但还没有进展。有人能帮我吗?

代码语言:javascript
复制
@interface DriverUtilitiesController()

void unmount_done(DADiskRef disk,
                  DADissenterRef dissenter,
                  void *context);

@end

+ (void)umnountDrivePath:(NSString *)voulumePath
{
    DASessionRef session = DASessionCreate(kCFAllocatorDefault);

    CFURLRef path = CFURLCreateWithString(NULL, (__bridge CFStringRef)voulumePath, NULL);

    DADiskRef disk = DADiskCreateFromVolumePath(kCFAllocatorDefault, session, path);

    DADiskUnmount(disk, kDADiskUnmountOptionDefault, unmount_done, NULL);

    CFRelease(disk);
}

#pragma mark - Unmount Callback

void unmount_done(DADiskRef disk,
                  DADissenterRef dissenter,
                  void *context)
{

    NSLog(@"Inside unmount_done");

    if (dissenter)
    {
        // Unmount failed. //
        NSLog(@"Unmount failed.");

    } else {
        NSLog(@"Unmounted Volume");
    }
}

更新.由于Ken的帮助,代码现在可以工作了

代码语言:javascript
复制
- (id)init
{
    self = [super init];

    self.session = DASessionCreate(kCFAllocatorDefault);

    DASessionScheduleWithRunLoop(_session, [[NSRunLoop mainRunLoop] getCFRunLoop], kCFRunLoopDefaultMode);

    return self;
}


- (void)umnountDrivePath:(NSString *)voulumePath
{

    CFURLRef path = CFURLCreateWithString(NULL, (__bridge CFStringRef)voulumePath, NULL);

    DADiskRef disk = DADiskCreateFromVolumePath(kCFAllocatorDefault, self.session, path);

    DADiskUnmount(disk, kDADiskUnmountOptionDefault, unmount_done, (__bridge void *)(self));

    CFRelease(disk);
}

void unmount_done(DADiskRef disk,
                  DADissenterRef dissenter,
                  void *context)
{
    if (dissenter)
    {
        // Unmount failed. //
        NSLog(@"Unmount failed.");

    } else {
        NSLog(@"Unmounted Volume");

    }

    DriverUtilitiesController *driverUtilitiesController = (__bridge DriverUtilitiesController *)context;


    [driverUtilitiesController clearUnmountCallback];
}

- (void)clearUnmountCallback
{
    DASessionUnscheduleFromRunLoop(_session, [[NSRunLoop mainRunLoop] getCFRunLoop], kCFRunLoopDefaultMode);

    CFRelease(self.session);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-01 05:09:16

DADiskUnmount()异步操作。当函数返回到您的代码时,磁盘不一定已被卸载。如果它成功了,它可能会在以后的某个时候发生。到时我们会打电话给你的。

程序等待该事件并响应调用回调的机制要么是运行循环,要么是调度队列。会话对象负责管理此等待和调用。您需要将会话对象安排在运行循环或调度队列上。您可以使用DASessionScheduleWithRunLoop()DASessionSetDispatchQueue(),如磁盘仲裁编程指南:使用磁盘仲裁通知和批准回调-调度运行循环或调度队列的会话中所述。

这意味着您不希望为每次卸载磁盘的尝试创建一个新的session对象。此外,您希望保持对session对象的引用,以便在不再需要它时(在您不再需要从它获得回调之后)取消调度并释放它。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34552164

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档