Skip to content

Commit 24f95c5

Browse files
chore: Cache application instances for their PIDs
1 parent a51d160 commit 24f95c5

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

WebDriverAgentLib/Utilities/FBXCAXClientProxy.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ NS_ASSUME_NONNULL_BEGIN
4343
attributes:(NSArray *)attributes
4444
error:(NSError**)error;
4545

46-
- (XCUIApplication *)monitoredApplicationWithProcessIdentifier:(int)pid;
46+
- (nullable XCUIApplication *)monitoredApplicationWithProcessIdentifier:(int)pid;
4747

4848
@end
4949

WebDriverAgentLib/Utilities/FBXCAXClientProxy.m

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,16 @@
1414
#import "FBMacros.h"
1515
#import "XCAXClient_iOS+FBSnapshotReqParams.h"
1616
#import "XCUIDevice.h"
17+
#import "XCUIApplication.h"
1718

1819
static id FBAXClient = nil;
1920

21+
@interface FBXCAXClientProxy ()
22+
23+
@property (nonatomic) NSMutableDictionary<NSNumber *, XCUIApplication *> *appsCache;
24+
25+
@end
26+
2027
@implementation FBXCAXClientProxy
2128

2229
+ (instancetype)sharedClient
@@ -25,6 +32,7 @@ + (instancetype)sharedClient
2532
static dispatch_once_t onceToken;
2633
dispatch_once(&onceToken, ^{
2734
instance = [[self alloc] init];
35+
instance.appsCache = [NSMutableDictionary dictionary];
2836
FBAXClient = [XCUIDevice.sharedDevice accessibilityInterface];
2937
});
3038
return instance;
@@ -85,7 +93,28 @@ - (NSDictionary *)attributesForElement:(id<FBXCAccessibilityElement>)element
8593

8694
- (XCUIApplication *)monitoredApplicationWithProcessIdentifier:(int)pid
8795
{
88-
return [[FBAXClient applicationProcessTracker] monitoredApplicationWithProcessIdentifier:pid];
96+
NSMutableSet *terminatedAppIds = [NSMutableSet set];
97+
for (NSNumber *appPid in self.appsCache) {
98+
if (![self.appsCache[appPid] running]) {
99+
[terminatedAppIds addObject:appPid];
100+
}
101+
}
102+
for (NSNumber *appPid in terminatedAppIds) {
103+
[self.appsCache removeObjectForKey:appPid];
104+
}
105+
106+
XCUIApplication *result = [self.appsCache objectForKey:@(pid)];
107+
if (nil != result) {
108+
return result;
109+
}
110+
111+
XCUIApplication *app = [[FBAXClient applicationProcessTracker]
112+
monitoredApplicationWithProcessIdentifier:pid];
113+
if (nil == app) {
114+
return nil;
115+
}
116+
[self.appsCache setObject:app forKey:@(pid)];
117+
return app;
89118
}
90119

91120
@end

0 commit comments

Comments
 (0)