@@ -158,7 +158,30 @@ public class Reporter {
158158 }
159159 }
160160
161+ func deviceAllowFlush( ) -> Bool {
162+ // Check network and battery condition
163+ var error : Error ?
164+ let connectionType = networkUtility. connectionType
165+ if connectionType == . none || connectionType == . undetermined {
166+ error = InstanaError . offline
167+ } else if suspendReporting. contains ( . cellularConnection) , connectionType == . cellular {
168+ error = InstanaError . noWifiAvailable
169+ } else if suspendReporting. contains ( . lowBattery) , !batterySafeForNetworking( ) {
170+ error = InstanaError . lowBattery
171+ }
172+ if error != nil {
173+ session. logger. add ( " Cannot send beacon: \( error!) " , level: . warning)
174+ completionHandler. forEach { $0 ( BeaconResult . failure ( error!) ) }
175+ return false
176+ }
177+ return true
178+ }
179+
161180 func canScheduleFlush( ) -> Bool {
181+ if !deviceAllowFlush( ) {
182+ return false
183+ }
184+
162185 if flusher == nil {
163186 return true
164187 }
@@ -185,16 +208,6 @@ public class Reporter {
185208
186209 let start = Date ( )
187210 var debounce : TimeInterval
188- let connectionType = networkUtility. connectionType
189- guard connectionType != . none else {
190- return handle ( flushResult: . failure( [ InstanaError . offline] ) )
191- }
192- if suspendReporting. contains ( . cellularConnection) , connectionType == . cellular {
193- return handle ( flushResult: . failure( [ InstanaError . noWifiAvailable] ) )
194- }
195- if suspendReporting. contains ( . lowBattery) , !batterySafeForNetworking( ) {
196- return handle ( flushResult: . failure( [ InstanaError . lowBattery] ) )
197- }
198211 var beacons : Set < CoreBeacon > = Set ( [ ] )
199212 inSlowModeBeforeFlush = isInSlowSendMode
200213 if inSlowModeBeforeFlush {
@@ -218,7 +231,7 @@ public class Reporter {
218231 guard let self = self else { return }
219232 self . dispatchQueue. async { [ weak self] in
220233 let originalBeacons : Set < CoreBeacon > ? = ( result. sentBeacons. count < beacons. count) ? beacons : nil
221- self ? . handle ( flushResult: result, start, fromBeaconFlusherCompletion : true , originalBeacons: originalBeacons)
234+ self ? . handle ( flushResult: result, start, originalBeacons: originalBeacons)
222235 }
223236 }
224237 flusher. schedule ( )
@@ -253,7 +266,6 @@ public class Reporter {
253266
254267 private func handle( flushResult: BeaconFlusher . Result ,
255268 _ start: Date = Date ( ) ,
256- fromBeaconFlusherCompletion: Bool = false ,
257269 originalBeacons: Set < CoreBeacon > ? = nil ) {
258270 let result : BeaconResult
259271 let errors = flushResult. errors
@@ -297,22 +309,20 @@ public class Reporter {
297309 }
298310 }
299311
300- if fromBeaconFlusherCompletion {
301- flusher = nil // mark this round flush done
302- lastFlushStartTime = nil
303- if inSlowModeBeforeFlush {
304- // Another flush either resend 1 beacon (still in slow mode currently)
305- // or flush remaining beacons (got out of slow send mode already)
306- var msg : String
307- if isInSlowSendMode {
308- msg = " schedule flush to send 1 beacon in slow send mode "
309- } else {
310- msg = " flush all beacons after out of slow send mode "
311- }
312- session. logger. add ( msg)
312+ flusher = nil // mark this round flush done
313+ lastFlushStartTime = nil
314+ if inSlowModeBeforeFlush {
315+ // Another flush either resend 1 beacon (still in slow mode currently)
316+ // or flush remaining beacons (got out of slow send mode already)
317+ var msg : String
318+ if isInSlowSendMode {
319+ msg = " schedule flush to send 1 beacon in slow send mode "
320+ } else {
321+ msg = " flush all beacons after out of slow send mode "
313322 }
314- // schedule next round flush
315- scheduleFlush ( )
323+ session. logger. add ( msg)
316324 }
325+ // schedule next round flush
326+ scheduleFlush ( )
317327 }
318328}
0 commit comments