Skip to content

Commit acb499b

Browse files
authored
Merge pull request #48 from flutter-news-app-full-source-code/Refine-Cache-Concurrency-Cleanup-in-country-service
fix(country): retry failed country fetch operations
2 parents 480e897 + 9115a37 commit acb499b

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

lib/src/services/country_service.dart

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,10 @@ class CountryService {
117117
_log.finer('Returning cached event countries.');
118118
return _cachedEventCountries!.data;
119119
}
120-
// Atomically assign the future if no fetch is in progress.
121-
_eventCountriesFuture ??= _fetchAndCacheEventCountries();
120+
// Atomically assign the future if no fetch is in progress,
121+
// and clear it when the future completes.
122+
_eventCountriesFuture ??= _fetchAndCacheEventCountries()
123+
.whenComplete(() => _eventCountriesFuture = null);
122124
return _eventCountriesFuture!;
123125
}
124126

@@ -133,8 +135,10 @@ class CountryService {
133135
_log.finer('Returning cached headquarter countries.');
134136
return _cachedHeadquarterCountries!.data;
135137
}
136-
// Atomically assign the future if no fetch is in progress.
137-
_headquarterCountriesFuture ??= _fetchAndCacheHeadquarterCountries();
138+
// Atomically assign the future if no fetch is in progress,
139+
// and clear it when the future completes.
140+
_headquarterCountriesFuture ??= _fetchAndCacheHeadquarterCountries()
141+
.whenComplete(() => _headquarterCountriesFuture = null);
138142
return _headquarterCountriesFuture!;
139143
}
140144

@@ -155,9 +159,13 @@ class CountryService {
155159
'event countries.',
156160
);
157161
return distinctCountries;
158-
} finally {
159-
// Clear the future once the operation is complete (success or error).
160-
_eventCountriesFuture = null;
162+
} catch (e, s) {
163+
_log.severe(
164+
'Failed to fetch distinct event countries via aggregation.',
165+
e,
166+
s,
167+
);
168+
rethrow; // Re-throw the original exception
161169
}
162170
}
163171

@@ -178,9 +186,13 @@ class CountryService {
178186
'headquarter countries.',
179187
);
180188
return distinctCountries;
181-
} finally {
182-
// Clear the future once the operation is complete (success or error).
183-
_headquarterCountriesFuture = null;
189+
} catch (e, s) {
190+
_log.severe(
191+
'Failed to fetch distinct headquarter countries via aggregation.',
192+
e,
193+
s,
194+
);
195+
rethrow; // Re-throw the original exception
184196
}
185197
}
186198

0 commit comments

Comments
 (0)