Composition in Realm

Issue #13

There is time we have models that have some kind of inheritance, like Dog, Cat, Mouse can be Animal. We can use composition to imitate inheritance, we just need to make sure it has unique primary key

Cat and Dog

These are pretty much basic Realm objects

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Dog: Object {
dynamic var id: Int = 0

required convenience init(json: [String: Any]) {
self.init()
id <- json.integer(key: "id")
}
}

class Cat: Object {
dynamic var id: Int = 0

required convenience init(json: [String: Any]) {
self.init()
id <- json.integer(key: "id")
}
}

Animal

Here Animal can contain either dog or cat, we can add more if there are many other “inheritance” of Animal. The required init makes sure Animal can only be init with 1 type of child class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Animal: Object {
dynamic var id: Int = 0

dynamic var cat: Cat?
dynamic var dog: Dog?

required convenience init(cat: Cat) {
self.init()
self.cat = cat
self.id = cat.id
}

required convenience init(dog: Dog) {
self.init()
self.dog = dog
self.id = dog.id
}
}

How to execute an action only once in Swift

Issue #10

There is time we want to execute an action only once. We can surely introduce a flag, but it will be nicer if we abstract that out using composition. Then we have

1
2
3
4
5
6
7
8
9
10
11
12
13
class Once {

var already: Bool = false

func run(@noescape block: () -> Void) {
guard !already else {
return
}

block()
already = true
}
}

Usage

1
2
3
4
5
6
7
8
9
10
11
class ViewController: UIViewController {
let once = Once()

override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)

once.run {
cameraMan.setup()
}
}
}

In the same way, we can check to run a closure when a value changes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
final class WhenChange<T: Equatable> {
private(set) var value: T

init(value: T) {
self.value = value
}

func run(newValue: T, closure: (T) -> Void) {
if newValue != value {
value = newValue
closure(value)
}
}
}

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
2
3
4
protocol PrimaryKeyAware {
var id: Int { get }
static func primaryKey() -> String?
}

and conform it in out Realm object

1
2
3
4
5
6
7
8
9
10
class Profile: Object, PrimaryKeyAware {

dynamic var firstName: String = ""
dynamic var lastName: String = ""
dynamic var id: Int = 0

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

This way, when using that object in out RealmStorage, we are safe to say that it has a primary key

1
2
3
4
5
6
7
8
9
10
11
12
13
class RealmStorage<T: Object> where T: PrimaryKeyAware {
let realm: Realm

init(realm: Realm = RealmProvider.realm()) {
self.realm = realm
}

func save(_ objects: [T]) {
try? realm.write {
realm.add(objects, update: true)
}
}
}

The usage is like this

1
2
3
let profile = Profile()
let storage = RealmStorage<Profile>()
storage.save([profile])

Configuration closure in Swift

Issue #2

When I was reading through Swinject, I found something interesting https://github.com/Swinject/Swinject/blob/master/Sources/Container.swift

1
2
3
4
public convenience init(parent: Container? = nil, registeringClosure: (Container) -> Void) {
self.init(parent: parent)
registeringClosure(self)
}

The init has a closure that makes configuration easy, much like a Builder pattern. So I think we can learn from that and make a Configurable protocol

1
2
3
4
5
6
7
8
9
10
protocol Configurable: class {}

extension Configurable {
func config(block: (Self) -> Void) -> Self {
block(self)
return self
}
}

extension NSObject : Configurable {}

With this, we can init some class with less hassle

1
2
3
4
let view = UIView().config {
$0.backgroundColor = .white
$0.layer.cornerRadius = 2
}

Hello world, again

Issue #1

I’ve used Wordpress, then moved to GitHub Pages with Jekyll, Octopress, Hexo, Hugo. You can view my page here http://fantageek.com/. It was good with all the custom themes and Disqus

But then I was a bit lazy with all the commands generate, commit, deploy, it hinders me from writing, so I moved to Medium.

The only thing I like about Medium is its discovery, your posts have high chanced of finding and viewing by people. What’s the point of writing if no one read it? But then I miss all the awesome markdown features of GitHub Pages. Medium is easy to use, but it seems it’s not for hackers, and I find it really uncomfortable when adding code block and headings. Medium also lists my comments as stories, which is kind of 😲

I like to write fast, and with good comments system, and I love Markdown.

I like GitHub. I use GitHub for my notes, so I think I will use it for my blog as well. Hope all these GitHub convenience will encourage me to write more often. This will, of course, be less discoverable by people. So if you by any chance visit this blog, ohayou from me 👋

Updated at 2020-12-17 17:12:38