Skip to content

Commit

Permalink
Fix bug where bundle load handlers weren't called
Browse files Browse the repository at this point in the history
Essentially, if the app bundles are locally "up-to-date", the `remoteBundlesDownloaded` property was not set to YES. This was resulting in a cycle where:

a) we weren't downloading new bundles because our manifest was up-to-date
b) we tried to load a bundle, and it was being added to the pending load handlers, because `remoteBundlesDownloaded` was NO
  • Loading branch information
rsattar committed Jan 27, 2016
1 parent 15b9963 commit 3f852f0
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions LaunchKit/Classes/Bundles/LKBundlesManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,21 @@ - (void) updateServerBundlesUpdatedTimeWithTime:(NSDate *)bundlesUpdatedTime
// We have the same 'local' server time as our current, so
// mark that we have the latest, and there's nothing else to do
self.latestRemoteBundlesManifestRetrieved = YES;

// We may have tried to load some bundles earlier, but were waiting to
// verify that our localBundlesFolderUpdatedTime is *still* the same as
// on the server, so flush any pending loads.
dispatch_async(dispatch_get_main_queue(), ^{
[self notifyAnyPendingBundleLoadHandlers];
});

// Check against any remoteBundleMap we may have whether or not we need to download anything
NSMutableArray *infosNeedingDownload = [self remoteBundleInfosNeedingDownloadForceRetrieve:NO];
self.remoteBundlesDownloaded = (infosNeedingDownload.count == 0);
if (self.remoteBundlesDownloaded) {
// We may have tried to load some bundles earlier, but were waiting to
// verify that our localBundlesFolderUpdatedTime is *still* the same as
// on the server, so flush any pending loads.
dispatch_async(dispatch_get_main_queue(), ^{
[self notifyAnyPendingBundleLoadHandlers];
});
} else if (!self.downloadingRemoteBundles) {
// This should only happen if somehow our local bundles cache was *partially* purged.
// We have an updated manifest, but some of our local bundles aren't downloaded, so go and get them
[self downloadRemoteBundlesForceRetrieve:NO associatedServerTimestamp:self.localBundlesFolderUpdatedTime completion:nil];
}
return;
}

Expand Down Expand Up @@ -630,9 +637,9 @@ - (void)deleteLocalBundleInfo:(LKBundleInfo *)bundleInfo

#pragma mark - Bundle Downloading

- (void) downloadRemoteBundlesForceRetrieve:(BOOL)forceRetrieve associatedServerTimestamp:(NSDate *)serverTimestamp completion:(void (^)(NSError *error))completion
- (NSMutableArray<LKBundleInfo *> *) remoteBundleInfosNeedingDownloadForceRetrieve:(BOOL)forceRetrieve
{
NSMutableArray *infosNeedingDownload = [NSMutableArray arrayWithCapacity:self.remoteBundleMap.count];
NSMutableArray<LKBundleInfo *> *infosNeedingDownload = [NSMutableArray arrayWithCapacity:self.remoteBundleMap.count];
// Make a list of infos that need to be downloaded
for (NSString *name in self.remoteBundleMap) {
LKBundleInfo *remoteInfo = self.remoteBundleMap[name];
Expand All @@ -642,6 +649,16 @@ - (void) downloadRemoteBundlesForceRetrieve:(BOOL)forceRetrieve associatedServer
[infosNeedingDownload addObject:remoteInfo];
}
}
return infosNeedingDownload;
}

- (void) downloadRemoteBundlesForceRetrieve:(BOOL)forceRetrieve associatedServerTimestamp:(NSDate *)serverTimestamp completion:(void (^)(NSError *error))completion
{
if (self.downloadingRemoteBundles) {
// TODO(Riz): fire completion handler?
return;
}
NSMutableArray<LKBundleInfo *> *infosNeedingDownload = [self remoteBundleInfosNeedingDownloadForceRetrieve:forceRetrieve];

__block NSInteger numItemsToDownload = infosNeedingDownload.count;
__weak LKBundlesManager *_weakSelf = self;
Expand Down

0 comments on commit 3f852f0

Please sign in to comment.