Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit d349b1b

Browse files
committed
remove async dispatchers
1 parent 3d0f624 commit d349b1b

File tree

1 file changed

+80
-81
lines changed

1 file changed

+80
-81
lines changed

CodePush.m

Lines changed: 80 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ + (void)clearUpdates
148148
#pragma mark - Private API methods
149149

150150
@synthesize bridge = _bridge;
151+
@synthesize methodQueue = _methodQueue;
151152

152153
+ (NSURL *)binaryBundleURL
153154
{
@@ -393,27 +394,29 @@ - (void)savePendingUpdate:(NSString *)packageHash
393394
resolver:(RCTPromiseResolveBlock)resolve
394395
rejecter:(RCTPromiseRejectBlock)reject)
395396
{
396-
dispatch_async(dispatch_get_main_queue(), ^{
397-
NSDictionary *mutableUpdatePackage = [updatePackage mutableCopy];
398-
NSURL *binaryBundleURL = [CodePush binaryBundleURL];
399-
if (binaryBundleURL != nil) {
400-
[mutableUpdatePackage setValue:[CodePush modifiedDateStringOfFileAtURL:binaryBundleURL]
401-
forKey:BinaryBundleDateKey];
402-
}
403-
404-
[CodePushPackage downloadPackage:mutableUpdatePackage
405-
// The download is progressing forward
406-
progressCallback:^(long long expectedContentLength, long long receivedContentLength) {
397+
NSDictionary *mutableUpdatePackage = [updatePackage mutableCopy];
398+
NSURL *binaryBundleURL = [CodePush binaryBundleURL];
399+
if (binaryBundleURL != nil) {
400+
[mutableUpdatePackage setValue:[CodePush modifiedDateStringOfFileAtURL:binaryBundleURL]
401+
forKey:BinaryBundleDateKey];
402+
}
403+
404+
[CodePushPackage downloadPackage:mutableUpdatePackage
405+
// The download is progressing forward
406+
progressCallback:^(long long expectedContentLength, long long receivedContentLength) {
407+
dispatch_async(_methodQueue, ^{
407408
// Notify the script-side about the progress
408409
[self.bridge.eventDispatcher
409410
sendDeviceEventWithName:@"CodePushDownloadProgress"
410411
body:@{
411412
@"totalBytes":[NSNumber numberWithLongLong:expectedContentLength],
412413
@"receivedBytes":[NSNumber numberWithLongLong:receivedContentLength]
413414
}];
414-
}
415-
// The download completed
416-
doneCallback:^{
415+
});
416+
}
417+
// The download completed
418+
doneCallback:^{
419+
dispatch_async(_methodQueue, ^{
417420
NSError *err;
418421
NSDictionary *newPackage = [CodePushPackage getPackage:mutableUpdatePackage[PackageHashKey] error:&err];
419422

@@ -422,16 +425,18 @@ - (void)savePendingUpdate:(NSString *)packageHash
422425
}
423426

424427
resolve(newPackage);
425-
}
426-
// The download failed
427-
failCallback:^(NSError *err) {
428+
});
429+
}
430+
// The download failed
431+
failCallback:^(NSError *err) {
432+
dispatch_async(_methodQueue, ^{
428433
if ([CodePushPackage isCodePushError:err]) {
429434
[self saveFailedUpdate:mutableUpdatePackage];
430435
}
431436

432437
reject([NSString stringWithFormat: @"%lu", (long)err.code], err.localizedDescription, err);
433-
}];
434-
});
438+
});
439+
}];
435440
}
436441

