문서 메뉴
문서 홈
/ /
Atlas Device SDK
/ /

사용자 지정 사용자 데이터 - 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는 다음에 백엔드를 호출할 때 토큰을 새로 고칩니다. 사용자 지정 사용자 데이터는 백엔드에 대한 다음 SDK 호출이 발생할 때까지의 시간을 더한 최대 30 분 동안 부실 상태가 될 수 있습니다.

참고

최신 버전의 사용자 지정 사용자 데이터가 필요한 경우 refreshCustomDataWithCompletion 메서드를 사용하여 최신 버전의 사용자 지정 데이터를 요청하세요.

사용자에 대한 사용자 지정 사용자 데이터를 만들려면 사용자 지정 사용자 데이터 컬렉션에서 MongoDB 문서를 만듭니다. 문서의 사용자 ID 필드에는 사용자의 사용자 ID가 포함되어야 합니다.

Realm UI에서 Custom User Data 탭 아래의 App Users 페이지에서 다음을 포함한 사용자 지정 사용자 데이터 설정을 찾고 구성합니다.

  • 사용자 지정 사용자 데이터 클러스터, 데이터베이스 및 컬렉션

  • 사용자 지정 사용자 데이터 문서를 사용자에게 매핑하는 데 사용되는 사용자 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 Data Explorer를 사용하여 사용자 지정 사용자 데이터를 업데이트할 수 있습니다.

MongoDB Data Access를 사용하여 사용자의 사용자 지정 사용자 데이터를 업데이트하려면 사용자 ID 필드에 사용자의 사용자 ID가 포함된 MongoDB 문서를 편집합니다. 다음 예제에서는 MongoDB Data Access 를 사용하여 사용자 지정 사용자 데이터 컬렉션에서 현재 로그인한 사용자의 사용자 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 속성에 액세스하거나 App Services UI App Users 페이지의 Users 탭 아래에서 사용자를 찾습니다.

돌아가기

사용자 인증

다음

사용자 메타데이터