Skip to content

Instantly share code, notes, and snippets.

@phylliswong
Created June 28, 2019 23:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phylliswong/e0fc4ebdea6a246ea76b618336c017f7 to your computer and use it in GitHub Desktop.
Save phylliswong/e0fc4ebdea6a246ea76b618336c017f7 to your computer and use it in GitHub Desktop.
public func get<T>(key: String, defaultValue: T) -> Any {
let group = DispatchGroup()
var allocations = [JSON]()
var value = [JSON]()
if (futureAllocations == nil) { return defaultValue }
// This needs to be a blocking operation
let _ = self.futureAllocations?.done { (jsonArray) in
allocations = jsonArray
}
// lines 9-11 MUST be complete BEFORE the rest of this code executes
if !Allocator.allocationsNotEmpty(allocations: allocations) {
return defaultValue
}
let type = getMyType(defaultValue)
guard let _ = type else { return defaultValue }
do {
let alloc = Allocations(allocations: allocations)
let v = try alloc.getValueFromAllocations(key, type, participant)
if let val = v { value = val }
} catch {
LOGGER.log(.error, message: "Unable to retrieve the treatment. Returning the default.")
return defaultValue
}
return value
}
@phylliswong
Copy link
Author

  let a = try futureAllocations?.wait()

Where is futureAllocations defined?

This gets initialized by the Allocator class's fetch method. When the client gets initialized, the value is a promise. Then when the promise is fulfilled ... after .wait() then I can perform the other tasks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment