Realm Xamarin  v0.81.0
Realm for Xamarin
Public Member Functions | Static Public Member Functions | Public Attributes | Properties | Events | List of all members
Realms.Realm Class Reference

A Realm instance (also referred to as a realm) represents a Realm database. More...

Inherits IDisposable.

Public Member Functions

void Add (RealmObject obj, bool update=false)
 This realm will start managing a RealmObject which has been created as a standalone object. More...
 
Add< T > (T obj, bool update=false)
 This realm will start managing a RealmObject which has been created as a standalone object. More...
 
IQueryable< dynamic > All (string className)
 Get a view of all the objects of a particular type. More...
 
IQueryable< T > All< T > ()
 Extract an iterable set of objects for direct use or further query. More...
 
Transaction BeginWrite ()
 Factory for a write Transaction. Essential object to create scope for updates. More...
 
void Close ()
 Closes the Realm if not already closed. Safe to call repeatedly. Note that this will close the file. Other references to the same database on the same thread will be invalidated. More...
 
dynamic CreateObject (string className)
 Factory for a managed object in a realm. Only valid within a Write transaction. More...
 
CreateObject< T > ()
 Factory for a managed object in a realm. Only valid within a Write transaction. More...
 
void Dispose ()
 Dispose automatically closes the Realm if not already closed. More...
 
override bool Equals (object obj)
 Generic override determines whether the specified object is equal to the current Realm. More...
 
bool Equals (Realm other)
 Determines whether the specified Realm is equal to the current Realm. More...
 
RealmObject Find (string className, long? primaryKey)
 Fast lookup of an object for dynamic use, from a class which has a PrimaryKey property. More...
 
RealmObject Find (string className, string primaryKey)
 Fast lookup of an object for dynamic use, from a class which has a PrimaryKey property. More...
 
Find< T > (long? primaryKey)
 Fast lookup of an object from a class which has a PrimaryKey property. More...
 
Find< T > (string primaryKey)
 Fast lookup of an object from a class which has a PrimaryKey property. More...
 
override int GetHashCode ()
 Serves as a hash function for a Realm based on the core instance. More...
 
bool IsSameInstance (Realm other)
 Determines whether this instance is the same core instance as the passed in argument. More...
 
void Manage< T > (T obj, bool update=false)
 This realm will start managing a RealmObject which has been created as a standalone object. More...
 
RealmObject ObjectForPrimaryKey (string className, long id)
 Fast lookup of an object for dynamic use, from a class which has a PrimaryKey property. More...
 
RealmObject ObjectForPrimaryKey (string className, string id)
 Fast lookup of an object for dynamic use, from a class which has a PrimaryKey property. More...
 
ObjectForPrimaryKey< T > (long id)
 Fast lookup of an object from a class which has a PrimaryKey property. More...
 
ObjectForPrimaryKey< T > (string id)
 Fast lookup of an object from a class which has a PrimaryKey property. More...
 
delegate void RealmChangedEventHandler (object sender, EventArgs e)
 Handler type used by RealmChanged More...
 
bool Refresh ()
 Update a Realm and outstanding objects to point to the most recent data for this Realm. This is only necessary when you have a Realm on a thread without a runloop that needs manual refreshing. More...
 
void Remove (RealmObject obj)
 Removes a persistent object from this realm, effectively deleting it. More...
 
void RemoveAll (string className)
 Remove all objects of a type from the realm. More...
 
void RemoveAll ()
 Remove all objects of all types managed by this realm. More...
 
void RemoveAll< T > ()
 Remove all objects of a type from the realm. More...
 
void RemoveRange< T > (IQueryable< T > range)
 Remove objects matching a query from the realm. More...
 
void Write (Action action)
 Execute an action inside a temporary transaction. If no exception is thrown, the transaction will automatically be committed. More...
 
Task WriteAsync (Action< Realm > action)
 Execute an action inside a temporary transaction on a worker thread. If no exception is thrown, the transaction will automatically be committed. More...
 

Static Public Member Functions

static bool Compact (RealmConfigurationBase config=null)
 Compacts a Realm file. A Realm file usually contains free/unused space. This method removes this free space and the file size is thereby reduced. Objects within the Realm file are untouched. More...
 
static void DeleteRealm (RealmConfigurationBase configuration)
 Deletes all the files associated with a realm. Hides knowledge of the auxiliary filenames from the programmer. More...
 
static Realm GetInstance (string databasePath)
 Factory for a Realm instance for this thread. More...
 
static Realm GetInstance (RealmConfigurationBase config=null)
 Factory for a Realm instance for this thread. More...
 

Public Attributes

