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

fix(DataStore): Reconcile mutation responses from conflict handler path #3372

Merged
merged 1 commit into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@ final class OutgoingMutationQueue: OutgoingMutationQueueBehavior {
api: api,
storageAdapter: storageAdapter,
graphQLResponseError: graphQLResponseError,
apiError: apiError
apiError: apiError,
reconciliationQueue: reconciliationQueue
) { [weak self] result in
guard let self = self else {
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,23 @@ class ProcessMutationErrorFromCloudOperation: AsynchronousOperation {
private let completion: (Result<MutationEvent?, Error>) -> Void
private var mutationOperation: AtomicValue<GraphQLOperation<MutationSync<AnyModel>>?>
private weak var api: APICategoryGraphQLBehavior?

private weak var reconciliationQueue: IncomingEventReconciliationQueue?

init(dataStoreConfiguration: DataStoreConfiguration,
mutationEvent: MutationEvent,
api: APICategoryGraphQLBehavior,
storageAdapter: StorageEngineAdapter,
graphQLResponseError: GraphQLResponseError<MutationSync<AnyModel>>? = nil,
apiError: APIError? = nil,
reconciliationQueue: IncomingEventReconciliationQueue? = nil,
completion: @escaping (Result<MutationEvent?, Error>) -> Void) {
self.dataStoreConfiguration = dataStoreConfiguration
self.mutationEvent = mutationEvent
self.api = api
self.storageAdapter = storageAdapter
self.graphQLResponseError = graphQLResponseError
self.apiError = apiError
self.reconciliationQueue = reconciliationQueue
self.completion = completion
self.mutationOperation = AtomicValue(initialValue: nil)

Expand Down Expand Up @@ -316,9 +319,24 @@ class ProcessMutationErrorFromCloudOperation: AsynchronousOperation {
dataStoreConfiguration.errorHandler(error)
}

if case .success(let response) = cloudResult,
case .failure(let error) = response {
dataStoreConfiguration.errorHandler(error)
if case let .success(graphQLResponse) = cloudResult {
if case .failure(let error) = graphQLResponse {
dataStoreConfiguration.errorHandler(error)
} else if case let .success(graphQLResult) = graphQLResponse {
guard let reconciliationQueue = reconciliationQueue else {
let dataStoreError = DataStoreError.configuration(
"reconciliationQueue is unexpectedly nil",
"""
The reference to reconciliationQueue has been released while an ongoing mutation was being processed.
\(AmplifyErrorMessages.reportBugToAWS())
"""
)
finish(result: .failure(dataStoreError))
return
}

reconciliationQueue.offer([graphQLResult], modelName: mutationEvent.modelName)
}
}

finish(result: .success(nil))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class ProcessMutationErrorFromCloudOperationTests: XCTestCase {
var storageAdapter: StorageEngineAdapter!
var localPost = Post(title: "localTitle", content: "localContent", createdAt: .now())
let queue = OperationQueue()

let reconciliationQueue = MockReconciliationQueue()

override func setUp() {
tryOrFail {
try setUpWithAPI()
Expand Down Expand Up @@ -585,6 +586,7 @@ class ProcessMutationErrorFromCloudOperationTests: XCTestCase {
api: mockAPIPlugin,
storageAdapter: storageAdapter,
graphQLResponseError: graphQLResponseError,
reconciliationQueue: reconciliationQueue,
completion: completion)

queue.addOperation(operation)
Expand Down Expand Up @@ -662,6 +664,7 @@ class ProcessMutationErrorFromCloudOperationTests: XCTestCase {
api: mockAPIPlugin,
storageAdapter: storageAdapter,
graphQLResponseError: graphQLResponseError,
reconciliationQueue: reconciliationQueue,
completion: completion)

queue.addOperation(operation)
Expand Down Expand Up @@ -956,6 +959,7 @@ class ProcessMutationErrorFromCloudOperationTests: XCTestCase {
api: mockAPIPlugin,
storageAdapter: storageAdapter,
graphQLResponseError: graphQLResponseError,
reconciliationQueue: reconciliationQueue,
completion: completion)

queue.addOperation(operation)
Expand Down Expand Up @@ -1035,6 +1039,7 @@ class ProcessMutationErrorFromCloudOperationTests: XCTestCase {
api: mockAPIPlugin,
storageAdapter: storageAdapter,
graphQLResponseError: graphQLResponseError,
reconciliationQueue: reconciliationQueue,
completion: completion)
queue.addOperation(operation)

Expand Down
Loading