Wrapper Classes should be frozen on create
See original GitHub issueIf we freeze the wrapper classes after creation, we can then safely pass those around between iOS threads.
Simple example in pseudo swift code:
this will work as both the creation and the call happen on the main thread
THREAD:MAIN {
let loadUserUseCaseIos = LoadUserUseCaseIos(LoadUserUseCase(...))
loadUserUseCaseIos.loadUser(username: "foo").subscribe(
scope: coroutineScope, //this can be provided automatically, more on that below
onSuccess: { user in print(user?.description() ?? "none") },
onThrow: { error in print(error.description())}
)
}
this will FAIL as the creation happens on another thread than the call
THREAD:MAIN {
let loadUserUseCaseIos = LoadUserUseCaseIos(LoadUserUseCase(...))
}
THREAD:IO {
loadUserUseCaseIos.loadUser(username: "foo").subscribe(
scope: coroutineScope, //this can be provided automatically, more on that below
onSuccess: { user in print(user?.description() ?? "none") },
onThrow: { error in print(error.description())}
)
}
But if we freeze the LoadUserUseCaseIos after creation, both will work fine. https://kotlinlang.org/docs/mobile/concurrency-overview.html#immutable-and-frozen-state
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (8 by maintainers)
Top Results From Across the Web
How to create custom errors in a wrapper class
I am using wrapper classes to add/ create new records for the 2 child objects on the page. Each action and issue added...
Read more >Jackson - Wrap POJO in a wrapper class with metadata
The issue is it seems to get stuck in a loop. Constantly trying to serialise the model, nesting within it self. I've tried...
Read more >Object.freeze() - JavaScript - MDN Web Docs
To make an object immutable, recursively freeze each non-primitive property (deep freeze). Use the pattern on a case-by-case basis based on your ...
Read more >Wrapper Classes in Java - GeeksforGeeks
A Wrapper class is a class whose object wraps or contains primitive data types. When we create an object to a wrapper class,...
Read more >Attributes — The Swift Programming Language (Swift 5.7)
Apply this attribute to a class, structure, enumeration, or protocol to treat ... Dynamic member lookup by member name can be used to...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Fixed in 0.6.0 via #33.
The samples can be found here:
I just looked through the repo by @russhwolf and he created 4 wrappers to be able to handle nullable and non-null data. I still think this is a good idea and would make the swift side of the code easier to work with. At least it would be up to the consumer of the library to choose and not forced.
https://github.com/touchlab/SwiftCoroutines