bool IsClosed => SharedRealmHandle.IsClosed
 Checks if database has been closed. More...
 

Properties

RealmConfigurationBase Config [get]
 Gets the RealmConfigurationBase that controls this realm's path and other settings. More...
 
RealmChangedEventHandler RealmChanged
 Triggered when a realm has changed (i.e. a transaction was committed). More...
 
RealmSchema Schema [get]
 Gets the RealmSchema instance that describes all the types that can be stored in this Realm. More...
 

Events

EventHandler< ErrorEventArgsError
 Triggered when a Realm-level exception has occurred. More...
 

Detailed Description

A Realm instance (also referred to as a realm) represents a Realm database.

Warning: Realm instances are not thread safe and can not be shared across threads You must call GetInstance on each thread in which you want to interact with the realm.

Member Function Documentation

§ Add()

void Realms.Realm.Add ( RealmObject  obj,
bool  update = false 
)

This realm will start managing a RealmObject which has been created as a standalone object.

Parameters
objMust be a standalone object, null not allowed.
updateIf true, and an object with the same primary key already exists, performs an update.
Exceptions
RealmInvalidTransactionExceptionIf you invoke this when there is no write Transaction active on the realm.
RealmObjectManagedByAnotherRealmExceptionYou can't manage an object with more than one realm

If the object is already managed by this realm, this method does nothing. This method modifies the object in-place, meaning that after it has run, obj will be managed. Cyclic graphs (Parent has Child that has a Parent) will result in undefined behavior. You have to break the cycle manually and assign relationships after all object have been managed.

References Realms.RealmObject.IsManaged, and Realms.RealmObject.Realm.

Referenced by Realms.Realm.Manage< T >().

§ Add< T >()

T Realms.Realm.Add< T > ( obj,
bool  update = false 
)

This realm will start managing a RealmObject which has been created as a standalone object.

Template Parameters
TThe Type T must not only be a RealmObject but also have been processed by the Fody weaver, so it has persistent properties.
Parameters
objMust be a standalone object, null not allowed.
updateIf true, and an object with the same primary key already exists, performs an update.
Exceptions
RealmInvalidTransactionExceptionIf you invoke this when there is no write Transaction active on the realm.
RealmObjectManagedByAnotherRealmExceptionYou can't manage an object with more than one realm

If the object is already managed by this realm, this method does nothing. This method modifies the object in-place, meaning that after it has run, obj will be managed. Returning it is just meant as a convenience to enable fluent syntax scenarios. Cyclic graphs (Parent has Child that has a Parent) will result in undefined behavior. You have to break the cycle manually and assign relationships after all object have been managed.

Returns
The passed object, so that you can write var person = realm.Add(new Person { Id = 1 });
Type Constraints
T :RealmObject 

§ All()

IQueryable<dynamic> Realms.Realm.All ( string  className)

Get a view of all the objects of a particular type.

Parameters
classNameThe type of the objects as defined in the schema.

Because the objects inside the view are accessed dynamically, the view cannot be queried into using LINQ or other expression predicates.

Returns
A queryable collection that without further filtering, allows iterating all objects of className, in this realm.

Referenced by Realms.Realm.RemoveAll().

§ All< T >()

IQueryable<T> Realms.Realm.All< T > ( )

Extract an iterable set of objects for direct use or further query.

Template Parameters
TThe Type T must be a RealmObject.
Returns
A queryable collection that without further filtering, allows iterating all objects of class T, in this realm.
Type Constraints
T :RealmObject 

Referenced by Realms.Realm.RemoveAll< T >().

§ BeginWrite()

Transaction Realms.Realm.BeginWrite ( )

Factory for a write Transaction. Essential object to create scope for updates.

using (var trans = realm.BeginWrite()) { var rex = realm.CreateObject<Dog>(); rex.Name = "Rex"; trans.Commit(); }

Returns
A transaction in write mode, which is required for any creation or modification of objects persisted in a Realm.

Referenced by Realms.Realm.Write().

§ Close()

void Realms.Realm.Close ( )

Closes the Realm if not already closed. Safe to call repeatedly. Note that this will close the file. Other references to the same database on the same thread will be invalidated.

References Realms.Realm.Dispose().

§ Compact()

static bool Realms.Realm.Compact ( RealmConfigurationBase  config = null)
static

Compacts a Realm file. A Realm file usually contains free/unused space. This method removes this free space and the file size is thereby reduced. Objects within the Realm file are untouched.

The realm file must not be open on other threads. The file system should have free space for at least a copy of the Realm file. This method must not be called inside a transaction. The Realm file is left untouched if any file operation fails.

