Skip to content

Instantly share code, notes, and snippets.

@deszip
Created July 9, 2019 13:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deszip/a0cf877b07cc2877129e0aaef2fed1e4 to your computer and use it in GitHub Desktop.
Save deszip/a0cf877b07cc2877129e0aaef2fed1e4 to your computer and use it in GitHub Desktop.
Building MXMetricPayload
// Build payload
MXMetricPayload *payload = [MXMetricPayload alloc];
SEL selector = @selector(initWithAppVersion:withMutipleAppVersions:withTimeStampBegin:withTimeStampEnd:);
NSMethodSignature *signature = [MXMetricPayload instanceMethodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setTarget:payload];
[invocation setSelector:selector];
NSString *appVersion = @"10";
NSArray *appVersions = @[appVersion];
NSDate *startDate = [NSDate date];
NSDate *endDate = [NSDate date];
[invocation setArgument:&appVersion atIndex:2];
[invocation setArgument:&appVersions atIndex:3];
[invocation setArgument:&startDate atIndex:4];
[invocation setArgument:&endDate atIndex:5];
[invocation invoke];
// Add metrics
MXCPUMetric *cpuMetric = [MXCPUMetric alloc];
[cpuMetric performSelector:@selector(initWithCumulativeCPUTimeMeasurement:) withObject:[NSNumber numberWithDouble:42]];
[payload performSelector:@selector(setCpuMetrics:) withObject:cpuMetric];
// Put payload to caches
NSString *path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0];
path = [path stringByAppendingPathComponent:@"Caches/MetricKit/Reports"];
NSError *error = nil;
if(![[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error]) {
NSLog(@"Failed to create directory \"%@\". Error: %@", path, error);
return;
}
NSString *filePath = [path stringByAppendingPathComponent:@"report"];
[NSKeyedArchiver archiveRootObject:payload toFile:filePath];
@karishmawynk
Copy link

Please tell how to integrate this file in any other framework?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment