实例化Swift 2.0的Realm数据库-最佳实践? [英] Instantiating Realm database for Swift 2.0 - best practice?

查看:105
本文介绍了实例化Swift 2.0的Realm数据库-最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道实例化Realm数据库的最佳实践是Swift 2. Realm for Swift 1.2和Swift 2之间的主要区别之一是Realm类增加了对错误处理的支持. 因此,Realm网站上的此代码不再起作用:

I'm wondering what the best practice for instantiating a Realm database is for Swift 2. One of the major differences between Realm for Swift 1.2 and Swift 2 is that the Realm class has added support for error handling. Therefore, this code on the Realm website does not work anymore:

let realm = Realm()

let realm = Realm()

我可以想到几种在Swift 2世界中实例化Realm类的方法:

I can think of a couple ways to instantiate a Realm class in the Swift 2 world:

(1)让领域=尝试! Realm()

(1) let realm = try! Realm()

此选项对我来说似乎有点不安全",因为如果类无法实例化,则可能会导致运行时错误.

This option seems a little "unsafe" to me as it potentially results in a runtime error if the class fails to instantiate.

(2)将整个Realm操作(包括类实例化)放入Do-Catch块中

(2) Place entire Realm operation (including class instantiation) inside Do-Catch block

do {
    let realm = try Realm()

    realm.write{realm.add(myObject)}
    }
    catch
    {
        print("Some Realm error")
    }

这绝对有效并且绝对安全.但是,我真的不喜欢实例化 每当我需要在数据库上执行操作时,都要使用Realm类.如果我尝试创建 如果是IVAR领域"并将其放置在Do-Catch块之外,则该变量将超出范围. 例如,以下代码将无法编译...

This definitely works and is definitely safe. HOWEVER, I don't really like having to instantiate the Realm class every time I need to perform an operation on the database. If I try to create an IVAR 'realm' and place it outside of the Do-Catch block, the variable goes out of scope. For instance, the following code will not compile...

    //IVAR declared outside of Do-Catch...
    let realm:Realm

    do{
        //Get instance of Realm
        realm = try Realm()

        //This write operation works
        realm.write{realm.add(myObject_1)}
    }
    catch
    {
        print("Some Realm error")
    }


    //Create another Dog object
    let myObject_2 = SomeObject()

    //This next line produces an error:  "Variable 'realm' used before being initialized".
    //Initialized 'realm' inside Do-Catch is now out of scope.
    realm.write({
        realm.add(myObject_2)
    })

对于任何在Swift 2的新错误处理环境中使用Realms的最佳做法的反馈,我将不胜感激(尤其是来自Realm的人).预先感谢.

I'd appreciate any feedback (especially someone from Realm) on what the best practice for working with Realms in the new error handling environment of Swift 2 should look like. Thanks in advance.

推荐答案

除非您实际上要处理收到的错误,否则我强烈建议使用try!.

Unless you're actually going to handle the errors you receive, I highly recommend using try!.

您的第二个代码段不起作用,因为如果初始化Realm失败,则该realm变量将永远不会赋值给该变量,这是无效的.您可以通过使realm变量的类型为Realm?来解决此问题.

Your second code snippet doesn't work because, if initializing the Realm fails, that realm variable is never assigned to, which is invalid. You could probably work around that by making the realm variable be of type Realm?.

这篇关于实例化Swift 2.0的Realm数据库-最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