Skip to content

Commit

Permalink
Merge pull request #8 from npr/feature/issue-7-observing-network-calls
Browse files Browse the repository at this point in the history
Observing network calls
  • Loading branch information
dmacra authored Nov 23, 2018
2 parents 521bc81 + a320e56 commit 28e0f8f
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 3 deletions.
4 changes: 4 additions & 0 deletions RAD.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
FF16FA2F20F37F3700ED572E /* OperationQueue+Background.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF16FA2E20F37F3700ED572E /* OperationQueue+Background.swift */; };
FF16FA3120F3897400ED572E /* URLSessionConfiguration+Configurations.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF16FA3020F3897400ED572E /* URLSessionConfiguration+Configurations.swift */; };
FF1A69EC219AE1D500D735F6 /* SentTimezonedDatePredicateOperation.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF1A69EB219AE1D500D735F6 /* SentTimezonedDatePredicateOperation.swift */; };
FF1DA16421A7F0EA0097AFB4 /* NetworkObserver.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF1DA16321A7F0EA0097AFB4 /* NetworkObserver.swift */; };
FF2193ED217F0314003417FF /* OperationIsReadyTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF2193EC217F0314003417FF /* OperationIsReadyTestCase.swift */; };
FF2193EF217F0528003417FF /* OperationIsExecutingTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF2193EE217F0528003417FF /* OperationIsExecutingTestCase.swift */; };
FF2193F1217F07E7003417FF /* OperationTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF2193F0217F07E7003417FF /* OperationTestCase.swift */; };
Expand Down Expand Up @@ -332,6 +333,7 @@
FF16FA2E20F37F3700ED572E /* OperationQueue+Background.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "OperationQueue+Background.swift"; sourceTree = "<group>"; };
FF16FA3020F3897400ED572E /* URLSessionConfiguration+Configurations.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "URLSessionConfiguration+Configurations.swift"; sourceTree = "<group>"; };
FF1A69EB219AE1D500D735F6 /* SentTimezonedDatePredicateOperation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentTimezonedDatePredicateOperation.swift; sourceTree = "<group>"; };
FF1DA16321A7F0EA0097AFB4 /* NetworkObserver.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkObserver.swift; sourceTree = "<group>"; };
FF2193EC217F0314003417FF /* OperationIsReadyTestCase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OperationIsReadyTestCase.swift; sourceTree = "<group>"; };
FF2193EE217F0528003417FF /* OperationIsExecutingTestCase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OperationIsExecutingTestCase.swift; sourceTree = "<group>"; };
FF2193F0217F07E7003417FF /* OperationTestCase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OperationTestCase.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -994,6 +996,7 @@
FFF2860620C95B3300BCFF0A /* CMTimeScale.swift */,
FF10A8632181D54E008DEE36 /* DatabaseFetcher.swift */,
FF154FB02180B60D00E60011 /* ListeningObserver.swift */,
FF1DA16321A7F0EA0097AFB4 /* NetworkObserver.swift */,
FF154FAE2180B5FF00E60011 /* Object.swift */,
FF0ECF432179C8AC009C5528 /* Roundable.swift */,
);
Expand Down Expand Up @@ -1447,6 +1450,7 @@
FF00128B21380772008740D0 /* TimeRangeBoundBuilder.swift in Sources */,
FF0262A421258F91007DF038 /* OutputOperation.swift in Sources */,
FF4A3FB8212AB58400970A97 /* RADPayload.swift in Sources */,
FF1DA16421A7F0EA0097AFB4 /* NetworkObserver.swift in Sources */,
FFA9D51D2126FE2C004EBDD0 /* FetchOperation.swift in Sources */,
FFA9D5232126FF90004EBDD0 /* CreateEmptyObjectPredicateOperation.swift in Sources */,
FFE8B56A20E6599C0038A53B /* NetworkScheduler.swift in Sources */,
Expand Down
13 changes: 12 additions & 1 deletion RAD/AnalyticsDebuggable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,22 @@ public protocol AnalyticsDebuggable {
///
/// - Parameter observer: The observer.
func addListeningObserver(_ observer: ListeningObserver)
/// Removes an observer. It is not required to call the remove.
/// Stop observing listening events.
/// It is not required to call this function.
///
/// - Parameter observer: The registered observer.
func removeListeningObserver(_ observer: ListeningObserver)

/// Add an observer to receive callbacks when requests are performed.
///
/// - Parameter observer: The observer.
func addNetworkObserver(_ observer: NetworkObserver)
/// Stop observing network requests.
/// It is not required to call this function.
///
/// - Parameter observer: The observer.
func removeNetworkObserver(_ observer: NetworkObserver)

/// Extract RAD payload from an AVAsset. The RAD payload is formatted
/// to be ready for displaying.
/// The completion handler is called on main queue.
Expand Down
18 changes: 16 additions & 2 deletions RAD/AnalyticsDebugger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,27 @@ final class AnalyticsDebugger: AnalyticsDebuggable, PlayerObservationDelegate {
observersContainer.append(observer)
}

/// Unregister from listening events.
/// Unregister from observing listening events.
///
/// - Parameter observer: The observer which should be removed.
/// - Parameter observer: The observer be remove.
func removeListeningObserver(_ observer: ListeningObserver) {
observersContainer.remove(observer)
}

/// Register to observe network calls.
///
/// - Parameter observer: The observer to register.
func addNetworkObserver(_ observer: NetworkObserver) {
NetworkService.shared.addNetworkObserver(observer)
}

/// Unregister from observing network calls.
///
/// - Parameter observer: The observer to remove.
func removeNetworkObserver(_ observer: NetworkObserver) {
NetworkService.shared.removeNetworkObserver(observer)
}

func extractRADPayload(
from asset: AVAsset,
completion: @escaping ExtractCompletion
Expand Down
12 changes: 12 additions & 0 deletions RAD/Model/Network/NetworkService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class NetworkService: NSObject, URLSessionTaskDelegate {

private var session: URLSession!
private var tasksMap: [URLSessionTask: Completion] = [:]
private var observersContainer = WeakReferenceContainer<NetworkObserver>()

private override init() {
super.init()
Expand All @@ -35,12 +36,23 @@ class NetworkService: NSObject, URLSessionTaskDelegate {
delegateQueue: OperationQueue.background)
}

func addNetworkObserver(_ observer: NetworkObserver) {
observersContainer.append(observer)
}

func removeNetworkObserver(_ observer: NetworkObserver) {
observersContainer.remove(observer)
}

func executeRequest(_ request: URLRequest, completion: Completion? = nil) {
let task = session.dataTask(with: request)
if let completion = completion {
tasksMap[task] = completion
}
task.resume()
observersContainer.forEach({
$0?.didBeginExecutionOfUrlRequest(request)
})
}

// MARK: URLSessionDelegate
Expand Down
27 changes: 27 additions & 0 deletions RAD/Model/NetworkObserver.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// NetworkObserver.swift
// RAD
//
// Copyright 2018 NPR
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
// this file except in compliance with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied. See the License for the specific language governing permissions
// and limitations under the License.
//

import Foundation

/// A protocol which may be implemented to observe the network requests
/// executed by framework.
public protocol NetworkObserver: AnyObject {
/// Callback for each performed network request.
///
/// - Parameter request: The request.
func didBeginExecutionOfUrlRequest(_ request: URLRequest)
}

0 comments on commit 28e0f8f

Please sign in to comment.