Example #1
0
        private Realm(SharedRealmHandle sharedRealmHandle, RealmConfiguration config)
        {
            SharedRealmHandle = sharedRealmHandle;
            Config            = config;
            // update OUR config version number in case loaded one from disk
            Config.SchemaVersion = NativeSharedRealm.get_schema_version(sharedRealmHandle);

            Metadata = (config.ObjectClasses ?? RealmObjectClasses).ToDictionary(t => t, CreateRealmObjectMetadata);
        }
Example #2
0
        /// <summary>
        /// Use to save the changes to the realm. If transaction is declared in a <c>using</c> block, must be used before the end of that block.
        /// </summary>
        public void Commit()
        {
            if (!_isOpen)
            {
                throw new Exception("Transaction was already closed. Cannot commit");
            }

            NativeSharedRealm.commit_transaction(_sharedRealmHandle);
            _isOpen = false;
        }
Example #3
0
        /// <summary>
        /// Use explicitly to undo the changes in a transaction, otherwise it is automatically invoked by exiting the block.
        /// </summary>
        public void Rollback()
        {
            if (!_isOpen)
            {
                throw new Exception("Transaction was already closed. Cannot roll back");
            }

            NativeSharedRealm.cancel_transaction(_sharedRealmHandle);
            _isOpen = false;
        }
Example #4
0
        private TableHandle GetTable(Type realmType)
        {
            var result    = new TableHandle();
            var tableName = "class_" + realmType.Name;

            RuntimeHelpers.PrepareConstrainedRegions();
            try { /* Retain handle in a constrained execution region */ }
            finally
            {
                var tablePtr = NativeSharedRealm.get_table(SharedRealmHandle, tableName, (IntPtr)tableName.Length);
                result.SetHandle(tablePtr);
            }
            return(result);
        }
Example #5
0
 /// <summary>
 /// 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 non-runloop thread that needs manual refreshing.
 /// </summary>
 /// <returns>
 /// Whether the realm had any updates. Note that this may return true even if no data has actually changed.
 /// </returns>
 public bool Refresh()
 {
     return(MarshalHelpers.IntPtrToBool(NativeSharedRealm.refresh(SharedRealmHandle)));
 }
Example #6
0
 /// <summary>
 /// Determines whether this instance is the same core instance as the specified rhs.
 /// </summary>
 /// <remarks>
 /// You can, and should, have multiple instances open on different threads which have the same path and open the same Realm.
 /// </remarks>
 /// <returns><c>true</c> if this instance is the same core instance the specified rhs; otherwise, <c>false</c>.</returns>
 /// <param name="rhs">The Realm to compare with the current Realm.</param>
 public bool IsSameInstance(Realm rhs)
 {
     return(MarshalHelpers.IntPtrToBool(NativeSharedRealm.is_same_instance(SharedRealmHandle, rhs.SharedRealmHandle)));
 }
Example #7
0
        public static Realm GetInstance(RealmConfiguration config = null)
        {
            config = config ?? RealmConfiguration.DefaultConfiguration;

            // TODO cache these initializers but note complications with ObjectClasses
            var schemaInitializer = new SchemaInitializerHandle();

            if (config.ObjectClasses == null)
            {
                foreach (var realmObjectClass in RealmObjectClasses)
                {
                    var objectSchemaHandle = GenerateObjectSchema(realmObjectClass);
                    NativeSchema.initializer_add_object_schema(schemaInitializer, objectSchemaHandle);
                }
            }
            else
            {
                foreach (var selectedRealmObjectClass in config.ObjectClasses)
                {
                    if (selectedRealmObjectClass.BaseType != typeof(RealmObject))
                    {
                        throw new ArgumentException($"The class {selectedRealmObjectClass.FullName} must descend directly from RealmObject");
                    }

                    Debug.Assert(RealmObjectClasses.Contains(selectedRealmObjectClass));  // user-specified class must have been picked up by our static ctor
                    var objectSchemaHandle = GenerateObjectSchema(selectedRealmObjectClass);
                    NativeSchema.initializer_add_object_schema(schemaInitializer, objectSchemaHandle);
                }
            }

            var schemaHandle = new SchemaHandle(schemaInitializer);

            var srHandle = new SharedRealmHandle();

            var    readOnly     = MarshalHelpers.BoolToIntPtr(config.ReadOnly);
            var    durability   = MarshalHelpers.BoolToIntPtr(false);
            var    databasePath = config.DatabasePath;
            IntPtr srPtr        = IntPtr.Zero;

            try {
                srPtr = NativeSharedRealm.open(schemaHandle,
                                               databasePath, (IntPtr)databasePath.Length,
                                               readOnly, durability,
                                               config.EncryptionKey,
                                               config.SchemaVersion);
            } catch (RealmMigrationNeededException) {
                if (config.ShouldDeleteIfMigrationNeeded)
                {
                    DeleteRealm(config);
                }
                else
                {
                    throw; // rethrow te exception
                    //TODO when have Migration but also consider programmer control over auto migration
                    //MigrateRealm(configuration);
                }
                // create after deleting old reopen after migrating
                srPtr = NativeSharedRealm.open(schemaHandle,
                                               databasePath, (IntPtr)databasePath.Length,
                                               readOnly, durability,
                                               config.EncryptionKey,
                                               config.SchemaVersion);
            }

            RuntimeHelpers.PrepareConstrainedRegions();
            try { /* Retain handle in a constrained execution region */ }
            finally
            {
                srHandle.SetHandle(srPtr);
            }

            return(new Realm(srHandle, config));
        }
Example #8
0
 internal Transaction(SharedRealmHandle sharedRealmHandle)
 {
     this._sharedRealmHandle = sharedRealmHandle;
     NativeSharedRealm.begin_transaction(sharedRealmHandle);
     _isOpen = true;
 }