自定义用户数据 — 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 } } } }
警告
自定义数据可能已过时
注意
如果您需要最新版本的自定义用户数据,请使用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 Access 、 Atlas Device Sync 、 MongoDB Compass或MongoDB Atlas 数据浏览器更新自定义用户数据。
要使用 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 用户界面中查找该用户。