【问题标题】:How to store Array of Dictionaries to Realm Database in Swift如何在 Swift 中将字典数组存储到领域数据库
【发布时间】:2018-10-10 08:29:35
【问题描述】:

我在 swift 应用程序中通过 api 调用从服务器获取 JSON 数据。 因此,我想将其存储到 Realm 数据库中,并且需要再次获取以显示在 tableview 中。

我对Realm数据库一无所知,看了几个论坛后,我有了创建Object类的基本思路。

所以,我已经安装了 Realm pod 文件并将该库导入到我的类中。

我的 JSON 数据是

[{
"type": "story",
"story": 

{
"author-name": "",
"headline": "Quotes ",
"summary": "Best quotes of Muhammad Ali",
"hero-image": "https://image”
}
},
{
"type": “Trending”,
"story": 
    {
    "author-name": "",
    "headline": "Quotes ",
    "summary": "Best quotes of Muhammad Ali",
    "hero-image": "https://image”
    }
    },
    {
    "type": “Technology”,
    "story": 

    {
    "author-name": "",
    "headline": "Quotes ",
    "summary": "Best quotes of Muhammad Ali",
    "hero-image": "https://image”
    }
    },
    {
    "type": “Top”,
    "story": 

    {
    "author-name": "",
    "headline": "Quotes ",
    "summary": "Best quotes of Muhammad Ali",
    "hero-image": "https://image”
    }
    }
    ]

而且我每个 type 关键字都有不同的模型类保存的数据从 api 数据显示在 Tableview

喜欢

let storyObj = StoryModule()
let trending = StoryModule()
let technology = StoryModule()
let stotopryObj1 = StoryModule()

我正在为每个 type

保存每个键值
if abc.type == "story" {
    let storyObj = abc.story
    storyObj.authorname = storyObj?.authorname
    storyObj.heroimage = storyObj?.heroimage
    storyObj.headline = storyObj?.headline
    storyObj.summary = storyObj?.summary
    self.treningStoriesList.append(storyObj)
}

其余的 Trending、Top 和 Technology 对象也是如此。

Realm 模块是

import RealmSwift

class DemoInfo: Object {
    @objc dynamic var category = ""
    let items = List<DemoList>()
}

class DemoList : Object {
    @objc dynamic var authorName = ""
    @objc dynamic var imageUrl = ""
    @objc dynamic var summary = ""
    @objc dynamic var headLine = ""
}

在MainViewController 类中,

let realmDB = try! Realm()

但是,我被打动了,如何保存那些storyObj,technology,top, etc 模块数据并获取。

谁能推荐我?

【问题讨论】:

    标签: ios iphone swift database realm


    【解决方案1】:

    如果你想在你的数据库中添加一个领域对象,你必须为每个领域对象类定义一个主键。因此,在您可以像这样创建领域对象之后,您需要更改 JSON 文件;

    DemoObject.swift

    import RealmSwift
    
    class DemoObject: Object {
    
        @objc dynamic var id: String = ""
        @objc dynamic var type: String = ""
        @objc dynamic var subObject: SubObject?
    
        override static func primaryKey() -> String? {
            return "id"
        }
    }
    

    SubObject.swift

    import RealmSwift
    
    class SubObject: Object {
    
        @objc dynamic var id: String = ""
        @objc dynamic var authorName: String = ""
        @objc dynamic var imageUrl: String = ""
        @objc dynamic var summary: String = ""
        @objc dynamic var headLine: String = ""
    
        override static func primaryKey() -> String? {
            return "id"
        }
    }
    

    然后,您可以使用这些代码来添加您的数据库。

    let realm = try! Realm()
    
    let demo = DemoObject()
    demo.id = "1"
    
    let sub = SubObject()
    sub.id = "1"
    sub.authorName = "Author Name"
    sub.headLine = "Head Line"
    sub.summary = "image Url"
    
    demo.subObject = sub
    
    try! realm.write {
        realm.add(demo, update: true)
    }
    

    【讨论】:

    • 这不是真的,Object 子类不需要需要有一个主键。如果您希望它们使用add(_:,update:) 方法自动更新,它们只需要有一个主键。
    • 是的,你是对的!如果他不想使用添加或更新功能,他可以从所有类中删除id 属性。感谢您的提示。
    • 这里的每个模块我都像上面一样添加了吗?
    • 如何区分故事、趋势、热门等?
    • @serdaryillar 我正在等待您的回复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多