Parameters
configOptional configuration.
Returns
true if successful, false if any file operation failed.

References Realms.Realm.GetInstance().

§ CreateObject()

dynamic Realms.Realm.CreateObject ( string  className)

Factory for a managed object in a realm. Only valid within a Write transaction.

Returns
A dynamically-accessed Realm object.
Parameters
classNameThe type of object to create as defined in the schema.

If the realm instance has been created from an un-typed schema (such as when migrating from an older version of a realm) the returned object will be purely dynamic. If the realm has been created from a typed schema as is the default case when calling

Realm.GetInstance()

the returned object will be an instance of a user-defined class, as if created by

Realm.CreateObject<T>()

.

Referenced by Realms.Realm.CreateObject< T >().

§ CreateObject< T >()

Factory for a managed object in a realm. Only valid within a Write transaction.

Using CreateObject is more efficient than creating standalone objects, assigning their values, then using Add because it avoids copying properties to the realm.

Template Parameters
TThe Type T must be a RealmObject.
Returns
An object which is already managed.
Exceptions
RealmInvalidTransactionExceptionIf you invoke this when there is no write Transaction active on the realm.
Type Constraints
T :RealmObject 
T :new() 

References Realms.Realm.CreateObject().

§ DeleteRealm()

static void Realms.Realm.DeleteRealm ( RealmConfigurationBase  configuration)
static

Deletes all the files associated with a realm. Hides knowledge of the auxiliary filenames from the programmer.

Parameters
configurationA configuration which supplies the realm path.

References Realms.RealmConfigurationBase.DatabasePath, and Realms.Realm.Schema.

§ Dispose()

void Realms.Realm.Dispose ( )

Dispose automatically closes the Realm if not already closed.

References Realms.Realm.Equals().

Referenced by Realms.Realm.Close().

§ Equals() [1/2]

override bool Realms.Realm.Equals ( object  obj)

Generic override determines whether the specified object is equal to the current Realm.

Parameters
objThe object to compare with the current Realm.
Returns
true if the Realms are functionally equal.

Referenced by Realms.Realm.Dispose().

§ Equals() [2/2]

bool Realms.Realm.Equals ( Realm  other)

Determines whether the specified Realm is equal to the current Realm.

Parameters
otherThe Realm to compare with the current Realm.
Returns
true if the Realms are functionally equal.

References Realms.Realm.Config, and Realms.Realm.IsClosed.

§ Find() [1/2]

RealmObject Realms.Realm.Find ( string  className,
long?  primaryKey 
)

Fast lookup of an object for dynamic use, from a class which has a PrimaryKey property.

Parameters
classNameName of class in dynamic situation.
primaryKeyPrimary key to be matched exactly, same as an == search. An argument of type long? works for all integer properties, supported as PrimaryKey.
Returns
Null or an object matching the primary key.
Exceptions
RealmClassLacksPrimaryKeyExceptionIf the RealmObject class lacks an [PrimaryKey].

Referenced by Realms.Realm.ObjectForPrimaryKey().

§ Find() [2/2]

RealmObject Realms.Realm.Find ( string  className,
string  primaryKey 
)

Fast lookup of an object for dynamic use, from a class which has a PrimaryKey property.

Parameters
classNameName of class in dynamic situation.
primaryKeyPrimary key to be matched exactly, same as an == search.
Returns
Null or an object matching the primary key.
Exceptions
RealmClassLacksPrimaryKeyExceptionIf the RealmObject class lacks an [PrimaryKey].

§ Find< T >() [1/2]

T Realms.Realm.Find< T > ( long?  primaryKey)

Fast lookup of an object from a class which has a PrimaryKey property.

Template Parameters
TThe Type T must be a RealmObject.
Parameters
primaryKeyPrimary key to be matched exactly, same as an == search. An argument of type long? works for all integer properties, supported as PrimaryKey.
Returns
Null or an object matching the primary key.
Exceptions
RealmClassLacksPrimaryKeyExceptionIf the RealmObject class T lacks an [PrimaryKey].
Type Constraints
T :RealmObject 

Referenced by Realms.Realm.ObjectForPrimaryKey< T >().

§ Find< T >() [2/2]

T Realms.Realm.Find< T > ( string  primaryKey)

Fast lookup of an object from a class which has a PrimaryKey property.

Template Parameters
TThe Type T must be a RealmObject.
Parameters
primaryKeyPrimary key to be matched exactly, same as an == search.
Returns
Null or an object matching the primary key.
Exceptions
RealmClassLacksPrimaryKeyExceptionIf the RealmObject class T lacks an [PrimaryKey].
Type Constraints
T :RealmObject 

