@Dependency(\.date.now) is returning incorrect dates (either in future or past) when used in escaping closures #335
Replies: 5 comments
-
@stephencelis, Can you please help me with above question ? |
Beta Was this translation helpful? Give feedback.
-
If you capture |
Beta Was this translation helpful? Give feedback.
-
@pyrtsa yes, I want to freeze the |
Beta Was this translation helpful? Give feedback.
-
I took a deep dive into the code and found that Date.now is a computed property with a getter that returns a new Date() value each time it's accessed. So, the swift-dependencies package shouldn't really affect or manipulate the value once it's captured in the closure, because it's evaluated at the moment it's captured. Does anyone have any insights on what might cause the date to be manipulated later? |
Beta Was this translation helpful? Give feedback.
-
Hi @desusai-doordash, please do not tag us directly in issues/discussions. You will need to share more information in order for anyone to help. Can you reproduce the problem you are seeing in a fresh, minimal project and share that? Since it isn't clear this is an issue with the library I am going to convert it to a discussion. Please feel free to continue the conversation over there! |
Beta Was this translation helpful? Give feedback.
-
Description
I am not able to reproduce this, but observed in production that the dates retrieved using
@Dependency(\.date.now)
are incorrect in many cases. Some times they are from future and sometimes they are from past.After going through the documentation, i've noticed that it's stated here that we should use
withEscapedDependencies
to reflect correct values in case of passing over dependencies to escaping closures. Would this apply for the dependencies provided by default likeDate
as well ?Would using
withEscapedDependencies
solve the issue with incorrect dates ?Here is a sample code to demonstrate how I am using the value of date from
@Dependency(\.date.now)
in an escaping closure, if that would be helpful to understand more about the issue.In the above codebase I am seeing startTime is getting assigned with date either from future or past.
Checklist
main
branch of this package.Expected behavior
Correct date value should be available inside the closure.
Actual behavior
Incorrect date values are available within the closure.
Steps to reproduce
I am not able to reproduce this, may be this is happening at a high level of concurrency.
Dependencies version information
1.6.1
Destination operating system
Across iOS versions
Xcode version information
No response
Swift Compiler version information
Beta Was this translation helpful? Give feedback.
All reactions