Search code examples
iosrealmswift4xcode9pojo

ios swift models structure with realm database


I am developing my iOs App and I am using Realm database. As I am totally new to ios developing (also swift and xcode) I have question about structuring data (I've already read some general project structure guidelines but couldn't find the answer). My thinking is connected with Java structures

For Realm databases (RealmSiwft) I created a model like this:

@objcMembers class Patient: Object {
    dynamic var patientId:Int = 0
    dynamic var refNumber:String = ""

    convenience init(id:Int, refNumber:String){
       self.init()
       self.patinetID = id
       self.refNumber = refNumber
    }
}

Now, it looks just like a POJO class in Java. But as I learned, this model structure is made that way so it can be able to use Realm.

So the question is, if I need somewhere else in my project to use Patient objects, is this Realm-POJO-model good to use? I mean, should I use it just like a normal Model even when I dont need to make database operations on it? Or should I make this Realm model alike DAO class for databases operations and make another model class like Patient.swift for whenever I want to play with Patient without using databases (I hope not, cause it's so much code duplicating)

And what if I need variables in that Patient Model that won't be stored in database? Can I make it without dynamic? What about init than? That blows my mind, as far as I learn swift it seems so ugly and unstructured, or I just can't switch to it yet...


Solution

  • if I need somewhere else in my project to use Patient objects, is this Realm-POJO-model good to use? even when I dont need to make database operations on it?

    You can use your Patient object without savings to the DB, move them to different controllers and so on.

    what if I need variables in that Patient Model that won't be stored in database?

    Look to ignoredProperties() method.

    Can I make it without dynamic?

    No you can't because of Realm based on Objective-C object, so this is necessary type.

    What about init than?

    You can create different Constructors methods, look to the Initialization doc. In case with Realm you should setup values to noticed variables (if you don't give them Default Property Values)

    Your class should look like this:

    class Patient: Object {
    
        // MARK: - Properties
        @objc dynamic var patientId: Int = 0
        @objc dynamic var refNumber: String = ""
    
        // MARK: - Meta
        // to set the model’s primary key
        override class func primaryKey() -> String? {
            return "patientId"
        }
    
        //Ignoring properties
        override static func ignoredProperties() -> [String] {
           return ["tmpID"]
        }
    
        //It's ok
        convenience init(id:Int, refNumber:String){
            self.init()
            self.patientId = id
            self.refNumber = refNumber
        }
    }
    

    All other detail information you can find in: realm docs

    Also you can extend you base code with swift extension:

    extension Patient {
        var info: String {
            return "\(patientId) " + refNumber
        }
    
        func isAvailableRefNumber() -> Bool {
            return refNumber.length > 6
        }
    }