§ GetHashCode()

override int Realms.Realm.GetHashCode ( )

Serves as a hash function for a Realm based on the core instance.

Returns
A hash code for this instance that is suitable for use in hashing algorithms and data structures such as a hash table.

§ GetInstance() [1/2]

static Realm Realms.Realm.GetInstance ( string  databasePath)
static

Factory for a Realm instance for this thread.

Parameters
databasePathPath to the realm, must be a valid full path for the current platform, relative subdirectory, or just filename.

If you specify a relative path, sandboxing by the OS may cause failure if you specify anything other than a subdirectory.
Instances are cached for a given absolute path and thread, so you may get back the same instance.

Returns
A realm instance, possibly from cache.
Exceptions
RealmFileAccessErrorExceptionThrows error if the file system returns an error preventing file creation.

References Realms.RealmConfiguration.ConfigWithPath(), and Realms.RealmConfiguration.DefaultConfiguration.

Referenced by Realms.Realm.Compact(), Realms.Realm.GetInstance(), and Realms.Realm.WriteAsync().

§ GetInstance() [2/2]

static Realm Realms.Realm.GetInstance ( RealmConfigurationBase  config = null)
static

Factory for a Realm instance for this thread.

Parameters
configOptional configuration.
Returns
A realm instance.
Exceptions
RealmFileAccessErrorExceptionThrows error if the file system returns an error, preventing file creation.

References Realms.RealmConfiguration.DefaultConfiguration, Realms.Realm.GetInstance(), and Realms.RealmConfigurationBase.ObjectClasses.

§ IsSameInstance()

bool Realms.Realm.IsSameInstance ( Realm  other)

Determines whether this instance is the same core instance as the passed in argument.

You can, and should, have multiple instances open on different threads which have the same path and open the same Realm.

Returns
true if this instance is the same core instance; otherwise, false.
Parameters
otherThe Realm to compare with the current Realm.

§ Manage< T >()

void Realms.Realm.Manage< T > ( obj,
bool  update = false 
)

This realm will start managing a RealmObject which has been created as a standalone object.

Template Parameters
TThe Type T must not only be a RealmObject but also have been processed by the Fody weaver, so it has persistent properties.
Parameters
objMust be a standalone object, null not allowed.
updateIf true, and an object with the same primary key already exists, performs an update.
Exceptions
RealmInvalidTransactionExceptionIf you invoke this when there is no write Transaction active on the realm.
RealmObjectManagedByAnotherRealmExceptionYou can't manage an object with more than one realm
Type Constraints
T :RealmObject 

References Realms.Realm.Add().

§ ObjectForPrimaryKey() [1/2]

RealmObject Realms.Realm.ObjectForPrimaryKey ( string  className,
long  id 
)

Fast lookup of an object for dynamic use, from a class which has a PrimaryKey property.

Parameters
classNameName of class in dynamic situation.
idId to be matched exactly, same as an == search.
Returns
Null or an object matdhing the id.
Exceptions
RealmClassLacksPrimaryKeyExceptionIf the RealmObject class lacks an [PrimaryKey].

References Realms.Realm.Find().

§ ObjectForPrimaryKey() [2/2]

RealmObject Realms.Realm.ObjectForPrimaryKey ( string  className,
string  id 
)

Fast lookup of an object for dynamic use, from a class which has a PrimaryKey property.

Parameters
classNameName of class in dynamic situation.
idId to be matched exactly, same as an == search.
Returns
Null or an object matdhing the id.
Exceptions
RealmClassLacksPrimaryKeyExceptionIf the RealmObject class lacks an [PrimaryKey].

References Realms.Realm.Find().

§ ObjectForPrimaryKey< T >() [1/2]

T Realms.Realm.ObjectForPrimaryKey< T > ( long  id)

Fast lookup of an object from a class which has a PrimaryKey property.

Template Parameters
TThe Type T must be a RealmObject.
Parameters
idId to be matched exactly, same as an == search. Int64 argument works for all integer properties supported as PrimaryKey.
Returns
Null or an object matching the id.
Exceptions
RealmClassLacksPrimaryKeyExceptionIf the RealmObject class T lacks an [PrimaryKey].
Type Constraints
T :RealmObject 

References Realms.Realm.Find< T >().

§ ObjectForPrimaryKey< T >() [2/2]

T Realms.Realm.ObjectForPrimaryKey< T > ( string  id)

Fast lookup of an object from a class which has a PrimaryKey property.

Template Parameters
TThe Type T must be a RealmObject.
Parameters
idId to be matched exactly, same as an == search.
Returns
Null or an object matdhing the id.
Exceptions
RealmClassLacksPrimaryKeyExceptionIf the RealmObject class T lacks an [PrimaryKey].
Type Constraints
T :RealmObject 

