Docs Menu
Docs Home
/ /
Atlas Device SDKs
/ /

Custom User Data - Swift SDK

On this page

  • Read a User's Custom Data
  • Create a User's Custom Data Document
  • Update a User's Custom Data

You can read the custom user data of a currently logged-in user through that user's User object. You cannot edit custom user data through a User object. To edit custom user data, see Update Custom User Data. To read the data, access the customData property on the User object of a logged-in user:

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
}
}
}
}

Warning

Custom Data May Be Stale

Atlas App Services does not dynamically update the value of the client-side user custom data document immediately when underlying data changes. Instead, App Services fetches the most recent version of custom user data whenever a user refreshes their access token, which is used by most SDK operations that contact the App Services backend. If the token is not refreshed before its default 30 minute expiration time, the Swift SDK refreshes the token on the next call to the backend. Custom user data could be stale for up to 30 minutes plus the time until the next SDK call to the backend occurs.

Note

If you require the most recent version of custom user data, use the refreshCustomDataWithCompletion method to request the latest version of a user's custom data.

To create custom user data for a user, create a MongoDB document in the custom user data collection. The user ID field of the document should contain the the user's user ID.

Tip

In the App Services UI, check the App Users page under the Custom User Data tab to find and configure custom user data settings, including:

  • The custom user data cluster, database, and collection

  • The user ID field used to map custom user data documents to users

The following example uses MongoDB Remote Access to insert a document containing the user ID of the currently logged in user and a favoriteColor value into the custom user data collection:

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)")
}
}
}
}

You can add any number of arbitrary fields and values to the custom user data document when you create it. The user ID field is the only requirement for the document to become available on the User object as custom user data.

You can update custom user data using MongoDB Data Access, Atlas Device Sync, MongoDB Compass, or the MongoDB Atlas Data Explorer.

To update a user's custom user data with MongoDB Data Access, edit the MongoDB document whose user ID field contains the user ID of the user. The following example uses MongoDB Data Access to update the favoriteColor field of the the document containing the user ID of the currently logged in user in the custom user data collection:

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)")
}
}
}
}

Tip

To determine a user's ID, access the User.id property or find the user in the App Services UI on the App Users page under the Users tab.

Back

Authenticate Users