iOS(Swift)Singleton Realm对象 [英] iOS (Swift) Singleton Realm Object

查看:142
本文介绍了iOS(Swift)Singleton Realm对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读教程后,我需要一些帮助来了解最有效的方法方法如下.

After reading this tutorial I need some assistance in understanding what the most efficient way to do the following.

打开我的应用程序时,它需要加载一个Profile对象.由于在应用程序的生命周期中应该只有其中之一,因此我将其设置为单例.

When my app is opened, it needs to load a Profile object. Since there should only be one of these in the lifetime of the app I set it up to be a singleton.

领域似乎是保存和检索数据的好方法.进一步查看后,我似乎需要一个数据模型才能使用Realms.在尝试将Object集成到下面所示的Profile.swift中失败之后,我需要一些如何处理此问题的帮助.我应该创建第二个类ProfileDataModel,以供Profile调用以检索和保存更改,还是可以将Realm Object包含在Singleton类中?

Realm seemed to be a great way to save and retrieve data. Upon further viewing it seems I need to have a data model in order to use Realms. After a failed attempt of integrating Object into the Profile.swift shown below I need some assistance in how I should handle this issue. Should I make a second class ProfileDataModel that can be called by Profile to retrieve and save changes, or is there a way to include a Realm Object into a Singleton class?

Profile.swift

Profile.swift

class Profile {

    //MARK: Singleton
    static let sharedInstance = Profile()

    //MARK: Properties
    var characterName: String
    var level: Int

    //MARK: Init
    private init() {
        //TODO: Load from realms
        self.characterName = "John Appleseed"
        self.level = 50
    }

    //MARK: Helper Methods
    static func save(){
        //TODO: Save to Realm
    }
}

推荐答案

我建议您创建一个数据库管理器类以处理所有数据库操作,然后可以分别创建数据模型并使用管理器类来获取/将数据存储在您的数据库中.

I suggest you to create a db manager class to handle all db operations on it, then you can create your data models separately and use your manager class to fetch/store data on your db.

class DBManager {
//MARK: Singleton
static let sharedInstance = DBManager()

//MARK: Init
private override init(){
    let config = Realm.Configuration(
        fileURL: dbPath,
        readOnly: false)
    do{
        myDB = try Realm(configuration: config)
        print(dbPath)
    }
    catch{
        print("boooom")
    }

}

    //retrive data from db
    func getDataFromDB() -> Results<DataModel>{
    let results: Results<NewsModel> = myDB.objects(DataModel)
    return results
    }

    //write an object in db
    func addDataModelEntry(object: DataModel){
        try! myDB.write{
            myDB.add(object, update: true)
        }
    }

}

//your controller you want to use your db manager class
class main(){ 
    func viewDidLoad(){
       DBManager.sharedInstance.getDataFromDB() ///here you have realm results

       DBManager.sharedInstance.addDataModelEntry(customDataModel) //to store your object on db
    }
}

我仅举一些示例来说明这样做的方式,您可以使用这些功能将其扩展为满足特定需求的任何类型的db操作.

i put some samples just to show the way of doing, you can use these functions to extend to any kind of db operations for your specific needs.

这篇关于iOS(Swift)Singleton Realm对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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