1414#import " FBMacros.h"
1515#import " XCAXClient_iOS+FBSnapshotReqParams.h"
1616#import " XCUIDevice.h"
17+ #import " XCUIApplication.h"
1718
1819static 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