Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic properties in Realm

Tags:

I am starting to work with Realm on iOS 8 or greater and looking at the documentation in Realm. I noticed that all of the properties have the dynamic keyword in front of them. Is that required in Realm? I have read the Apple documentation on the keyword which can be found here. https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/AdoptingCocoaDesignPatterns.html

like image 321
Cody Weaver Avatar asked Oct 16 '15 18:10

Cody Weaver


People also ask

What is dynamic realm?

DynamicRealm is a dynamic variant of Realm . This means that all access to data and/or queries are done using string based class names instead of class type references. This is useful during migrations or when working with string-based data like CSV or XML files.

What is realm for Swift?

Realm Swift is an easy to use alternative to SQLite and Core Data that makes persisting, querying, and syncing data as simple as working directly with native Swift objects. Deploy a sample appView documentation.


2 Answers

Yes, it is mandatory for normal var properties. From the realm docs.

Realm model properties need the dynamic var attribute in order for these properties to become accessors for the underlying database data.

There are two exceptions to this: List and RealmOptional properties cannot be declared as dynamic because generic properties cannot be represented in the Objective-C runtime, which is used for dynamic dispatch of dynamic properties, and should always be declared with let.

The dynamic keyword is what allows for Realm to be notified of changes to model variables, and consequently reflect them to the database.

like image 50
Edman Avatar answered Nov 24 '22 00:11

Edman


In Swift 3, we declared our property like this

dynamic var Name : String = "" 

In Swift 4, we declared our property like this

@objc dynamic var Name : String = "" 
like image 31
Khawar Islam Avatar answered Nov 23 '22 23:11

Khawar Islam