437442
/*
@@ -452,21 +457,19 @@ - (void)savePendingUpdate:(NSString *)packageHash
452457
RCT_EXPORT_METHOD(getCurrentPackage:(RCTPromiseResolveBlock)resolve
453458
rejecter:(RCTPromiseRejectBlock)reject)
454459
{
455-
dispatch_async(dispatch_get_main_queue(), ^{
456-
NSError *error;
457-
NSMutableDictionary *package = [[CodePushPackage getCurrentPackage:&error] mutableCopy];
458-
459-
if (error) {
460-
reject([NSString stringWithFormat: @"%lu", (long)error.code], error.localizedDescription, error);
461-
}
462-
463-
// Add the "isPending" virtual property to the package at this point, so that
464-
// the script-side doesn't need to immediately call back into native to populate it.
465-
BOOL isPendingUpdate = [self isPendingUpdate:[package objectForKey:PackageHashKey]];
466-
[package setObject:@(isPendingUpdate) forKey:PackageIsPendingKey];
467-
468-
resolve(package);
469-
});
460+
NSError *error;
461+
NSMutableDictionary *package = [[CodePushPackage getCurrentPackage:&error] mutableCopy];
462+
463+
if (error) {
464+
reject([NSString stringWithFormat: @"%lu", (long)error.code], error.localizedDescription, error);
465+
}
466+
467+
// Add the "isPending" virtual property to the package at this point, so that
468+
// the script-side doesn't need to immediately call back into native to populate it.
469+
BOOL isPendingUpdate = [self isPendingUpdate:[package objectForKey:PackageHashKey]];
470+
[package setObject:@(isPendingUpdate) forKey:PackageIsPendingKey];
471+
472+
resolve(package);
470473
}
471474

472475
/*
@@ -477,32 +480,30 @@ - (void)savePendingUpdate:(NSString *)packageHash
477480
resolver:(RCTPromiseResolveBlock)resolve
478481
rejecter:(RCTPromiseRejectBlock)reject)
479482
{
480-
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
481-
NSError *error;
482-
[CodePushPackage installPackage:updatePackage
483-
removePendingUpdate:[self isPendingUpdate:nil]
484-
error:&error];
483+
NSError *error;
484+
[CodePushPackage installPackage:updatePackage
485+
removePendingUpdate:[self isPendingUpdate:nil]
486+
error:&error];
487+
488+
if (error) {
489+
reject([NSString stringWithFormat: @"%lu", (long)error.code], error.localizedDescription, error);
490+
} else {
491+
[self savePendingUpdate:updatePackage[PackageHashKey]
492+
isLoading:NO];
485493

486-
if (error) {
487-
reject([NSString stringWithFormat: @"%lu", (long)error.code], error.localizedDescription, error);
488-
} else {
489-
[self savePendingUpdate:updatePackage[PackageHashKey]
490-
isLoading:NO];
491-
492-
if (installMode == CodePushInstallModeOnNextResume && !_hasResumeListener) {
493-
// Ensure we do not add the listener twice.
494-
// Register for app resume notifications so that we
495-
// can check for pending updates which support "restart on resume"
496-
[[NSNotificationCenter defaultCenter] addObserver:self
497-
selector:@selector(loadBundle)
498-
name:UIApplicationWillEnterForegroundNotification
499-
object:[UIApplication sharedApplication]];
500-
_hasResumeListener = YES;
501-
}
502-
// Signal to JS that the update has been applied.
503-
resolve(nil);
494+
if (installMode == CodePushInstallModeOnNextResume && !_hasResumeListener) {
495+
// Ensure we do not add the listener twice.
496+
// Register for app resume notifications so that we
497+
// can check for pending updates which support "restart on resume"
498+
[[NSNotificationCenter defaultCenter] addObserver:self
499+
selector:@selector(loadBundle)
500+
name:UIApplicationWillEnterForegroundNotification
501+
object:[UIApplication sharedApplication]];
502+
_hasResumeListener = YES;
504503
}
505-
});
504+
// Signal to JS that the update has been applied.
505+
resolve(nil);
506+
}
506507
}
507508

508509
/*
@@ -551,33 +552,31 @@ - (void)savePendingUpdate:(NSString *)packageHash
551552
RCT_EXPORT_METHOD(getNewStatusReport:(RCTPromiseResolveBlock)resolve
552553
rejecter:(RCTPromiseRejectBlock)reject)
553554
{
554-
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
555-
if (needToReportRollback) {
556-
needToReportRollback = NO;
557-
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
558-
NSMutableArray *failedUpdates = [preferences objectForKey:FailedUpdatesKey];
559-
if (failedUpdates) {
560-
NSDictionary *lastFailedPackage = [failedUpdates lastObject];
561-
if (lastFailedPackage) {
562-
resolve([CodePushTelemetryManager getRollbackReport:lastFailedPackage]);
563-
return;
564-
}
565-
}
566-
} else if (_isFirstRunAfterUpdate) {
567-
NSError *error;
568-
NSDictionary *currentPackage = [CodePushPackage getCurrentPackage:&error];
569-
if (!error && currentPackage) {
570-
resolve([CodePushTelemetryManager getUpdateReport:currentPackage]);
555+
if (needToReportRollback) {
556+
needToReportRollback = NO;
557+
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
558+
NSMutableArray *failedUpdates = [preferences objectForKey:FailedUpdatesKey];
559+
if (failedUpdates) {
560+
NSDictionary *lastFailedPackage = [failedUpdates lastObject];
561+
if (lastFailedPackage) {
562+
resolve([CodePushTelemetryManager getRollbackReport:lastFailedPackage]);
571563
return;
572564
}
573-
} else if (isRunningBinaryVersion) {
574-
NSString *appVersion = [[CodePushConfig current] appVersion];
575-
resolve([CodePushTelemetryManager getBinaryUpdateReport:appVersion]);
565+
}
566+
} else if (_isFirstRunAfterUpdate) {
567+
NSError *error;
568+
NSDictionary *currentPackage = [CodePushPackage getCurrentPackage:&error];
569+
if (!error && currentPackage) {
570+
resolve([CodePushTelemetryManager getUpdateReport:currentPackage]);
576571
return;
577572
}
578-
579-
resolve(nil);
580-
});
573+
} else if (isRunningBinaryVersion) {
574+
NSString *appVersion = [[CodePushConfig current] appVersion];
575+
resolve([CodePushTelemetryManager getBinaryUpdateReport:appVersion]);
576+
return;
577+
}
578+
579+
resolve(nil);
581580
}
582581

583582
/*

0 commit comments

Comments
 (0)