Skip to content

Commit

Permalink
Move to use the property rather than a NSString.
Browse files Browse the repository at this point in the history
  • Loading branch information
mandel-macaque committed Feb 6, 2019
1 parent 9bd39d3 commit 50c92a2
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/Foundation/NSUrlSessionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
using nuint = System.UInt32;
#endif

#if !MONOMAC && !WATCH
#if !MONOMAC
using UIKit;
#endif

Expand Down Expand Up @@ -124,7 +124,7 @@ public partial class NSUrlSessionHandler : HttpMessageHandler
readonly NSUrlSession session;
readonly Dictionary<NSUrlSessionTask, InflightData> inflightRequests;
readonly object inflightRequestsLock = new object ();
#if !MONOMAC && !WATCH
#if !MONOMAC && !MONOTOUCH_WATCH
readonly bool isBackgroundSession = false;
NSObject notificationToken; // needed to make sure we do not hang if not using a background session
#endif
Expand All @@ -150,7 +150,7 @@ public NSUrlSessionHandler (NSUrlSessionConfiguration configuration)
if (configuration == null)
throw new ArgumentNullException (nameof (configuration));

#if !MONOMAC && !WATCH
#if !MONOMAC && !MONOTOUCH_WATCH
// if the configuration has an identifier, we are dealing with a background session,
// therefore, we do not have to listen to the notifications.
isBackgroundSession = !string.IsNullOrEmpty (configuration.Identifier);
Expand All @@ -173,13 +173,12 @@ public NSUrlSessionHandler (NSUrlSessionConfiguration configuration)
inflightRequests = new Dictionary<NSUrlSessionTask, InflightData> ();
}

#if !MONOMAC && !WATCH
#if !MONOMAC && !MONOTOUCH_WATCH

void AddNotification ()
{
if (!isBackgroundSession && notificationToken == null)
using (var notification = new NSString ("UIApplicationWillResignActiveNotification"))
notificationToken = NSNotificationCenter.DefaultCenter.AddObserver (notification, BackgroundNotificationCb);
notificationToken = NSNotificationCenter.DefaultCenter.AddObserver (UIApplication.WillResignActiveNotification, BackgroundNotificationCb);
}

void RemoveNotification ()
Expand All @@ -206,7 +205,7 @@ void RemoveInflightData (NSUrlSessionTask task, bool cancel = true)
{
lock (inflightRequestsLock) {
inflightRequests.Remove (task);
#if !MONOMAC && !WATCH
#if !MONOMAC && !MONOTOUCH_WATCH
// do we need to be notified? If we have not inflightData, we do not
if (inflightRequests.Count == 0)
RemoveNotification ();
Expand All @@ -221,7 +220,7 @@ void RemoveInflightData (NSUrlSessionTask task, bool cancel = true)

protected override void Dispose (bool disposing)
{
#if !MONOMAC && !WATCH
#if !MONOMAC && !MONOTOUCH_WATCH
// remove the notification if present, method checks against null
RemoveNotification ();
#endif
Expand Down Expand Up @@ -305,7 +304,7 @@ protected override async Task<HttpResponseMessage> SendAsync (HttpRequestMessage
});

lock (inflightRequestsLock) {
#if !MONOMAC && !WATCH
#if !MONOMAC && !MONOTOUCH_WATCH
// Add the notification whenever needed
AddNotification ();
#endif
Expand Down

0 comments on commit 50c92a2

Please sign in to comment.