如何返回领域对象或如果为空则创建一个?

时间:2021-10-19 07:16:12

Suppose I have a SearchText object as such:

假设我有一个SearchText对象:

class SearchText: Object {
    dynamic var text: String = ""
}

I would like to create an accessor such that I can get that object if it exists or create one if it doesn't. This is what I have.

我想创建一个访问器,以便我可以获取该对象(如果存在)或创建一个(如果不存在)。这就是我所拥有的。

extension Realm {
    var searchText: SearchText {
        if let searchText = objects(SearchText.self).first {
            return searchText
        } else {
            let searchText = SearchText()
            try! write {
                add(searchText)
            }
            return searchText
        }
}

This kinda works but here is my problem. I would like to use searchText and also update its value. Something like:

这种方式有效,但这是我的问题。我想使用searchText并更新其值。就像是:

func updateSearchText(text: String) {
        try! write {
            searchText?.text = text
        }
    }

When I try to updateSearchText, I get a realm exception of Realm is already in a write transaction. I think it's because I'm nesting to writes: creating the text and then updating it.

当我尝试更新SearchText时,我得到一个领域异常,Realm已经在写入事务中。我认为这是因为我正在嵌套写:创建文本然后更新它。

How would I do this elegantly?

我怎么会优雅地做到这一点?

1 个解决方案

#1


1  

Realm doesn't provide nested transaction currently. There is a way to avoid the exception is check you're in write transaction before open a transaction. Like the following:

Realm目前不提供嵌套事务。有一种方法可以避免异常,即在打开事务之前检查您是否处于写入事务中。如下:

func updateSearchText(text: String) {
    func update() {
        searchText?.text = text
    }

    if inWriteTransaction {
        update()
    } else {
        try! write {
            update()
        }
    }
}

Maybe searchText setter also should be:

也许searchText setter也应该是:

var searchText: SearchText {
    if let searchText = objects(SearchText.self).first {
        return searchText
    } else {
        let searchText = SearchText()
        if inWriteTransaction {
            add(searchText)
        } else {
            try! write {
                add(searchText)
            }
        }
        return searchText
    }
}

#1


1  

Realm doesn't provide nested transaction currently. There is a way to avoid the exception is check you're in write transaction before open a transaction. Like the following:

Realm目前不提供嵌套事务。有一种方法可以避免异常,即在打开事务之前检查您是否处于写入事务中。如下:

func updateSearchText(text: String) {
    func update() {
        searchText?.text = text
    }

    if inWriteTransaction {
        update()
    } else {
        try! write {
            update()
        }
    }
}

Maybe searchText setter also should be:

也许searchText setter也应该是:

var searchText: SearchText {
    if let searchText = objects(SearchText.self).first {
        return searchText
    } else {
        let searchText = SearchText()
        if inWriteTransaction {
            add(searchText)
        } else {
            try! write {
                add(searchText)
            }
        }
        return searchText
    }
}