Exemplo n.º 1
0
 public SchemaInitializerHandle()
 {
     RuntimeHelpers.PrepareConstrainedRegions();
     try { /* Retain handle in a constrained execution region */ }
     finally
     {
         SetHandle(NativeSchema.initializer_create());
     }
 }
Exemplo n.º 2
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));
        }
Exemplo n.º 3
0
 protected override void Unbind()
 {
     NativeSchema.initializer_destroy(handle);
 }