Skip to content

Commit 668df0b

Browse files
authored
Proper shutdown mechanism (MobileNativeFoundation#559)
* safely shutdown Signed-off-by: Nan Wang <nanwang1101@yahoo.com> * no need to erase sims Signed-off-by: Nan Wang <nanwang1101@yahoo.com> --------- Signed-off-by: Nan Wang <nanwang1101@yahoo.com>
1 parent 0ff8ffd commit 668df0b

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

bp/src/BPSimulator.m

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ - (NSString *)installApplicationWithHost:(NSString *)testHost withError:(NSError
127127
}];
128128
return nil;
129129
} else {
130-
[simDevice shutdownWithError:errPtr];
130+
[self shutdownSimulatorWithError:errPtr];
131131
if(*errPtr) {
132132
[BPUtils printInfo:ERROR withString:@"Shutdown simulator failed with error: %@", [*errPtr localizedDescription]];
133133
[deviceSet deleteDeviceAsync:simDevice completionHandler:^(NSError *error) {
@@ -307,6 +307,19 @@ - (BOOL)useSimulatorWithDeviceUDID:(NSUUID *)deviceUDID {
307307
return YES;
308308
}
309309

310+
- (BOOL)shutdownSimulatorWithError:(id *)error {
311+
[BPUtils printInfo:INFO withString:@"Starting Safe Shutdown of %@", self.device.UDID.UUIDString];
312+
313+
// Calling shutdown when already shutdown should be avoided (if detected).
314+
if ([self.device.stateString isEqualToString:@"Shutdown"]) {
315+
[BPUtils printInfo:INFO withString:@"Shutdown of %@ succeeded as it is already shutdown", self.device];
316+
*error = nil;
317+
return YES;
318+
}
319+
320+
return [self.device shutdownWithError:error];
321+
}
322+
310323
- (void)bootWithCompletion:(void (^)(NSError *error))completion {
311324
// Now boot it.
312325
[BPUtils printInfo:INFO withString:@"Booting a simulator without launching Simulator app"];
@@ -320,7 +333,7 @@ - (void)openSimulatorHeadlessWithCompletion:(void (^)(NSError *))completion {
320333
[self.device bootAsyncWithOptions:options completionHandler:^(NSError *bootError){
321334
NSError *error = [self waitForDeviceReady];
322335
if (error) {
323-
[self.device shutdownWithError:&error];
336+
[self shutdownSimulatorWithError:&error];
324337
if (error) {
325338
[BPUtils printInfo:ERROR withString:@"Shutting down Simulator failed: %@", [error localizedDescription]];
326339
}
@@ -571,7 +584,7 @@ - (void)deleteSimulatorWithCompletion:(void (^)(NSError *error, BOOL success))co
571584
}
572585
if (self.device) {
573586
[BPUtils printInfo:INFO withString:@"Shutting down Simulator"];
574-
[self.device shutdownWithError:&error];
587+
[self shutdownSimulatorWithError:&error];
575588
if (error) {
576589
[BPUtils printInfo:ERROR withString:@"Shutting down Simulator failed: %@", [error localizedDescription]];
577590
completion(error, NO);

0 commit comments

Comments
 (0)