Primary key in Realm
Issue #4
Realm is great. But without primary key, it will duplicate the record, like https://github.com/realm/realm-java/issues/2730, http://stackoverflow.com/questions/32322460/should-i-define-the-primary-key-for-each-entity-in-realm, … So to force ourselves into the good habit of declaring primary key, we can leverage Swift protocol
Create primary constrain protocol like this
1 | protocol PrimaryKeyAware { |
and conform it in out Realm object
1 | class Profile: Object, PrimaryKeyAware { |
This way, when using that object in out RealmStorage
, we are safe to say that it has a primary key
1 | class RealmStorage<T: Object> where T: PrimaryKeyAware { |
The usage is like this
1 | let profile = Profile() |