Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use data and lock #40

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions FlagsmithClient/Classes/Flagsmith.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import FoundationNetworking

/// Manage feature flags and remote config across multiple projects,
/// environments and organisations.
public class Flagsmith {
public final class Flagsmith {
/// Shared singleton client object
public static let shared = Flagsmith()
private let apiManager = APIManager()
Expand Down Expand Up @@ -263,7 +263,7 @@ public class Flagsmith {
}
}

public class CacheConfig {
public final class CacheConfig {

/// Cache to use when enabled, defaults to the shared app cache
public var cache: URLCache = URLCache.shared
Expand Down
23 changes: 16 additions & 7 deletions FlagsmithClient/Classes/Internal/APIManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import FoundationNetworking
#endif

/// Handles interaction with a **Flagsmith** api.
class APIManager : NSObject, URLSessionDataDelegate {
final class APIManager : NSObject, URLSessionDataDelegate {

private var session: URLSession!
private let lock: NSLock = NSLock()
private let delegateQueue: OperationQueue = OperationQueue()

/// Base `URL` used for requests.
var baseURL = URL(string: "https://edge.api.flagsmith.com/api/v1/")!
Expand All @@ -22,22 +24,25 @@ class APIManager : NSObject, URLSessionDataDelegate {

// store the completion handlers and accumulated data for each task
private var tasksToCompletionHandlers:[URLSessionDataTask:(Result<Data, Error>) -> Void] = [:]
private var tasksToData:[URLSessionDataTask:NSMutableData] = [:]
private var tasksToData:[URLSessionDataTask:Data] = [:]

override init() {
super.init()
let configuration = URLSessionConfiguration.default
self.session = URLSession(configuration: configuration, delegate: self, delegateQueue: OperationQueue.main)
delegateQueue.maxConcurrentOperationCount = 1
self.session = URLSession(configuration: configuration, delegate: self, delegateQueue: delegateQueue)
}

func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
if let dataTask = task as? URLSessionDataTask {
lock.lock()
defer { lock.unlock() }
if let completion = tasksToCompletionHandlers[dataTask] {
if let error = error {
completion(.failure(FlagsmithError.unhandled(error)))
}
else {
let data = tasksToData[dataTask] ?? NSMutableData()
let data = tasksToData[dataTask] ?? Data()
completion(.success(data as Data))
}
}
Expand All @@ -47,7 +52,6 @@ class APIManager : NSObject, URLSessionDataDelegate {
}

func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, willCacheResponse proposedResponse: CachedURLResponse, completionHandler: @escaping (CachedURLResponse?) -> Void) {

// intercept and modify the cache settings for the response
if Flagsmith.shared.cacheConfig.useCache {
let newResponse = proposedResponse.response(withExpirationDuration: Int(Flagsmith.shared.cacheConfig.cacheTTL))
Expand All @@ -58,7 +62,10 @@ class APIManager : NSObject, URLSessionDataDelegate {
}

func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
let existingData = tasksToData[dataTask] ?? NSMutableData()
lock.lock()
defer { lock.unlock() }

var existingData = tasksToData[dataTask] ?? Data()
existingData.append(data)
tasksToData[dataTask] = existingData
}
Expand Down Expand Up @@ -98,8 +105,10 @@ class APIManager : NSObject, URLSessionDataDelegate {
}

// we must use the delegate form here, not the completion handler, to be able to modify the cache
lock.lock()
let task = session.dataTask(with: request)
tasksToCompletionHandlers[task] = completion
lock.unlock()
task.resume()
}

Expand Down
2 changes: 1 addition & 1 deletion FlagsmithClient/Classes/Internal/FlagsmithAnalytics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation

/// Internal analytics for the **FlagsmithClient**
class FlagsmithAnalytics {
final class FlagsmithAnalytics {

/// Indicates if analytics are enabled.
var enableAnalytics: Bool = true
Expand Down
2 changes: 1 addition & 1 deletion FlagsmithClient/Classes/Internal/Router.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
import FoundationNetworking
#endif

enum Router {
enum Router: Sendable {
private enum HTTPMethod: String {
case get = "GET"
case post = "POST"
Expand Down