diff --git a/src/CommunityToolkit.Datasync.Client/Offline/OperationsQueue/OperationsQueueManager.cs b/src/CommunityToolkit.Datasync.Client/Offline/OperationsQueue/OperationsQueueManager.cs index ba2211c3..c278b17b 100644 --- a/src/CommunityToolkit.Datasync.Client/Offline/OperationsQueue/OperationsQueueManager.cs +++ b/src/CommunityToolkit.Datasync.Client/Offline/OperationsQueue/OperationsQueueManager.cs @@ -397,8 +397,13 @@ internal async Task PushAsync(IEnumerable entityTypes, PushOpt if (operation.Kind != OperationKind.Delete) { _ = response.ContentStream.Seek(0L, SeekOrigin.Begin); // Reset the memory stream to the beginning. - object? newValue = JsonSerializer.Deserialize(response.ContentStream, entityType, DatasyncSerializer.JsonSerializerOptions); - object? oldValue = await this._context.FindAsync(entityType, [operation.ItemId], cancellationToken).ConfigureAwait(false); + object? newValue = await JsonSerializer.DeserializeAsync( + response.ContentStream, + entityType, + DatasyncSerializer.JsonSerializerOptions, + cancellationToken); + + object? oldValue = await FindOldValue(operation, entityType, cancellationToken); ReplaceDatabaseValue(oldValue, newValue); } @@ -410,6 +415,32 @@ internal async Task PushAsync(IEnumerable entityTypes, PushOpt return null; } + /// + /// Internal helper - find the old value for a datasync operation and an entity type. + /// + /// The datasync operation. + /// The entity type. + /// A to observe. + /// The object associated with the datasync operation, or null. + internal async ValueTask FindOldValue(DatasyncOperation operation, Type entityType, CancellationToken cancellationToken) + { + this.pushlock.Enter(); + try + { + object? oldValue = await this._context + .FindAsync( + entityType, + [operation.ItemId], + cancellationToken) + .ConfigureAwait(false); + return oldValue; + } + finally + { + this.pushlock.Exit(); + } + } + /// /// Internal helper - replaces an old value of an entity in the database with a new value. ///