Realm-cocoa: Dynamic Object schema Swift

Created on 20 Feb 2016  路  10Comments  路  Source: realm/realm-cocoa

I really need dynamic schema generated at runtime and I would like to extend Swift Realm class for supporting dynamic schema. Unfortunately the required initializer of Realm class is marked as internal for good reasons. I wonder if there is a way to set RLMRealm object in Realm class and how much damage would generate if I would pass a RLMRealm instance with dynamic option marked as true. ?

Are there some concrete plans for releasing a Swift production version that support dynamic schema ?

Thank you

O-Community Pipeline-Idea-Backlog T-Enhancement

All 10 comments

I can't really answer any of your questions directly, but I have been playing around with dynamic realms for a while. The only fragility I have found is that triggering migrations can be finicky, especially in the latest release. But if you handle that, and make sure to only open a realm with one schema version at a time, it might work.

I use the Objective C bindings (but from Swift), since the type safe API is only in the way when working with dynamic objects. At least as I see it.

I can't speak for Realm, but I would guess it's on the agenda at some point, since Android got it, and the underlying mechanisms are there (they are needed for migrations).

It would be interesting to hear of your experiences with this!

@GreatApe Thank you Gustaf for input. I don't need migration because I can perform a full syncronization with the backend based on the dynamic schema.

I thought I can use Swift bindings since there are already implemented things in a more cleaner way but I guess I'll have to implement by myself.

So you would throw away the realm file when you change the schema, and open a new one with the new schema?

I use migration to save the new schema in the realm, so I don't have to keep track of it myself, and hopefully it will also sync the schema changes for me when Realm sync is released. Maybe there is a better way!

As to the Swift bindings, you would still have to access your objects as plain RLMObjects, right? You won't have any dot properties. And how would you use Lists when their generic ElementType changes dynamically? I thought about bridging the dynamic APIs to Swift but then I figured that it wouldn't add anything for me. The stringly typed API is just what I need anyway.

Yes, I can afford to delete the entire realm file and start a new one from scratch by lazy fetching user data from backend. It's like I would flush a cache when JSON model is changing on server side, so I don't have to change the Swift api wrapper every time the backend is changing the response model.

@georgepoenaru @GreatApe I haven't used realm much, but just an idea for discussion. This is definitely overkill. maybe we can use realm for react-native to do dynamic schema generation? code on the JS side is more dynamic. it supports migration as well.

Or, maybe we can create an OC class at runtime, based on a dictionary and fed that into realm?

http://nikoyuwono.com/2016/01/12/realm-browser/
"Recently realm released realm 0.86 that included Dynamic Realms. With Dynamic Realms we can have access to Realm Schema and we can also create objects and access fields dynamically with using strings"

is dynamic schema available for objectivec/swift now?

Dynamic schema's and dynamic realms are different. What Realm Java released in 0.86, Realm Objective-C and Swift has had for quite some time, it enables the stringly-typed dynamic operations in migrations.

There's a clang submodule that's included in Realm Objective-C called Realm.Dynamic, which includes many dynamic features that you can use in your apps.

Realm Swift also includes methods prefixed with dynamic as stringly-typed equivalents to other strongly typed methods, such as Realm.dynamicObjects(...), Realm.dynamicCreate(...), etc.

Thanks for the explanation. Good to know Objectivec also support one.

This which I want to achieve with Realm so it behave like Parse PFObject. where the column/field can be added and remove realtime. Not sure if it is possible or not with Realm


PFObject *gameScore = [PFObject objectWithClassName:@"GameScore"];
gameScore[@"score"] = @1337;
pgameScore[@"playerName"] = @"Sean Plott";
gameScore[@"cheatMode"] = @NO;
[gameScore saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (succeeded) {
// The object has been saved.
} else {
// There was a problem, check error.description
}
}];

similar to this http://nikoyuwono.com/2016/01/12/realm-browser/ but in objectivec/swift

I think this will be dynamic schema implementation

  • (void)testDynamicSchema {
    RLMSchema *schema = [[RLMSchema alloc] init];
    RLMProperty *prop = [[RLMProperty alloc] initWithName:@"a"
    type:RLMPropertyTypeInt
    objectClassName:nil
    indexed:NO
    optional:NO];
    RLMObjectSchema *objectSchema = [[RLMObjectSchema alloc] initWithClassName:@"TrulyDynamicObject"
    objectClass:RLMObject.class properties:@[prop]];
    schema.objectSchema = @[objectSchema];
    RLMRealm *dyrealm = [self realmWithTestPathAndSchema:schema];
    XCTAssertNotNil(dyrealm, @"dynamic realm shouldn't be nil");
    }

Is there a way to do this using the latest RealmSwift? realm.dynamicCreate("classname") does not work. I tried creating the Schema like @steve21124 mentioned above (It did create the Schema and I was able to see it in realm.schema) but realm.dynamicObject(ofType: "classname", forPrimaryKey ) fails. It says that the model is not managd by Realm

Was this page helpful?
0 / 5 - 0 ratings