public class App extends Object
The App can be used to:
ApiKeyAuth
, EmailPasswordAuthImpl
)Functions
MongoClient
To create an app that is linked with a remote Realm App initialize Realm and configure the App as shown below:
class MyApplication extends Application { App APP; // The App instance should be a global singleton \@Override public void onCreate() { super.onCreate(); Realm.init(this); AppConfiguration appConfiguration = new AppConfiguration.Builder(BuildConfig.MONGODB_REALM_APP_ID) .appName(BuildConfig.VERSION_NAME) .appVersion(Integer.toString(BuildConfig.VERSION_CODE)) .build(); APP = new App(appConfiguration); } }
After configuring the App you can start managing users, configure Synchronized Realms, call remote Realm Functions and access remote data through Mongo Collections. The examples below show the synchronized APIs which cannot be used from the main thread. For the equivalent asynchronous counterparts. The example project in please see https://github.com/realm/realm-java/tree/v10/examples/mongoDbRealmExample.
To register a new user and/or login with an existing user do as shown below:
// Register new user APP.getEmailPassword().registerUser(username, password); // Login with existing user User user = APP.login(Credentials.emailPassword(username, password))
With an authorized user you can synchronize data between the local device and the remote Realm
App by opening a Realm with a SyncConfiguration
as indicated below:
SyncConfiguration syncConfiguration = new SyncConfiguration.Builder(user, "<partition value>") .build(); Realm instance = Realm.getInstance(syncConfiguration); SyncSession session = APP.getSync().getSession(syncConfiguration); instance.executeTransaction(realm -> { realm.insert(...); }); session.uploadAllLocalChanges(); instance.close();
You can call remote Realm functions as shown below:
Functions functions = user.getFunctions(); Integer sum = functions.callFunction("sum", Arrays.asList(1, 2, 3, 4), Integer.class);
And access collections from the remote Realm App as shown here:
MongoClient client = user.getMongoClient(SERVICE_NAME) MongoDatabase database = client.getDatabase(DATABASE_NAME) MongoCollection<DocumentT> collection = database.getCollection(COLLECTION_NAME); Long count = collection.count().get()
Modifier and Type | Class and Description |
---|---|
static interface |
App.Callback<T>
Callback for async methods available to the
App . |
static class |
App.Result<T>
Result class representing the result of an async request from this app towards MongoDB Realm.
|
Modifier and Type | Field and Description |
---|---|
static ThreadPoolExecutor |
NETWORK_POOL_EXECUTOR
Thread pool used when doing network requests against MongoDB Realm.
|
Constructor and Description |
---|
App(AppConfiguration config)
Constructor for creating an App according to the given AppConfiguration.
|
App(String appId) |
Modifier and Type | Method and Description |
---|---|
void |
addAuthenticationListener(AuthenticationListener listener)
Sets a global authentication listener that will be notified about User events like
login and logout.
|
Map<String,User> |
allUsers()
Returns all known users that are either
User.State.LOGGED_IN or
User.State.LOGGED_OUT . |
User |
currentUser()
Returns the current user that is logged in and still valid.
|
boolean |
equals(Object o)
Two Apps are considered equal and will share their underlying state if they both refer
to the same
AppConfiguration.getAppId() . |
AppConfiguration |
getConfiguration()
Returns the configuration object for this app.
|
EmailPasswordAuth |
getEmailPassword()
Returns a wrapper for interacting with functionality related to users either being created or
logged in using the
Credentials.Provider.EMAIL_PASSWORD identity provider. |
Functions |
getFunctions(User user)
Returns a Functions manager for invoking the Realm App's Realm Functions.
|
Functions |
getFunctions(User user,
CodecRegistry codecRegistry)
Returns a Functions manager for invoking the Realm App's Realm Functions with a custom
codec registry for encoding and decoding arguments and results.
|
Sync |
getSync()
Returns the Sync instance managing the ongoing Realm Sync sessions
synchronizing data between the local and the remote Realm App associated with this app.
|
int |
hashCode() |
User |
login(Credentials credentials)
Logs in as a user with the given credentials associated with an authentication provider.
|
RealmAsyncTask |
loginAsync(Credentials credentials,
App.Callback<User> callback)
Logs in as a user with the given credentials associated with an authentication provider.
|
void |
removeAuthenticationListener(AuthenticationListener listener)
Removes the provided global authentication listener.
|
User |
removeUser(User user)
Removes a users credentials from this device.
|
User |
switchUser(User user)
Switch current user.
|
public static ThreadPoolExecutor NETWORK_POOL_EXECUTOR
This pool is only exposed for testing purposes and replacing it while the queue is not empty will result in undefined behaviour.
public App(String appId)
public App(AppConfiguration config)
config
- The configuration to use for this App instance.AppConfiguration.Builder
public User currentUser()
A user is invalidated when he/she logs out or the user's refresh token expires or is revoked.
If two or more users are logged in, it is the last valid user that is returned by this method.
User
that has logged in and is still valid. null
if no
user is logged in or the user has expired.public Map<String,User> allUsers()
User.State.LOGGED_IN
or
User.State.LOGGED_OUT
.
Only users that at some point logged into this device will be returned.
public User switchUser(User user)
The current user is the user returned by currentUser()
.
user
- the new current user.IllegalArgumentException
- if the user is is not User.State.LOGGED_IN
.public User removeUser(User user) throws AppException
user
- to removeAppException
- if called from the UI thread or if the user was logged in, but
could not be logged out.public User login(Credentials credentials) throws AppException
The user who logs in becomes the current user. Other App functionality acts on behalf of the current user.
If there was already a current user, that user is still logged in and can be found in the
list returned by allUsers()
.
It is also possible to switch between which user is considered the current user by using
switchUser(User)
.
credentials
- the credentials representing the type of login.User
representing the logged in user.AppException
- if the user could not be logged in.public RealmAsyncTask loginAsync(Credentials credentials, App.Callback<User> callback)
The user who logs in becomes the current user. Other App functionality acts on behalf of the current user.
If there was already a current user, that user is still logged in and can be found in the
list returned by allUsers()
.
It is also possible to switch between which user is considered the current user by using
switchUser(User)
.
credentials
- the credentials representing the type of login.callback
- callback when logging in has completed or failed. The callback will always
happen on the same thread as this method is called on.IllegalStateException
- if not called on a looper thread.public EmailPasswordAuth getEmailPassword()
Credentials.Provider.EMAIL_PASSWORD
identity provider.Credentials.Provider.EMAIL_PASSWORD
identity provider.public void addAuthenticationListener(AuthenticationListener listener)
Callbacks to authentication listeners will happen on the UI thread.
listener
- listener to register.IllegalArgumentException
- if listener
is null
.public void removeAuthenticationListener(AuthenticationListener listener)
listener
- listener to remove.public Sync getSync()
public Functions getFunctions(User user)
This will use the app's default codec registry to encode and decode arguments and results.
public Functions getFunctions(User user, CodecRegistry codecRegistry)
Functions
public AppConfiguration getConfiguration()
public boolean equals(Object o)
AppConfiguration.getAppId()
.