Skip to content

Commit 7057740

Browse files
author
Clay Bridges
committed
Fix for indexing forever spinner
Fix for #86.
1 parent 241e39c commit 7057740

1 file changed

Lines changed: 58 additions & 13 deletions

File tree

‎Classes/Util/PBEasyPipe.m‎

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,50 +7,95 @@
77
//
88

99
#import "PBEasyPipe.h"
10+
#import <objc/runtime.h>
1011

1112
NSString *const PBEasyPipeErrorDomain = @"PBEasyPipeErrorDomain";
1213
NSString *const PBEasyPipeUnderlyingExceptionKey = @"PBEasyPipeUnderlyingExceptionKey";
1314

15+
#pragma mark - NSPipe category
16+
17+
/* Some NSTask's generate a lot of output. Using a regular pipe with these tasks causes the pipe to fill up before the
18+
task completes; it will basically fail silently. To fix this, the pipe must occasionally be read into a
19+
cache, but because this can occur at the end of a chain of async operations, it gets complicated. Instead, you can set
20+
the .dataOutput property on this NSPipe category, and the intermittent results automatically will be read into
21+
that NSMutableData.
22+
*/
23+
24+
@interface NSPipe (PBEasyPipe)
25+
26+
@property (nonatomic, strong) NSMutableData *dataOutput;
27+
28+
@end
29+
30+
@implementation NSPipe (PBEasyPipe)
31+
32+
- (void)setDataOutput:(nonnull NSMutableData *)dataOutput {
33+
objc_setAssociatedObject(self, @selector(dataOutput), dataOutput, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
34+
35+
NSFileHandle *fileHandle = self.fileHandleForReading;
36+
NSAssert(fileHandle != nil, @"should have file handle for reading");
37+
38+
fileHandle.readabilityHandler = ^(NSFileHandle * _Nonnull fileHandle) {
39+
[dataOutput appendData:fileHandle.availableData];
40+
};
41+
}
42+
43+
- (NSMutableData *)dataOutput {
44+
return objc_getAssociatedObject(self, @selector(dataOutput));
45+
}
46+
47+
- (void)clear {
48+
self.fileHandleForReading.readabilityHandler = nil;
49+
}
50+
51+
@end
52+
53+
#pragma mark - PBEasyPipe implementation
54+
1455
@implementation PBEasyPipe
1556

1657
+ (nullable NSTask *)performCommand:(NSString *)command arguments:(NSArray *)arguments inDirectory:(NSString *)directory terminationHandler:(void (^)(NSTask *, NSError *error))terminationHandler {
1758
NSParameterAssert(command != nil);
1859

1960
NSTask *task = [self taskForCommand:command arguments:arguments inDirectory:directory];
61+
NSPipe *pipe = task.standardOutput;
62+
pipe.dataOutput = [NSMutableData data];
63+
2064
task.terminationHandler = ^(NSTask *task) {
2165
dispatch_async(dispatch_get_main_queue(), ^{
2266
terminationHandler(task, nil);
2367
});
2468
};
69+
2570
@try {
2671
[task launch];
27-
}
28-
@catch (NSException *exception) {
72+
} @catch (NSException *exception) {
2973
NSString *desc = @"Exception raised while launching task";
3074
NSString *failureReason = [NSString stringWithFormat:@"The task \"%@\" failed to launch", command];
31-
NSDictionary *info = @{
32-
NSLocalizedDescriptionKey: desc,
33-
NSLocalizedFailureReasonErrorKey: failureReason,
34-
PBEasyPipeUnderlyingExceptionKey: exception,
35-
};
36-
NSError *error = [NSError errorWithDomain:PBEasyPipeErrorDomain
37-
code:PBEasyPipeTaskLaunchError
38-
userInfo:info];
75+
NSDictionary *info = @{NSLocalizedDescriptionKey: desc,
76+
NSLocalizedFailureReasonErrorKey: failureReason,
77+
PBEasyPipeUnderlyingExceptionKey: exception};
78+
NSError *error = [NSError errorWithDomain:PBEasyPipeErrorDomain code:PBEasyPipeTaskLaunchError userInfo:info];
3979
terminationHandler(task, error);
40-
return nil;
4180
}
81+
4282
return task;
4383
}
4484

4585
+ (nullable NSTask *)performCommand:(NSString *)command arguments:(NSArray *)arguments inDirectory:(NSString *)directory completionHandler:(void (^)(NSTask *, NSData *readData, NSError *error))completionHandler {
86+
4687
return [self performCommand:command arguments:arguments inDirectory:directory terminationHandler:^(NSTask *task, NSError *error) {
88+
NSPipe *pipe = task.standardOutput;
89+
4790
if (error) {
91+
[pipe clear];
4892
completionHandler(task, nil, error);
4993
return;
5094
}
5195

52-
NSData *data = [[task.standardOutput fileHandleForReading] readDataToEndOfFile];
53-
completionHandler(task, data, nil);
96+
[pipe clear];
97+
98+
completionHandler(task, pipe.dataOutput, nil);
5499
}];
55100
}
56101

0 commit comments

Comments
 (0)