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

Crash when saving data using datastore #3595

Closed
amruth-movano opened this issue Apr 4, 2024 · 7 comments
Closed

Crash when saving data using datastore #3595

amruth-movano opened this issue Apr 4, 2024 · 7 comments
Assignees
Labels
bug Something isn't working datastore Issues related to the DataStore category

Comments

@amruth-movano
Copy link

Describe the bug

We are saving data to data store with predicate. I am getting crash when predicate is not nil. But when predicate set to nil, everything works fine.

Fatal error: SWIFT TASK CONTINUATION MISUSE: save(_:modelSchema:where:) tried to resume its continuation more than once, throwing DataStoreError: Cannot apply a condition on model which does not exist.
Recovery suggestion: Save the model instance without a condition first.!

But we want to add predicate for all the queries

Steps To Reproduce

1. Save the data using predicate

Expected behavior

App should save data to dynamodb

Amplify Framework Version

2.28.0

Amplify Categories

DataStore

Dependency manager

Swift PM

Swift version

5.0

CLI version

NA

Xcode version

15.2

Relevant log output

Fatal error: SWIFT TASK CONTINUATION MISUSE: save(_:modelSchema:where:) tried to resume its continuation more than once, throwing DataStoreError: Cannot apply a condition on model which does not exist.
Recovery suggestion: Save the model instance without a condition first.!

Is this a regression?

No

Regression additional context

No response

Platforms

iOS

OS Version

iOS 17.4

Device

iPhone 15 Plus

Specific to simulators

No response

Additional context

No response

@ruisebas
Copy link
Member

ruisebas commented Apr 4, 2024

Hi @amruth-movano , thanks for opening this issue. We will try to reproduce and post any updates here.

To help us root cause, could you please enable verbose logging by adding Amplify.Logging.logLevel = .verbose before calling Amplify.configure() and share with us the logs up until the crash?

If you can add some code snippets on how you're using the API, that's also helpful.

Thanks!

@ruisebas ruisebas added bug Something isn't working datastore Issues related to the DataStore category labels Apr 4, 2024
@amruth-movano
Copy link
Author

@ruisebas please see the attached file with verbose output.
amplify_verbose.txt

@amruth-movano
Copy link
Author

amruth-movano commented Apr 4, 2024

@ruisebas , We are saving the data directly to dynamodb using datastore.

public final class SampleDataStore: SampleDataStorable {

    private let dataStore: DataStoreBaseBehavior
    private var userId: GetUserId

    init(dataStore: DataStoreBaseBehavior, userId: @escaping GetUserId) {
        self.dataStore = dataStore
        self.userId = userId
    }

    public convenience init() {
        self.init(
            dataStore: Amplify.DataStore,
            userId: { try await Amplify.Auth.getCurrentUser().userId }
        )
    } 
    public func saveSampleData(_ value: DataStore.SampleData) async throws {
        let userId = try await userId()
        let telemetry = SampleDataMapper.map(value, userId: userId)

        _ = try await dataStore.save(telemetry, where: nil)
    }
}

@ruisebas
Copy link
Member

ruisebas commented Apr 8, 2024

Hi @amruth-movano, thank for all the information. I was able to reproduce the crash and I'm working on a fix for it.

However, please note that setting a predicate condition on a create operation (i.e. calling save with a model that does not exist) is not supported and it will result in a failure. You can only set conditions on update operations (i.e. calling save with a model that already exist).

@5d
Copy link
Member

5d commented Apr 17, 2024

The DataStore feature is designed to enable data accessibility even when the device is not connected to the internet or has limited connectivity. When you create model instances using DataStore.save, it does not directly create the item in the DynamoDB table. Instead, it saves the data to a local SQLite database first, and then synchronizes it with the DynamoDB table later.
If you want to apply conditional checks while creating items directly in the DynamoDB table, you can use the API plugin provided by Amplify.

Amplify.API.mutate(.mutation(...))

@5d
Copy link
Member

5d commented Apr 18, 2024

The fix for multiple withCheckedContinuation invocations is released in v2.29.1. This issue is closed for now, but feel free to reopen it if the problem persists.

@5d 5d closed this as completed Apr 18, 2024
Copy link
Contributor

This issue is now closed. Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

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

No branches or pull requests

3 participants