I am just starting out in this wacky world of programming and I have come across a very frustrating problem:
I am trying to use Disk Arbitration Framework to put all Disks in a array.
#import "DiskDetector.h"
#import "Disk.h"
@implementation DiskDetector
-(id)init {
self.arrayOfDisks = [[NSMutableArray alloc]init];
DASessionRef session;
session = DASessionCreate(kCFAllocatorDefault);
DARegisterDiskAppearedCallback(session, kDADiskDescriptionMatchVolumeMountable,diskAppearedCallback,NULL);
DARegisterDiskDisappearedCallback(session,kDADiskDescriptionMatchVolumeMountable, diskDisappearedCallback, NULL);
DASessionScheduleWithRunLoop(session, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
return self;
}
-(void)addToArrayOfDisks:(Disk*)disk {
}
void diskAppearedCallback(DADiskRef disk, void* context)
{
Disk *theDisk = [[Disk alloc]initWithNewDisk:disk];
NSLog(@"Disk Name: %@",theDisk.diskName);
NSLog(@"Disk Number: %@",theDisk.diskBSDName);
NSLog(@"Disk Connection Type: %@",theDisk.diskConnection);
NSLog(@"Disk Capacity in Bytes: %@",theDisk.diskTotalCapacityInBytes);
NSLog(@"Volume Name: %@",theDisk.partitionScheme);
//How Do I return "theDisk" from this function?
//[self.arrayOfDisks addObject:theDisk] does not work...complier has problem with use of "self"
}
void diskDisappearedCallback(DADiskRef disk, void* context)
{
NSLog(@"%s was ejected", DADiskGetBSDName(disk));
}
@end
As you can see, everything logs okay. The issue is that I want to return "theDisk" object in some way So I can work with it.
Since the callback function is void, I cannot do a return statement. If I attempt to modify the function's return type the DARegisterDiskAppearedCallback Function will not work entirely.
Again, my only goal here is to get information about all disks and partitions on computer and put them in an array so I can get info about them and manipulate them.
Also, can anyone explain when one would put something in "(void*)context" in the callback function? Apple Documentation is very vague on this...or maybe I am missing something entirely
The context
parameter is for your use. You can set whatever you want as the context, and DiskArbitration will pass it to you when it calls the callback. You can use this to solve your first problem by passing your DiskDetector object as the context:
-(id)init {
// ...
DARegisterDiskAppearedCallback(session, kDADiskDescriptionMatchVolumeMountable, diskAppearedCallback, self);
// ...
}
void diskAppearedCallback(DADiskRef disk, void* context) {
DiskDetector *detector = (DiskDetector *)context;
// ...
[detector.arrayOfDisks addObject:theDisk];
}