简体   繁体   English

realm.write和realm.beginWrite + realm.commitWrite有什么区别?

[英]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? 在Realm中有两种执行写入事务的方法,它们之间的区别是什么?

1. 1。

try! realm.write {
  ...
}

2. 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. 更新于2017年4月19日,更简洁,并解释选择一个优于另一个的优势。


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: 方法realm.write是一种执行写入事务的更方便的方法,但在内部 ,它仍然只是使用完全相同的beginWrite / commitWrite事务API:

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. 话虽这么说,虽然realm.write {}编写起来更快更清晰,但仍有一些实例可能需要依赖beginWrite / commitWrite

beginWrite and commitWrite() are more manual, which is great if you want more control. beginWritecommitWrite()更加手动,如果你想要更多的控制,这是很好的。 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). realm.write {}实现了自己的错误处理例程,但是如果你想执行自己的特定错误处理,可以使用beginWrite / commitWrite (或者你可以尝试在其中包含try realm.write {} 。拥有do / catch块)。

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() . 拥有更多控制权的另一个好处是,您可以实现可以选择直接取消已使用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. realm.write {}使用闭包,这使得包装事务代码非常优雅,最小,但可能会丢失您可能想要的控制量。 beginWrite / commitWrite gives you more control, but ultimately require you as the user to do more work in terms of handling potential errors. beginWrite / commitWrite为您提供了更多控制权,但最终要求您作为用户在处理潜在错误方面做更多工作。


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 . 方法realm.write只是一种执行写入事务而不是使用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 . 实际上,如果你查看了Realm Swift的源代码 ,你会发现realm.write实际上只是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. 使用beginWritecommitWrite另一种情况是当您不想触发更改通知时。 To do so, you can pass the notification token to commitWrite as, commitWrite(withoutNotifying: [token]) . 为此,您可以将通知令牌传递给commitWrite as commitWrite(withoutNotifying: [token])

More detail is in the Realm's official article - Interface-Driven Writes 更多细节在Realm的官方文章 - Interface-Driven Writes中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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