Docs 菜单

Docs 主页开发应用程序Atlas Device SDKs

自定义用户数据 - .NET SDK

在此页面上

  • 概述
  • 读取用户的自定义用户数据
  • 创建用户的自定义用户数据文档
  • 更新用户的自定义数据
  • 删除用户的自定义数据

您可以在 Realm 中存储有关用户的任意自定义用户数据。例如,您可以存储用户的首选语言、出生日期或当地时区。在写入和读取此数据之前,您必须在后端启用自定义用户数据。要了解更多信息,请参阅启用自定义用户数据。

重要

要使用自定义用户数据,必须首先启用自定义用户数据。

要创建、更新或删除自定义用户数据,您需要从自定义用户数据配置中获取以下信息:

  • 自定义用户数据集群

  • 自定义用户数据数据库

  • 存储自定义用户数据文档的collection

  • 用于将自定义用户数据文档映射到用户的用户 ID 字段(通过用户 ID)

您可以在 App Services 用户界面中找到此信息。 在左侧边栏中,单击 App Users ,然后选择Custom User Data标签页。

您可以通过对已登录用户的 User对象调用GetCustomData()方法来检索自定义用户数据:

await user.RefreshCustomDataAsync();
// Tip: define a class that represents the custom data
// and use the gerneic overload of GetCustomData<>()
var customUserData = user.GetCustomData<CustomUserData>();
Console.WriteLine($"User has pets: {customUserData.HasPets}");
Console.WriteLine($"User's favorite color is {customUserData.FavoriteColor}");
Console.WriteLine($"User's timezone is {customUserData.LocalTimeZone}");

警告

自定义数据可能已过时

当基础数据发生变化时,App Services 不会立即动态更新CustomData的值。相反,每当用户刷新其访问令牌或您显式调用RefreshCustomDataAsync()方法时,App Services 都会获取最新版本的自定义用户数据,从而确保您的应用拥有最新的自定义用户数据。

要为用户创建自定义用户数据,请在自定义用户数据集合中创建 MongoDB 文档。 文档的用户 ID 字段必须包含用户的用户 ID。 以下示例使用MongoDB 数据访问将包含当前登录用户的用户 ID 和多个自定义属性的文档插入到自定义用户数据集合中:

app = App.Create(myRealmAppId);
user = await app.LogInAsync(Credentials.Anonymous());
mongoClient = user.GetMongoClient("mongodb-atlas");
dbTracker = mongoClient.GetDatabase("tracker");
userDataCollection = dbTracker.GetCollection<CustomUserData>("user_data");
var cud = new CustomUserData(user.Id)
{
FavoriteColor = "pink",
LocalTimeZone = "+8",
HasPets = true
};
var insertResult = await userDataCollection.InsertOneAsync(cud);

您可能会发现,创建表示自定义用户数据对象的 C# 类 (POCO) 很有帮助。 在写入、读取和更新属性时,SDK 会将此类与 BSON 进行序列化/反序列化。 上面的示例使用以下类来映射属性:

public class CustomUserData
{
public string _id { get; private set; }
public string _partition { get; private set; }
public string FavoriteColor { get; set; }
public string LocalTimeZone { get; set; }
public bool HasPets { get; set; }
public CustomUserData(string id, string partition = "myPart")
{
this._id = id;
this._partition = partition;
}
}

更新自定义用户数据使用与写入相同的代码。以下示例使用UpdateOneAsync()方法查找和更新数据,然后刷新数据以确保最新更改可用:

var updateResult = await userDataCollection.UpdateOneAsync(
new BsonDocument("_id", user.Id),
new BsonDocument("$set", new BsonDocument("HasPets", false)));
await user.RefreshCustomDataAsync();
var customUserData = user.GetCustomData<CustomUserData>();
Console.WriteLine($"User has pets: {customUserData.HasPets}");
Console.WriteLine($"User's favorite color is {customUserData.FavoriteColor}");
Console.WriteLine($"User's timezone is {customUserData.LocalTimeZone}");

删除自定义用户数据使用与写入和更新相同的方法。以下示例使用DeleteOneAsync()方法查找并删除数据:

var deleteResult = await userDataCollection.DeleteOneAsync(
new BsonDocument("_id", user.Id));
// The `DeletedCount` should be 1
Console.WriteLine(deleteResult.DeletedCount);
// There should no longer be a custom user document for the user
var customData = await userDataCollection.FindOneAsync(
new BsonDocument("_id", user.Id));
Console.WriteLine(customData == null);
← 对用户进行身份验证 - .NET SDK