ios - Nested Object Update issue in Realm -
please check following model class of team
, member
. in team, there members
can hold list of member
. json object used create , update teams in service methods. member can updated individually. in member
object, there property lastseenat
updated according member's activity.
class team: object { dynamic var channelid: string? = nil dynamic var title: string? = nil let members = list<member>() override class func primarykey() -> string? { return "channelid" } } class member: object { dynamic var _id: string? dynamic var lastseenat: date? override class func primarykey() -> string? { return "_id" } }
service methods updating teams , members
func createandupdateteams(_ teams: [[string:any]]?) { dispatchqueue(label: "queuenametocreate", attributes: []).async { let realm = try! realm() try! realm.write({ () in team in teams! { realm.create(team.self, value: team, update: true) } }) } } func updatemember(_ member: [string:any]?) { dispatchqueue(label: "queuenametoupdatemember", attributes: []).async { let realm = try! realm() try! realm.write({ realm.create(memberrealm.self, value: member, update: true) }) } }
to real-time update on teams, have added notification block on results<team>
follows:
teams = realm.objects(team.self); notificationtoken = teams?._addnotificationblock({ [weak self](changes) in switch( changes ) { case .initial: self?.tableview?.reloaddata() case .update(_,let deletions,let insertions,let modifications): print("modified: deletions: \(deletions.count),\n insertions: \(insertions.count),\n modifications:\(modifications.count)") self?.tableview?.reloaddata() case .error: break } })
problem
when called updatemember(_:)
update member
object, notification block gets called. though there no update in team, tableview load same data. effect screen fluctuates each updatemember(_:)
call.
github sample project generate issue https://github.com/milankamilya/realmupdatetest
what see intended behaviour of realm
. notification blocks called design if single object updated in one-to-many relationship of 1 of objects in scope of completion block. since team
class has one-to-many relationship member
, when setting notification block on team
objects, notification block , should called each time member
object gets updated if in stored in 1 of team
objects members
property.
see this github issue explained issue in more details.
one workaround use define members
linkingobjects
instead of list
. haven't tried myself, according answers on linked github issue, notification block doesn't called when object changes part of linkingobjects
in contrast one-to-many relationship using list
.
to use linkingobjects
members
, need change model classes following way:
class team: object { dynamic var channelid: string? = nil dynamic var title: string? = nil let members = linkingobjects(fromtype: member.self, property: "team") //finds member objects, member.team = self (this team) override class func primarykey() -> string? { return "channelid" } } class member: object { dynamic var _id: string? dynamic var team:team? dynamic var lastseenat: date? override class func primarykey() -> string? { return "_id" } }
Comments
Post a Comment