realm-swift: Avoid triggering change notifications for setting properties to existing values for createOrUpdate

Goals

I have a json file that contains an array of objects whose model is exactly the same as my RLMObject model so I would like to use -createOrUpdateInDefaultRealmWithValue: or -createOrUpdateInRealm:withValue: to automatically update RLMObject with values from that json every time it’s loaded.

Expected Results

If NSDictionary from a json has all the same values as an existing RLMObject, -createOrUpdateInDefaultRealmWithValue: and -createOrUpdateInRealm:withValue: should not cause change notifications to be triggered for that object.

Actual Results

Even if json has all the same values as an existing RLMObject, -createOrUpdateInDefaultRealmWithValue: and -createOrUpdateInRealm:withValue: will cause change notifications to be triggered for every object even when its “update” causes no actual value updates.

I know that’s probably caused by KVO which fires no matter if new value is the same, but is there any way you could check the old value before setting it so that it doesn’t trigger? I can and will do this manually like I did with Core Data, but it would be really nice if it was done automatically because I can’t imagine a scenario where I would want to be notified when RLMObject’s properties are assigned to the same values as they were before.

Version of Realm and Tooling

Realm version: 0.99.1

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 4
  • Comments: 24 (6 by maintainers)

Most upvoted comments

When are you planning for fixing this. Actually i use realm for cache purpose. So lets say i have a feed Object which contains a user object and lets say i have tab bar and on each tab i have pushed 2-3 controllers. Now i also have comments, explore, chat and board object which also has user object. Now lets say i refresh feed but gets data that is already in realm. As no data is new but since the realm.create with update true has been fired it will generate notification for that user and as this user belongs to other pages as well, nearly 300-400 modification change gets fired without any actual update. This is currently producing some lag in my application. I am worried what will happen if user tries to go deep in the app, the modification call will be 2-3 times the current call.

No news on our side, but I’d like to reiterate that I (and at least a few other people) at Realm consider the current behavior to be suboptimal and would like to implement the changes proposed in this thread. But we don’t have concrete plans to do that right away, hence the prioritization level of S:P2 Backlog indicated by the label on this issue.

@freeubi I couldn’t agree more with @tirrorex, this is definitely NOT implemented yet and it’s a LOT of unnecessary pollution – and of course super error-prone and hard to trace if you missed some newly-added property somewhere. This is a cross-cutting concern and something the framework should be handling itself.

This is especially important for server synching where you can get object updates that only change a specific property, yet you apply the changes to an object all at once (either by applying the JSON or materializing a new object and doing an upsert - ie: createOrUpdate). Thus to have a production-ready, real-time application (the core value prop of Realm) you NEED to have this in the framework.

Issue NOT fixed. ETA on it coming? Common guys…

I’m also very interested in the timeline of this fix. I have a similar scenario as @jovanpreet. Would like to only be notified of objects who’s properties (1 or more) have actually changed.

No, no news on my side, but the project you’ve created to me looks to be an issue on our side. That should be helpful in figuring out what’s happening, if it is indeed a bug, and if so how we can fix it.

I’ll need to spend the proper time to investigate in order to get back to you on that. I’ll post here once I’ve done that.

I originally was creating my objects with out a primary key and parsing updates for my objects from my returned JOSN from the server. I would then apply these updates with out discrimination and noticed way to many modifications being passed through in my addNotificationBlock (On a List<Object> of the highest level objects in my data graph). My fault I told myself. I went and changed all my realm objects to have a primary key and passed updates using the realm.add(self, update: true) method.

I was still getting all the notifications!! I’m glad I found this thread. I simply stopped reloading my UITableViewCell’s and I can manage the ~3 second spike of CPU usage. When reloading the UITableViewCell’s the CPU was pegged at 100% for a few seconds.

I guess what I’m trying to say is that I would love it if Realm did actually only send notifications for actual changes. If not, I guess I can propagate my own custom notifications on the server sync’s.