User Metadata

With a user metadata, you can store additional information to a user. The user metadata is used to store a NSDictionary of NSString key-value pairs in a SBDUser instance. Use cases for metadata are such as a phone number, an email or a long description of the user, which can each be fetched and rendered into the UI.


Create, update, retrieve, and delete a user metadata

A user metadata is a <NSString *, NSString *> NSDictionary that is stored into a user. Its uses are very flexible, allowing you to customize a channel to fit you and your users' needs.

Create

To store a metadata into a user, create a <NSString *, NSString *> NSDictionary, and then pass it as an argument when calling createMetaData:completionHandler: method. You can store multiple key-value pairs in the dictionary.

SBDUser *user = [SBDMain getCurrentUser];

NSDictionary *data = @{
        @"phone": @"001-0002-0003",
        @"email": @"sales@sendbird.com",
        };

[user createMetaData:data completionHandler:^(NSDictionary<NSString *,NSString *> * _Nullable metaData, SBDError * _Nullable error) {
    if (error != nil) {    // Error. 
        return;
    }
}];
let user = SBDMain.getCurrentUser()!

let data = [
    "phone": "001-0002-0003",
    "email": "sales@sendbird.com"
]

user.createMetaData(data) { (metaData, error) in
    guard error == nil else {    // Error.
        return
    }
}

Update

The process for updating a user metadata is identical with the creation. Values are updated for existing keys, while new key-value pairs added.

SBDUser *user = [SBDMain getCurrentUser];

NSDictionary *data = @{
        @"phone": @"001-0001-0004",
        @"email": @"engineering@sendbird.com",
        };

[user updateMetaData:data completionHandler:^(NSDictionary<NSString *,NSString *> * _Nullable metaData, SBDError * _Nullable error) {
    if (error != nil) {    // Error. 
        return;
    }
}];
let user = SBDMain.getCurrentUser()!

let data = [
    "phone": "001-0001-0004",
    "email": "engineering@sendbird.com"
]

user.updateMetaData(data) { (metaData, error) in
    guard error == nil else {    // Error.
        return
    }
}

Retrieve

To get the stored user metadata, read the metadata property of a SBDUser instance.

SBDUser *user = [SBDMain getCurrentUser];
NSDictionary *metaData = user.metaData;
let user = SBDMain.getCurrentUser()!
let metaData = user.metaData

Delete

The process for deleting a user metadata is as simple as creating one.

SBDUser *user = [SBDMain getCurrentUser];

[user deleteMetaDataWithKey:@"phone" completionHandler:^(SBDError * _Nullable error) {
    if (error != nil) {    // Error.
        return;
    }
}];
let user = SBDMain.getCurrentUser()!

user.deleteMetaData(withKey: "phone") { (error) in
    guard error == nil else {    // Error. 
        return
    }
}