internal override Realm CreateRealm(RealmSchema schema)
        {
            var configuration = new Configuration
            {
                Path      = DatabasePath,
                read_only = IsReadOnly,
                delete_if_migration_needed = ShouldDeleteIfMigrationNeeded,
                schema_version             = SchemaVersion
            };

            Migration migration = null;

            if (MigrationCallback != null)
            {
                migration = new Migration(this, schema);
                migration.PopulateConfiguration(ref configuration);
            }

            if (ShouldCompactOnLaunch != null)
            {
                var handle = GCHandle.Alloc(ShouldCompactOnLaunch);
                configuration.should_compact_callback         = ShouldCompactOnLaunchCallback;
                configuration.managed_should_compact_delegate = GCHandle.ToIntPtr(handle);
            }

            var srPtr = IntPtr.Zero;

            try
            {
                srPtr = SharedRealmHandle.Open(configuration, schema, EncryptionKey);
            }
            catch (ManagedExceptionDuringMigrationException)
            {
                throw new AggregateException("Exception occurred in a Realm migration callback. See inner exception for more details.", migration?.MigrationException);
            }

            var srHandle = new SharedRealmHandle();

            srHandle.SetHandle(srPtr);
            if (Dynamic && !schema.Any())
            {
                srHandle.GetSchema(nativeSchema => schema = RealmSchema.CreateFromObjectStoreSchema(nativeSchema));
            }

            return(new Realm(srHandle, this, schema));
        }
Пример #2
0
        internal override Realm CreateRealm(RealmSchema schema)
        {
            var configuration = CreateNativeConfiguration();
            configuration.delete_if_migration_needed = ShouldDeleteIfMigrationNeeded;
            configuration.read_only = IsReadOnly;

            Migration migration = null;
            if (MigrationCallback != null)
            {
                migration = new Migration(this, schema);
                configuration.managed_migration_handle = GCHandle.ToIntPtr(migration.MigrationHandle);
            }

            GCHandle? shouldCompactHandle = null;
            if (ShouldCompactOnLaunch != null)
            {
                shouldCompactHandle = GCHandle.Alloc(ShouldCompactOnLaunch);
                configuration.managed_should_compact_delegate = GCHandle.ToIntPtr(shouldCompactHandle.Value);
            }

            var srPtr = IntPtr.Zero;
            try
            {
                srPtr = SharedRealmHandle.Open(configuration, schema, EncryptionKey);
            }
            catch (ManagedExceptionDuringMigrationException)
            {
                throw new AggregateException("Exception occurred in a Realm migration callback. See inner exception for more details.", migration?.MigrationException);
            }
            finally
            {
                migration?.ReleaseHandle();
                shouldCompactHandle?.Free();
            }

            var srHandle = new SharedRealmHandle(srPtr);
            if (IsDynamic && !schema.Any())
            {
                schema = srHandle.GetSchema();
            }

            return new Realm(srHandle, this, schema);
        }