Anilkumar iOS Developer
Anilkumar iOS Developer

Reputation: 3745

How to store Array of Dictionaries to Realm Database in Swift

I am getting JSON data from server by api call in swift application. So, I want to store that into Realm data base and again need to fetch to show in tableview.

I have no idea about Realm database, After, checked few forums, I got basic idea for creating Object class.

So, I have installed Realm pod file and imported that library to my classes.

My JSON data is

[{
"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”
    }
    }
    ]

And I have each type keyword has different model class saved data from api data to show in Tableview

like

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

and I am saving each key value for every 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)
}

It is same for remaining Trending, Top and Technology objects.

and the Realm module is

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 = ""
}

And In MainViewController class,

let realmDB = try! Realm()

But, Here I got struck, How to save those storyObj,technology,top, etc module data and fetch.

Can anyone suggest me?

Upvotes: 0

Views: 2064

Answers (1)

serdaryillar
serdaryillar

Reputation: 413

If you want to add a realm object in your db, you must define a primary key for each realm object classes. So, you need to change your JSON file, after you can create your realm objects like this;

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"
    }
}

Then, you can use these codes to add your db.

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)
}

Upvotes: 0

Related Questions