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

List's loadedState should be public to know what is lazy loaded #3286

Closed
agrabz opened this issue Oct 9, 2023 · 11 comments
Closed

List's loadedState should be public to know what is lazy loaded #3286

agrabz opened this issue Oct 9, 2023 · 11 comments
Assignees
Labels
api Issues related to the API category bug Something isn't working datastore Issues related to the DataStore category feature-parity

Comments

@agrabz
Copy link

agrabz commented Oct 9, 2023

Describe the bug

It's currently impossible to determine if a List is lazy loaded or not, which can cause crashes.

Steps To Reproduce

1. Have a scheme with relationships between parent and child
2. Run codegen
3. Fetch parent object
4. Try to access child object as below:
parent.Children?.compactMap {
                    $0.childName
                }
5. Get crash with: Call `fetch` to lazy load the list before accessing `elements`.

Expected behavior

Inside List+Model.swift
In the class: public class List<ModelType: Model>: Collection, Codable, ExpressibleByArrayLiteral, ModelListMarker {
The below variable:
var loadedState: LoadedState
Is set to
public var loadedState: LoadedState

So I can determine if it's safe to use the list or not like:

parent.Children?.loadedState == .loaded ? .compactMap {
                    $0.childName
                } : nil

Or even better would be to just keep it nil instead of this loaded state, however I understand that this would be a bigger change compared to the above request.

Amplify Framework Version

latest

Amplify Categories

API

Dependency manager

Swift PM

Swift version

5.9

CLI version

12.5.0

Xcode version

15.0

Relevant log output

<details>
<summary>Log Messages</summary>


INSERT LOG MESSAGES HERE
```

Is this a regression?

No

Regression additional context

No response

Platforms

iOS

OS Version

not relevant

Device

not relevant

Specific to simulators

No response

Additional context

No response

@thisisabhash thisisabhash added api Issues related to the API category datastore Issues related to the DataStore category labels Oct 9, 2023
@thisisabhash
Copy link
Member

Hello,
Please use the Lazy Load feature for loading the child elements. The documentation is available here:
https://docs.amplify.aws/cli/migration/lazy-load-custom-selection-set/

For example, call pattern will change like below:
https://docs.amplify.aws/cli/migration/lazy-load-custom-selection-set/#scenario-3-belongs-to--has-one-access-pattern

Previously

let comment = /* queried Comment through DataStore or API */
let post = comment.post

With the latest codegen

let comment = /* queried Comment through DataStore or API */
let post = try await comment.post

@agrabz
Copy link
Author

agrabz commented Oct 10, 2023

Hello, Please use the Lazy Load feature for loading the child elements. The documentation is available here: https://docs.amplify.aws/cli/migration/lazy-load-custom-selection-set/

For example, call pattern will change like below: https://docs.amplify.aws/cli/migration/lazy-load-custom-selection-set/#scenario-3-belongs-to--has-one-access-pattern

Previously

let comment = /* queried Comment through DataStore or API */
let post = comment.post

With the latest codegen

let comment = /* queried Comment through DataStore or API */
let post = try await comment.post

Hi Abhash,

Thank you, but this isn't what I want. I don't want to fetch what I don't have already. I want to know if it's already loaded or not and apply logic based on that. Fetching without the proper logic is unnecessary load on the network. I don't see why loadedState is internal. If there's no specific reason, could you please make its getter public?

@thisisabhash thisisabhash added the feature-request Request a new feature label Oct 10, 2023
@github-actions
Copy link
Contributor

This has been identified as a feature request. If this feature is important to you, we strongly encourage you to give a 👍 reaction on the request. This helps us prioritize new features most important to you. Thank you!

@agrabz
Copy link
Author

agrabz commented Oct 13, 2023

With all the respect, I don't understand why this is a feature request and not a bug. This is a serious usability issue. The Amplify SDK allows me to use an object that should not be used and instead of throwing an exception it crashes the application.

@atierian
Copy link
Member

atierian commented Oct 13, 2023

allows me to use an object that should not be used and instead of throwing an exception it crashes the application.

Can you elaborate on that please - how are you using an object that shouldn't be used and how is it leading to a crash?
Please paste a full crash report if you have one. Thanks!

edit: I see it in your original post now. Taking a closer look.

@atierian
Copy link
Member

We're looking at publicly exposing the loadedState.

In the meantime, you should be able to call fetch() before accessing it if you're unsure. If the List is already loaded, fetch() is a no-op. I recognize that's not very ergonomic because fetch() is async - we're prioritizing the work and will get back to you here with any updates.

@agrabz
Copy link
Author

agrabz commented Oct 13, 2023 via email

@thisisabhash thisisabhash self-assigned this Oct 13, 2023
@atierian atierian added feature-parity work in progress Issues was triaged and investigation done and removed feature-request Request a new feature labels Oct 13, 2023
@thisisabhash
Copy link
Member

@agrabz we've opened a PR to address this - we're working to put out a release soon. We thank you for your patience.

@agrabz
Copy link
Author

agrabz commented Oct 15, 2023 via email

@atierian atierian added bug Something isn't working pending-release Code has been merged but pending release Code has been merged but pending release and removed work in progress Issues was triaged and investigation done labels Oct 17, 2023
@atierian
Copy link
Member

The isLoaded property was added in 2.21.0 released yesterday (10/20/2023).

Note, we kept the assertionFailure in there when accessing without fetching first. This only crashes in debug builds (-Onone); it won't crash / returns an empty array in release builds (-O).

Thanks again for reporting this.

@github-actions github-actions bot removed the pending-release Code has been merged but pending release Code has been merged but pending release label Oct 21, 2023
@agrabz
Copy link
Author

agrabz commented Oct 24, 2023

Thanks a lot again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Issues related to the API category bug Something isn't working datastore Issues related to the DataStore category feature-parity
Projects
None yet
Development

No branches or pull requests

3 participants