From 1ab5ad82b54b579c7a54bc49d76cc2f2552a0985 Mon Sep 17 00:00:00 2001 From: Ian Bytchek Date: Fri, 15 May 2020 17:08:34 +0100 Subject: [PATCH] Improve best appcast item selection handling This fixes #1572 and takes care of situations when item dates are unavailable and when items with higher versions use earlier dates than items with lower versions, e.g., version 1.2.3 was published today and version 2.3.4 was published yesterday. --- Sparkle/SUBasicUpdateDriver.m | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Sparkle/SUBasicUpdateDriver.m b/Sparkle/SUBasicUpdateDriver.m index 745434f4..1a940399 100644 --- a/Sparkle/SUBasicUpdateDriver.m +++ b/Sparkle/SUBasicUpdateDriver.m @@ -118,13 +118,17 @@ - (SUAppcastItem *)bestItemFromAppcastItems:(NSArray *)appcastItems getDeltaItem:(SUAppcastItem * __autoreleasing *)deltaItem withHostVersion:(NSString *)hostVersion comparator:(id)comparator { SUAppcastItem *item = nil; + NSComparisonResult order; + for(SUAppcastItem *candidate in appcastItems) { if ([self hostSupportsItem:candidate]) { + // Pick this item if nothing is picked yet. Always pick an item with a higher version. Only if versions are the same + // compare their dates and pick this item if its date is not lower – this covers cases when no date is available + // and picks items at the end of the appcast list as they are more likely to be the most recent releases. if ( - !item || ( - [item.date compare:candidate.date] == NSOrderedAscending && - [comparator compareVersion:item.versionString toVersion:candidate.versionString] != NSOrderedDescending - ) + !item + || (order = [comparator compareVersion:item.versionString toVersion:candidate.versionString]) == NSOrderedAscending + || (order == NSOrderedSame && [item.date compare:candidate.date] != NSOrderedDescending) ) { item = candidate; }