Skip to content

Comments

Implements SecureStorage.GetOrSetAsync(key, value?) (for #27966)#28565

Open
scarletquasar wants to merge 5 commits intodotnet:mainfrom
scarletquasar:main
Open

Implements SecureStorage.GetOrSetAsync(key, value?) (for #27966)#28565
scarletquasar wants to merge 5 commits intodotnet:mainfrom
scarletquasar:main

Conversation

@scarletquasar
Copy link

@scarletquasar scarletquasar commented Mar 22, 2025

Description of Change

This PR addresses a recently created issue that suggests the creation of the method SecureStorage.GetOrSetAsync(key, value?) that is focused in providing an easy interface to work with various operations that involve retrieving data from SecureStorage when there is not guarantee that the data already exists, allowing the developer to set a default value directly inside the operation chain instead of creating if statements and checks.

Usage example:

var tasks = new List<Task>
{
    SecureStorage.Default.GetOrSetAsync("user_email", ExternalBackendService.GetUserEmail()),
    SecureStorage.Default.GetOrSetAsync("user_id", ExternalBackendService.GetUserId()),
};

var result = await Task.WhenAll(tasks);
SomeStateFunction(result);

Current (unoptimized) way to achieve the same result today:

var userEmail = await SecureStorage.Default.GetAsync("user_email");
var userId = await SecureStorage.Default.GetAsync("user_id");

if (userEmail is null)
{
    var thisUserEmail = ExternalBackendService.GetUserEmail();
    await SecureStorage.Default.SetAsync("user_email", thisUserEmail);
    userEmail = thisUserEmail;
}

if (userId is null)
{
    var thisUserId = ExternalBackendService.GetUserIdl();
    await SecureStorage.Default.SetAsync("user_email", thisUserId);
    userId = thisUserId;
}
SomeStateFunction(userEmail);
SomeOtherStateFunction(userId);

Public API Changes

string result = await SecureStorage.[Profile].GetOrSetAsync("key", "value"); //New Api

Issues Fixed

#27966 - To https://github.com/dotnet/maui/milestone/14

Extra Statements

  • I am not familiarized with the public API files present in that repository, so I basically updated all Shipped files with the public API changes, I didn't find more documentation about how properly deal with that and I can change my PR based on the next comments;
  • I have added basic tests that ensure the basic functionality of the new method, but if needed, I'd love to add new tests such as race condition specific verifications and other possible suggestions;
  • A little more details can be found in the issue itself, but feel free to discuss anything you want (as a reviewer) here!

@scarletquasar scarletquasar requested a review from a team as a code owner March 22, 2025 20:11
@dotnet-policy-service dotnet-policy-service bot added the community ✨ Community Contribution label Mar 22, 2025
@scarletquasar
Copy link
Author

@dotnet-policy-service agree

@mattleibow mattleibow added the area-essentials Essentials: Device, Display, Connectivity, Secure Storage, Sensors, App Info label Mar 23, 2025
@jsuarezruiz
Copy link
Contributor

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@rmarinho
Copy link
Member

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-essentials Essentials: Device, Display, Connectivity, Secure Storage, Sensors, App Info community ✨ Community Contribution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants