Exemple #1
0
        /// <summary>
        /// This realm will start managing a RealmObject which has been created as a standalone object.
        /// </summary>
        /// <typeparam name="T">The Type T must not only be a RealmObject but also have been processd by the Fody weaver, so it has persistent properties.</typeparam>
        /// <param name="obj">Must be a standalone object, null not allowed.</param>
        /// <exception cref="RealmOutsideTransactionException">If you invoke this when there is no write Transaction active on the realm.</exception>
        /// <exception cref="RealmObjectAlreadyManagedByRealmException">You can't manage the same object twice. This exception is thrown, rather than silently detecting the mistake, to help you debug your code</exception>
        /// <exception cref="RealmObjectManagedByAnotherRealmException">You can't manage an object with more than one realm</exception>
        public void Manage <T>(T obj) where T : RealmObject
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            if (obj.IsManaged)
            {
                if (obj.Realm.SharedRealmHandle == this.SharedRealmHandle)
                {
                    throw new RealmObjectAlreadyManagedByRealmException("The object is already managed by this realm");
                }

                throw new RealmObjectManagedByAnotherRealmException("Cannot start to manage an object with a realm when it's already managed by another realm");
            }


            if (!IsInTransaction)
            {
                throw new RealmOutsideTransactionException("Cannot start managing a Realm object outside write transactions");
            }

            var tableHandle = Metadata[typeof(T)].Table;

            var rowPtr    = NativeTable.add_empty_row(tableHandle);
            var rowHandle = CreateRowHandle(rowPtr, SharedRealmHandle);

            obj._Manage(this, rowHandle);
            obj._CopyDataFromBackingFieldsToRow();
        }
Exemple #2
0
        /// <summary>
        /// Factory for a managed object in a realm. Only valid within a Write transaction.
        /// </summary>
        /// <remarks>Using CreateObject is more efficient than creating standalone objects, assigning their values, then using Manage because it avoids copying properties to the realm.</remarks>
        /// <typeparam name="T">The Type T must not only be a RealmObject but also have been processd by the Fody weaver, so it has persistent properties.</typeparam>
        /// <returns>An object which is already managed.</returns>
        /// <exception cref="RealmOutsideTransactionException">If you invoke this when there is no write Transaction active on the realm.</exception>
        public T CreateObject <T>() where T : RealmObject, new()
        {
            if (!IsInTransaction)
            {
                throw new RealmOutsideTransactionException("Cannot create Realm object outside write transactions");
            }

            var objectType = typeof(T);

            if (Config.ObjectClasses != null && !Config.ObjectClasses.Contains(objectType))
            {
                throw new ArgumentException($"The class {objectType.Name} is not in the limited set of classes for this realm");
            }

            var metadata = Metadata[typeof(T)];

            var result = (T)metadata.Helper.CreateInstance();

            var rowPtr    = NativeTable.add_empty_row(metadata.Table);
            var rowHandle = CreateRowHandle(rowPtr, SharedRealmHandle);

            result._Manage(this, rowHandle);
            return(result);
        }