@@ -95,9 +95,7 @@ class SubscriptionService : Service(), CoroutineScope {
95
95
}
96
96
Core .notification.notify(NOTIFICATION_ID , notification.build())
97
97
counter = 0
98
- val workers = urls.asIterable().map { url ->
99
- async(Dispatchers .IO ) { fetchJson(url, urls.size(), notification) }
100
- }
98
+ val workers = urls.asIterable().map { url -> fetchJsonAsync(url, urls.size(), notification) }
101
99
try {
102
100
val localJsons = workers.awaitAll()
103
101
withContext(Dispatchers .Main ) {
@@ -127,20 +125,20 @@ class SubscriptionService : Service(), CoroutineScope {
127
125
return START_NOT_STICKY
128
126
}
129
127
130
- private suspend fun fetchJson (url : URL , max : Int , notification : NotificationCompat .Builder ): File ? {
128
+ private fun fetchJsonAsync (url : URL , max : Int , notification : NotificationCompat .Builder ) = async( Dispatchers . IO ) {
131
129
val tempFile = File .createTempFile(" subscription-" , " .json" , cacheDir)
132
130
try {
133
131
(url.openConnection() as HttpURLConnection ).useCancellable {
134
132
tempFile.outputStream().use { out -> inputStream.copyTo(out ) }
135
133
}
136
- return tempFile
134
+ tempFile
137
135
} catch (e: IOException ) {
138
136
Timber .d(e)
139
137
launch(Dispatchers .Main ) {
140
138
Toast .makeText(this @SubscriptionService, e.readableMessage, Toast .LENGTH_LONG ).show()
141
139
}
142
140
if (! tempFile.delete()) tempFile.deleteOnExit()
143
- return null
141
+ null
144
142
} finally {
145
143
withContext(Dispatchers .Main ) {
146
144
counter + = 1
0 commit comments