References Realms.Realm.Find< T >().

§ RealmChangedEventHandler()

delegate void Realms.Realm.RealmChangedEventHandler ( object  sender,
EventArgs  e 
)

Handler type used by RealmChanged

Parameters
senderThe Realm which has changed.
eCurrently an empty argument, in future may indicate more details about the change.

§ Refresh()

bool Realms.Realm.Refresh ( )

Update a Realm and outstanding objects to point to the most recent data for this Realm. This is only necessary when you have a Realm on a thread without a runloop that needs manual refreshing.

Returns
Whether the realm had any updates. Note that this may return true even if no data has actually changed.

§ Remove()

void Realms.Realm.Remove ( RealmObject  obj)

Removes a persistent object from this realm, effectively deleting it.

Parameters
objMust be an object persisted in this realm.
Exceptions
RealmInvalidTransactionExceptionIf you invoke this when there is no write Transaction active on the realm.
ArgumentNullExceptionIf you invoke this with a standalone object.

References Realms.RealmObject.IsManaged.

§ RemoveAll() [1/2]

void Realms.Realm.RemoveAll ( string  className)

Remove all objects of a type from the realm.

Parameters
classNameType of the objects to remove as defined in the schema.

References Realms.Realm.All().

§ RemoveAll() [2/2]

void Realms.Realm.RemoveAll ( )

Remove all objects of all types managed by this realm.

§ RemoveAll< T >()

void Realms.Realm.RemoveAll< T > ( )

Remove all objects of a type from the realm.

Template Parameters
TType of the objects to remove.
Type Constraints
T :RealmObject 

References Realms.Realm.All< T >().

§ RemoveRange< T >()

void Realms.Realm.RemoveRange< T > ( IQueryable< T >  range)

Remove objects matching a query from the realm.

Template Parameters
TType of the objects to remove.
Parameters
rangeThe query to match for.

§ Write()

void Realms.Realm.Write ( Action  action)

Execute an action inside a temporary transaction. If no exception is thrown, the transaction will automatically be committed.

Creates its own temporary transaction and commits it after running the lambda passed to action. Be careful of wrapping multiple single property updates in multiple Write calls. It is more efficient to update several properties or even create multiple objects in a single Write, unless you need to guarantee finer-grained updates.

realm.Write(() => { d = realm.CreateObject<Dog>(); d.Name = "Eddie"; d.Age = 5; });

Parameters
actionAction to perform inside a transaction, creating, updating or removing objects.

References Realms.Realm.BeginWrite().

§ WriteAsync()

Task Realms.Realm.WriteAsync ( Action< Realm action)

Execute an action inside a temporary transaction on a worker thread. If no exception is thrown, the transaction will automatically be committed.

Opens a new instance of this realm on a worker thread and executes action inside a write transaction. Realms and realm objects are thread-affine, so capturing any such objects in the action delegate will lead to errors if they're used on the worker thread.

await realm.WriteAsync(tempRealm => { var pongo = tempRealm.All<Dog>().Single(d => d.Name == "Pongo"); var missis = tempRealm.All<Dog>().Single(d => d.Name == "Missis"); for (var i = 0; i < 15; i++) { tempRealm.Add(new Dog { Breed = "Dalmatian", Mum = missis, Dad = pongo }); } }); Note that inside the action, we use tempRealm.

Parameters
actionAction to perform inside a transaction, creating, updating or removing objects.
Returns
A standard Task so it can be used by await.

References Realms.Realm.Config, and Realms.Realm.GetInstance().

Member Data Documentation

§ IsClosed

bool Realms.Realm.IsClosed => SharedRealmHandle.IsClosed

Checks if database has been closed.

Returns
True if closed.

Referenced by Realms.Realm.Equals().

Property Documentation

§ Config

RealmConfigurationBase Realms.Realm.Config
get

Gets the RealmConfigurationBase that controls this realm's path and other settings.

Referenced by Realms.Realm.Equals(), and Realms.Realm.WriteAsync().

§ RealmChanged

RealmChangedEventHandler Realms.Realm.RealmChanged
addremove

Triggered when a realm has changed (i.e. a transaction was committed).

§ Schema

RealmSchema Realms.Realm.Schema
get

Gets the RealmSchema instance that describes all the types that can be stored in this Realm.

Referenced by Realms.Realm.DeleteRealm().

Event Documentation

§ Error

EventHandler<ErrorEventArgs> Realms.Realm.Error

Triggered when a Realm-level exception has occurred.