Shiv Jaiswal
Shiv Jaiswal

Reputation: 559

How to add and update values in objects using Realm (Swift)?

I have two classes given below. I want to perform operations like (Add, Update and Delete) to these objects.

Class CheckOutImageList is an array of object that contains images and Class CheckOutDetail is an object that contains image object.

class CheckOutImageList: Object {

    @objc dynamic var imageUrl = ""
    @objc dynamic var imageName = "test"
    @objc dynamic var isProfile = Bool()
    @objc dynamic var imageDesc = ""

    override static func primaryKey() -> String? {
        return "imageName"
    }
}

class CheckOutDetail: Object {

    @objc dynamic var taskId = 0
    var imageList = List<CheckOutImageList>()
    @objc dynamic var remarks = ""
    @objc dynamic var isFollowUp = Bool()
    @objc dynamic var followDate = ""
    @objc dynamic var followTime = ""

    override static func primaryKey() -> String? {
        return "taskId"
    }
}

I can perform add and update operation on CheckOutDetail class but need help to add, update and delete in image list array.

Upvotes: 1

Views: 7254

Answers (2)

Shiv Jaiswal
Shiv Jaiswal

Reputation: 559

To resolve this problem I made some changes in my objects. What I did, I just create two separate objects for both CheckOutDetails and CheckOutImageList like below:

class CheckOutImageList: Object {

    @objc dynamic var taskId = 0
    @objc dynamic var imageUrl = ""
    @objc dynamic var imageName = ""
    @objc dynamic var isProfile = Bool()
    @objc dynamic var imageDesc = ""

    override static func primaryKey() -> String? {
        return "imageName"
    }
}

class CheckOutDetail: Object {

    @objc dynamic var taskId = 0
    @objc dynamic var remarks = ""
    @objc dynamic var isFollowUp = Bool()
    @objc dynamic var followDate = ""
    @objc dynamic var followTime = ""

    override static func primaryKey() -> String? {
        return "taskId"
    }
}

//Code to update old data or add new image data in the CheckOutImageList Object
if self.checkImageNameExist(imageName: imgDetail.fileName) {
    try! realm.write {
        realm.add(obj, update: true) //obj is of type CheckOutImageList
    }
} else {
    try! realm.write {
        realm.add(obj)
    }
}

//Function will return true if primary key already exist else return false
func checkImageNameExist(imageName: String) -> Bool {
    return realm.object(ofType: CheckOutImageList.self, forPrimaryKey: imageName) != nil
}

And use taskId in CheckOutDetails as a foreign key to fetch details of the same id.

Upvotes: 2

Sid Mhatre
Sid Mhatre

Reputation: 3417

You can append CheckOutImageList element to the object CheckOutDetail.

Try this :

let realmIn = try! Realm()

var checkOutDetail = CheckOutDetail()
checkOutDetail.taskId = 1
checkOutDetail.imageList = "imageList"
checkOutDetail.isFollowUp = "isFollowUp "
checkOutDetail.followTime = "followTime"


// You can use for to append multiple elements to list
let newCheckOutImageList = CheckOutImageList()
newCheckOutImageList.imageUrl = "url"
nweCheckOutImageList.imageName = "imageName"
newCheckOutImageList.isProfile = "isProfile"
newCheckOutImageList.imageDesc = "imageDesc"

checkOutDetail.imageList.append(newCheckOutImageList)

try! realmIn.write {
     realmIn.add(checkOutDetail, update: true)
}

Upvotes: 0

Related Questions