3

I'm getting started with realm and trying to figure out the best way to write my model layer, so after overthinking about it I decide to ask here to get some opinions. Here we go:

I'm writing a realm version of these tables:

Account
id - String (PK)
Name - String
Type - Int

Checking Account
id - String (FK PK)

In this case a Checking Account is an Account, but realm still not support inheritance (I think using Account as a superclass would be the better way to figure these tables out in the model layer)

This is what I actually did with realm:

Account class:

import Foundation
import RealmSwift

class Account: Object {

    dynamic var id = NSUUID().UUIDString
    dynamic var name = ""
    dynamic var type = 3

    override static func primaryKey() -> String {
        return "id"
    }
}

Checking Account class:

import Foundation
import RealmSwift

class CheckingAccount: Object {

    dynamic lazy var id: String = {
        return self.account.id
    }()
    dynamic var account = Account()

    convenience init(name: String, type: AccountType) {
        self.init()

        self.account.name = name
        self.account.type = type.rawValue
    }

    override static func primaryKey() -> String? {
        return "id"
    }

}

What about it? It's a "Don't do" way to figure out my SQL tables with realm?

Thank you!

  • Can you share a little more about what you are trying to do? Not sure what the question is exactly – yoshyosh Aug 19 '15 at 18:17
  • @yoshyosh I'm asking if this is a great way to represent these relational tables with realm classes or if there is a better way. Maybe supress the `id` property of Bank since it has a property called `account` that holds the unique id of this Bank data (since a Bank is a specialization of Account). Or even use the `id` property of the Bank class as other unique value (not the same as Account `id`). You got it? I'm asking about how can I “translate" my relational database into realm classes. Thank you for your attention :D – Filipe Alvarenga Aug 19 '15 at 23:16
  • Ultimately you want to break away from the concept of foreign keys, I have a few examples discussing this in this github issue https://github.com/realm/realm-cocoa/issues/1741. I would have to understand a bit more about what you are trying to do with Bank since I would think a bank has many accounts and an account has one bank. That semantically makes sense for my mental model which keeps things simple. A good question is what do you plan to use these primary key id's for? – yoshyosh Aug 19 '15 at 23:43
  • This discussions helped me to understand that I was mixing the concepts of a relational database with the non-relational database. Sorry about the semantics, I was wrong about it too, `Bank` in this case represents `Checking Account` or `Current Account` we will have more types of accounts soon, such as `Savings`. So I plan to use these primary key id's to search for expenses or incomes in these accounts, so I want to calculate the balance of accounts and show some statistics to the user (It's a personal finances app). – Filipe Alvarenga Aug 20 '15 at 16:39
  • How about a User that has many accounts? Do Checking Account, Current Account, and Savings account have similar properties? I could then see you fetching a user by their primary key, ended up with `currentUser` and then using `currentUser.accounts` to iterate through each account to get their balances, adding them up etc. You could also have computed properties on the user like currentBalance using this same logic. As long as each account has one user and that user has a primary key it should make searching for accounts easier with predicates. – yoshyosh Aug 20 '15 at 17:16
  • I imagine you creating a tableview and setting its datasource by using `currentUser.accounts`, keeping the logic very simple without needing to do any PK lookup. – yoshyosh Aug 20 '15 at 17:17
  • Hey filipe just checking in, did our conversation help clear things up? – yoshyosh Aug 24 '15 at 15:51
  • Hey yosh, sorry about the delay. Yes, our conversation helped me a lot to understand how I should think when creating my models with Realm. Thank you so much for the attention. – Filipe Alvarenga Sep 13 '15 at 23:38

0 Answers0