Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between realm.write and realm.beginWrite+realm.commitWrite?

Tags:

ios

swift

realm

There are two ways of performing a write transaction in Realm, what's the difference between them?

1.

try! realm.write {
  ...
}

2.

realm.beginWrite()
...
try! realm.commitWrite()
like image 759
lozy219 Avatar asked Nov 08 '16 17:11

lozy219


1 Answers

Updated on 4/19/2017 to be more concise and to explain the advantages of choosing one over the other.


Functionally, there's no difference between the two. The method realm.write is a more convenient method to perform write transactions, but internally, it still simply makes use of the exact same beginWrite/commitWrite transaction APIs:

public func write(_ block: (() throws -> Void)) throws {
    beginWrite()
    do {
        try block()
    } catch let error {
        if isInWriteTransaction { cancelWrite() }
        throw error
    }
    if isInWriteTransaction { try commitWrite() }
}

That being said, while realm.write {} is quicker and cleaner to write, there are still instances where you might want to rely on beginWrite / commitWrite instead.

beginWrite and commitWrite() are more manual, which is great if you want more control. realm.write {} implements its own error handling routine, but if you'd like to perform your own specific error handling, you can do so with beginWrite / commitWrite (Or alternatively, you could try enclosing try realm.write {} in its own do / catch block).

Another advantage to having more control is that you can implement logic that might choose to outright cancel a transaction that's already started using cancelWrite().

Ultimately, it depends on the level of control you want with controlling handling specific write transactions, and how you want to organize your code. You could easily consider both depending on the complexity of the write transaction you're planning on performing.

realm.write {} uses closures which makes wrapping the transaction code very elegant, and minimal, but at a potential loss of the amount of control you might want. beginWrite / commitWrite gives you more control, but ultimately require you as the user to do more work in terms of handling potential errors.


Original Answer

There's absolutely no difference between the two. The method realm.write is simply a more convenient method to perform write transactions instead of using beginWrite/commitWrite.

In fact, if you check out the source code for Realm Swift, you'll see that realm.write is actually just a wrapper for beginWrite/commitWrite.

public func write(_ block: (() throws -> Void)) throws {
    beginWrite()
    do {
        try block()
    } catch let error {
        if isInWriteTransaction { cancelWrite() }
        throw error
    }
    if isInWriteTransaction { try commitWrite() }
}

So there's no difference between using the two. Both are available to you so you can pick the one that is easiest for you to integrate into your code. :)

like image 75
TiM Avatar answered Oct 19 '22 02:10

TiM