Created
June 28, 2019 23:41
-
-
Save phylliswong/e0fc4ebdea6a246ea76b618336c017f7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
How did u use dispatch groups?
public func get<T>(key: String, defaultValue: T) -> Any {
// init dispatch group here
let group = DispatchGroup()
var allocations = [JSON]()
var value = [JSON]()
if (futureAllocations == nil) { return defaultValue }
// This needs to be a blocking operation
group.enter()
let _ = self.futureAllocations?.done { (jsonArray) in
allocations = jsonArray
group.leave()
}
group.notify(queue: .main) { [weak self] in
// here things broke down. my code was like.... weak self what????
// also i assumed i could wrap the code that needed to complete in here, however that didn't work cuz scope
}
// these lines of code executed before the group finished its work :(
// 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
}
I was unable to move the following code inside the notify method because it is a closure that expects a Void return.
Got the solution...it was waaaaay easier than I was making it.
public func get<T>(key: String, defaultValue: T) -> Any {
var value = [JSON]()
var promisedAllocations = [JSON]()
if (futureAllocations == nil) {
print("\(String(describing: futureAllocations))")
return defaultValue
}
do {
let a = try futureAllocations?.wait()
guard let alloc = a else {
return defaultValue
}
promisedAllocations = alloc
if !Allocator.allocationsNotEmpty(allocations: promisedAllocations) {
return defaultValue
}
let type = getMyType(defaultValue)
guard let _ = type else { return defaultValue }
do {
let alloc = Allocations(allocations: promisedAllocations)
let v = try alloc.getValueFromAllocations(key, type, participant)
value = [v] as! [JSON]
} catch {
LOGGER.log(.error, message: "Unable to retrieve the treatment. Returning the default.")
return defaultValue
}
} catch {
LOGGER.log(.debug, message: "Error retrieving Allocations")
}
return value
}
let a = try futureAllocations?.wait()
Where is futureAllocations
defined?
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
Things I tried that didn't quite work are:
}.then {
}.ensure........
The problem is that I am "unpacking" the promise and need to wait for it to be done before the rest of the code gets executed.
SMH 🤷🏽♀️