“文档” 菜单
文档首页
/ /
Atlas Device SDKs
/ /

自定义用户数据 — Swift SDK

在此页面上

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

您可以通过当前登录用户的 User对象读取该用户的自定义用户数据。 您无法通过User对象编辑自定义用户数据。 要编辑自定义用户数据,请参阅更新自定义用户数据。 要读取数据,请访问已登录用户的User对象上的customData属性:

RLMApp *app = [RLMApp appWithId:YOUR_APP_ID];
[app loginWithCredential:[RLMCredentials anonymousCredentials] completion:^(RLMUser *user, NSError *error) {
if (error != nil) {
NSLog(@"Failed to log in: %@", error);
return;
}
// If the user data has been refreshed recently, you can access the
// custom user data directly on the user object
NSLog(@"User custom data: %@", [user customData]);
// Refresh the custom data
[user refreshCustomDataWithCompletion:^(NSDictionary *customData, NSError *error) {
if (error != nil) {
NSLog(@"Failed to refresh custom user data: %@", error);
return;
}
NSLog(@"Favorite color: %@", customData[@"favoriteColor"]);
}];
}];
let appId = YOUR_APP_SERVICES_APP_ID // replace this with your App ID
let app = App(id: appId)
app.login(credentials: Credentials.anonymous) { (result) in
switch result {
case .failure(let error):
print("Failed to log in: \(error.localizedDescription)")
case .success(let user):
// If the user data has been refreshed recently, you can access the
// custom user data directly on the user object
print("User custom data: \(user.customData)")
// Refresh the custom user data
user.refreshCustomData { (result) in
switch result {
case .failure(let error):
print("Failed to refresh custom data: \(error.localizedDescription)")
case .success(let customData):
// favoriteColor was set on the custom data.
print("Favorite color: \(customData["favoriteColor"] ?? "not set")")
return
}
}
}
}

警告

自定义数据可能已过时

当基础数据发生变化时,Atlas App Services 不会立即动态更新客户端用户自定义数据文档的值。 相反,每当用户刷新其 访问令牌 时,Atlas App Services 都会获取最新版本的自定义用户数据,而大多数联系Atlas App Services 后端的 SDK 操作都会使用该令牌。如果令牌在其默认30分钟到期时间之前未刷新,则 Swift SDK 会在下次调用后端时刷新令牌。 自定义用户数据可能会过时长达30分钟,加上 SDK 下次调用后端之前的时间。

注意

如果您需要最新版本的自定义用户数据,请使用refreshCustomDataWithCompletion方法请求最新版本的用户自定义数据。

要为用户创建自定义用户数据,请在自定义用户数据集合中创建 MongoDB 文档。 文档的用户 ID 字段应包含用户的用户 ID。

提示

在 App Services 用户界面中,检查Custom User Data标签页下的 App Users页面,查找并配置自定义用户数据设置,包括:

  • 自定义用户数据集群、数据库和collection

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

以下示例使用 MongoDB 远程访问将包含当前登录用户的用户 ID 和favoriteColor值的文档插入自定义用户数据集合:

RLMApp *app = [RLMApp appWithId:YOUR_APP_ID];
[app loginWithCredential:[RLMCredentials anonymousCredentials] completion:^(RLMUser *user, NSError *error) {
if (error != nil) {
NSLog(@"Failed to log in: %@", error);
return;
}
RLMMongoClient *client = [user mongoClientWithServiceName:@"mongodb-atlas"];
RLMMongoDatabase *database = [client databaseWithName:@"my_database"];
RLMMongoCollection *collection = [database collectionWithName:@"users"];
[collection insertOneDocument:
@{@"userId": [user identifier], @"favoriteColor": @"pink"}
completion:^(id<RLMBSON> newObjectId, NSError *error) {
if (error != nil) {
NSLog(@"Failed to insert: %@", error);
}
NSLog(@"Inserted custom user data document with object ID: %@", newObjectId);
}];
}];
let appId = YOUR_APP_SERVICES_APP_ID // replace this with your App ID
let app = App(id: appId)
app.login(credentials: Credentials.anonymous) { (result) in
switch result {
case .failure(let error):
print("Failed to log in: \(error.localizedDescription)")
case .success(let user):
let client = user.mongoClient("mongodb-atlas")
let database = client.database(named: "my_database")
let collection = database.collection(withName: "users")
// Insert the custom user data object
collection.insertOne([
"userId": AnyBSON(user.id),
"favoriteColor": "pink"
]) { (result) in
switch result {
case .failure(let error):
print("Failed to insert document: \(error.localizedDescription)")
case .success(let newObjectId):
print("Inserted custom user data document with object ID: \(newObjectId)")
}
}
}
}

在创建自定义用户数据文档时,您可以向其中添加任意数量的字段和值。用户 ID 字段是文档要在User对象上作为自定义用户数据使用的唯一要求。

您可以使用MongoDB Data AccessAtlas Device SyncMongoDB CompassMongoDB Atlas Data Explorer 更新自定义用户数据。

要使用 MongoDB 数据访问更新用户的自定义用户数据,请编辑其用户 ID 字段包含该用户的用户 ID 的 MongoDB 文档。 以下示例使用MongoDB 数据访问来更新包含自定义用户数据集合中当前登录用户的用户 ID 的文档的favoriteColor字段:

RLMApp *app = [RLMApp appWithId:YOUR_APP_ID];
[app loginWithCredential:[RLMCredentials anonymousCredentials] completion:^(RLMUser *user, NSError *error) {
if (error != nil) {
NSLog(@"Failed to log in: %@", error);
return;
}
RLMMongoClient *client = [user mongoClientWithServiceName:@"mongodb-atlas"];
RLMMongoDatabase *database = [client databaseWithName:@"my_database"];
RLMMongoCollection *collection = [database collectionWithName:@"users"];
// Update the user's custom data document
[collection updateOneDocumentWhere:@{@"userId": [user identifier]}
updateDocument: @{@"favoriteColor": @"cerulean"}
completion:^(RLMUpdateResult *updateResult, NSError *error) {
if (error != nil) {
NSLog(@"Failed to insert: %@", error);
}
NSLog(@"Matched: %lu, modified: %lu", [updateResult matchedCount], [updateResult modifiedCount]);
}];
}];
let appId = YOUR_APP_SERVICES_APP_ID // replace this with your App ID
let app = App(id: appId)
app.login(credentials: Credentials.anonymous) { (result) in
switch result {
case .failure(let error):
print("Failed to log in: \(error.localizedDescription)")
case .success(let user):
// Access the custom user document remotely to update it.
let client = user.mongoClient("mongodb-atlas")
let database = client.database(named: "my_database")
let collection = database.collection(withName: "users")
collection.updateOneDocument(
filter: ["userId": AnyBSON(user.id)],
update: ["favoriteColor": "cerulean"]
) { (result) in
switch result {
case .failure(let error):
print("Failed to update: \(error.localizedDescription)")
return
case .success(let updateResult):
// User document updated.
print("Matched: \(updateResult.matchedCount), updated: \(updateResult.modifiedCount)")
}
}
}
}

提示

要确定用户的 ID,请访问User.id属性或在Users标签页下App Users页面上的 App Services 用户界面中查找该用户。

后退

验证用户身份

来年

用户元数据