简体   繁体   中英

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

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()

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. :)

Another case to use beginWrite & commitWrite is when you don't want to fire change notifications. To do so, you can pass the notification token to commitWrite as, commitWrite(withoutNotifying: [token]) .

More detail is in the Realm's official article - Interface-Driven Writes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM