本文介绍了领域数据库Swift 3.1的设计模式-Singleton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何实现Class的设计,该设计可以管理/处理 RealmSwift框架的所有查询

I'm wondering how to implement a design for a Class which can manage/handle all the queries for RealmSwift Framework

此问题与实现SQLite Model Manager相似,但适用于RealmSwift.

This question is somewhat similar to implementing SQLite Model Manager but for RealmSwift.

具体来说,我不需要上面提到的单例对象/实例.

Specifically I don't require a singleton object/instance mention above.

推荐答案

Realm有一个相当聪明的内部缓存系统,每次let realm = try! Realm()之类的调用发生时,Realm的先前实例都会保留并回收.因此,这实际上不是必需的,也不建议尝试将Realm实例本身合并为一个单例.

Realm has a rather clever internal caching system where previous instances of Realm are held onto and recycled each time a call like let realm = try! Realm() occurs. As such, it's not really necessary, nor recommended to try and incorporate a Realm instance itself into a singleton.

如果要大量自定义Realm实例的设置,通常可以通过Realm Configuration对象来实现,该对象是静态的并且是线程安全的.如果是这种情况,那么在需要创建新的Realm实例时,最好有一个单例(甚至只是静态类方法)返回适当的Configuration对象.

If you want to heavily customise your Realm instance's settings, you'll normally do that through a Realm Configuration object, which is static and thread-safe. If that's the case, it would be more appropriate to have a singleton (or even just a static class method) that returns the appropriate Configuration object when you need to create a new Realm instance.

swift中的东西上有关于如何在Swift中创建单例的页面,它实质上只是类的单个静态属性实施:

that thing in swift has a page on how to create singletons in Swift, and it's essentially just a single static property of a class implementation:

class SomeManager {
    static let sharedInstance = SomeManager()
}

这篇关于领域数据库Swift 3.1的设计模式-Singleton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 07:27