Skip to content

Conversation

@618coffee
Copy link
Contributor

@618coffee 618coffee commented Nov 3, 2020

Description of changes:

The existing implementation of Amplify.reset() is to call reset() on every Categories and put them in global DispatchGroup which might cause a race condition. For example, during the DataStore reset process, it depends on Hub to dispatch some event, but at that point, Hub might already been reset and cause a precondition failure on https://github.com/aws-amplify/amplify-ios/blob/main/Amplify/Categories/Hub/HubCategory.swift#L44

The change I made is:
reset() on logging and hub categories should be called when other categories finish resetting.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@618coffee 618coffee self-assigned this Nov 3, 2020
Copy link
Contributor

@diegocstn diegocstn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we maybe use the partition(by:) method to get a partitioned array with .logging and .hub cases at the end?

let group = DispatchGroup()

for categoryType in CategoryType.allCases {
let manuallyResetCategories = [CategoryType.logging, .hub]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: let manuallyResetCategories: [CategoryType] = [.logging, .hub]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Synced offline. You can take a look again.

Comment on lines 39 to 41
case .hub, .logging:
// should be waited until other finish resetting
break
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this class has a lot of hard coding logging and hub categories, maybe you can isolate these two categories that you can reuse in the rest of the code, like

let resetLastCategories = [CategoryType.hub, .logging]

then you can filter before line 25 by let resetFirstCategories = CategoryType.allCases.filter { !resetLastCategories.contains($0) }

With this, would you be able to avoid empty switch cases, similar to code like this?

for categoryType in resetFirstCategories {
    reset(categoryType.category, in: group) { group.leave() }
}

group.wait()

for categoryType in resetLastCategories {
    reset(categoryType.category, in: group) { group.leave() }
}
group.wait()


@618coffee 618coffee merged commit 582c7dd into main Nov 4, 2020
@618coffee 618coffee deleted the amplify/reset branch November 4, 2020 01:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants