Class App
- java.lang.Object
-
- io.realm.mongodb.App
-
public class App extends Object
An App is the main client-side entry point for interacting with a MongoDB Realm App.The App can be used to:
- Register uses and perform various user-related operations through authentication providers
(
ApiKeyAuth
,EmailPasswordAuthImpl
) - Synchronize data between the local device and a remote Realm App with Synchronized Realms
- Invoke Realm App functions with
Functions
- Access remote data from MongoDB databases with a
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()
- Register uses and perform various user-related operations through authentication providers
(
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
App.Callback<T>
Callback for async methods available to theApp
.static class
App.Result<T>
Result class representing the result of an async request from this app towards MongoDB Realm.
-
Field Summary
Fields Modifier and Type Field Description static ThreadPoolExecutor
NETWORK_POOL_EXECUTOR
Thread pool used when doing network requests against MongoDB Realm.
-
Constructor Summary
Constructors Constructor Description App(AppConfiguration config)
Constructor for creating an App according to the given AppConfiguration.App(String appId)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method 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 eitherUser.State.LOGGED_IN
orUser.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 sameAppConfiguration.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 theCredentials.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.
-
-
-
Field Detail
-
NETWORK_POOL_EXECUTOR
public static ThreadPoolExecutor NETWORK_POOL_EXECUTOR
Thread pool used when doing network requests against MongoDB Realm.This pool is only exposed for testing purposes and replacing it while the queue is not empty will result in undefined behaviour.
-
-
Constructor Detail
-
App
public App(String appId)
-
App
public App(AppConfiguration config)
Constructor for creating an App according to the given AppConfiguration.- Parameters:
config
- The configuration to use for this App instance.- See Also:
AppConfiguration.Builder
-
-
Method Detail
-
currentUser
@Nullable public User currentUser()
Returns the current user that is logged in and still valid.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.
- Returns:
- current
User
that has logged in and is still valid.null
if no user is logged in or the user has expired.
-
allUsers
public Map<String,User> allUsers()
Returns all known users that are eitherUser.State.LOGGED_IN
orUser.State.LOGGED_OUT
.Only users that at some point logged into this device will be returned.
- Returns:
- a map of user identifiers and users known locally.
-
switchUser
public User switchUser(User user)
Switch current user.The current user is the user returned by
currentUser()
.- Parameters:
user
- the new current user.- Throws:
IllegalArgumentException
- if the user is is notUser.State.LOGGED_IN
.
-
removeUser
public User removeUser(User user) throws AppException
Removes a users credentials from this device. If the user was currently logged in, they will be logged out as part of the process. This is only a local change and does not affect the user state on the server.- Parameters:
user
- to remove- Returns:
- user that was removed.
- Throws:
AppException
- if called from the UI thread or if the user was logged in, but could not be logged out.
-
login
public User login(Credentials credentials) throws AppException
Logs in as a user with the given credentials associated with an authentication provider.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)
.- Parameters:
credentials
- the credentials representing the type of login.- Returns:
- a
User
representing the logged in user. - Throws:
AppException
- if the user could not be logged in.
-
loginAsync
public RealmAsyncTask loginAsync(Credentials credentials, App.Callback<User> callback)
Logs in as a user with the given credentials associated with an authentication provider.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)
.- Parameters:
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.- Throws:
IllegalStateException
- if not called on a looper thread.
-
getEmailPassword
public EmailPasswordAuth getEmailPassword()
Returns a wrapper for interacting with functionality related to users either being created or logged in using theCredentials.Provider.EMAIL_PASSWORD
identity provider.- Returns:
- wrapper for interacting with the
Credentials.Provider.EMAIL_PASSWORD
identity provider.
-
addAuthenticationListener
public void addAuthenticationListener(AuthenticationListener listener)
Sets a global authentication listener that will be notified about User events like login and logout.Callbacks to authentication listeners will happen on the UI thread.
- Parameters:
listener
- listener to register.- Throws:
IllegalArgumentException
- iflistener
isnull
.
-
removeAuthenticationListener
public void removeAuthenticationListener(AuthenticationListener listener)
Removes the provided global authentication listener.- Parameters:
listener
- listener to remove.
-
getSync
public 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.- Returns:
- the Sync instance associated with this App.
-
getFunctions
public Functions getFunctions(User user)
Returns a Functions manager for invoking the Realm App's Realm Functions.This will use the app's default codec registry to encode and decode arguments and results.
-
getFunctions
public 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.- See Also:
Functions
-
getConfiguration
public AppConfiguration getConfiguration()
Returns the configuration object for this app.- Returns:
- the configuration for this app.
-
equals
public boolean equals(Object o)
Two Apps are considered equal and will share their underlying state if they both refer to the sameAppConfiguration.getAppId()
.
-
-