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

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

one.

try! realm.write { ... } 

2.

 realm.beginWrite() ... try! realm.commitWrite() 
+6
source share
2 answers

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


Functionally, there is no difference between the two. The realm.write method is a more convenient way to write transactions, but internally , it still just uses the same beginWrite / commitWrite transaction API:

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

Speaking of which, while realm.write {} faster and cleaner to write, there are still cases where you can rely on beginWrite / commitWrite .

beginWrite and commitWrite() are more manual, which is great if you want more control. realm.write {} implements its own error handling procedure, but if you want to do your own error handling, you can do it with beginWrite / commitWrite (or, alternatively, you can try to include try realm.write {} in your own do / catch ).

Another advantage of better management is that you can implement logic that can decisively cancel a transaction that has already started using cancelWrite() .

Ultimately, it depends on the level of control that you want, controlling the processing of specific write transactions and how you want to organize your code. You can easily consider both options, depending on the complexity of the write transaction that you plan to execute.

realm.write {} uses locks that make transactional code very elegant and minimal, but with the potential loss of necessary control. beginWrite / commitWrite gives you more control, but ultimately requires you to do more work in terms of handling potential errors.


Original answer

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

In fact, if you look at the source code for Realm Swift , you will 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 is no difference between using the two. Both are available to you, so you can choose the one that you most easily integrate into your code. :)

+6
source

Another use of beginWrite and commitWrite is when you don't want to trigger change notifications. To do this, you can pass the notification token to commitWrite as, commitWrite(withoutNotifying: [token]) .

Read more in the official article on Kings - Written by Interface

0
source

Source: https://habr.com/ru/post/1012149/


All Articles