-
Notifications
You must be signed in to change notification settings - Fork 5
Avoiding Useless Update Processes
Sometimes you might need to update just some entities and not all of them. But how can you do so when the CRUD process iterates through the whole JSON structure entity by entity?
The DKDBManager lets you choose whether an object needs an update or not. The function - shouldUpdateEntityWithDictionary receives as a parameter the JSON structure as a NSDictionary object. You can use the current entity and the parameter to check if an update is needed or not.
For example; is the updated_at
field the same in the JSON and in the local database?
-
If so, return
false
and no update process will occur -
Otherwise return
true
and the - updateWithDictionary: function will be called.
Due to the cascading process if a parent entity should NOT be updated then its child entities shouldn't either.
In order to avoid useless actions the update verification will stop at the first valid and already updated parent object. Its children will not get updated and the DKDBManager will not even ask them if they should be.
Here is an implementation of the function - shouldUpdateEntityWithDictionary that checks the lastest update_at
:
override public func shouldUpdateEntityWithDictionary(dictionary: [NSObject:AnyObject]!) -> Bool {
// if the updated_at value from the dictionary is a different one.
let lastUpdatedAt = (GET_DATE(dictionary, "updated_at") ?? NSDate())
return (self.updated_at.isEqualToDate(lastUpdatedAt) == false)
}
TIP
The parent updated_at
timestamp should be refresh everytime one of its child gets updated. If so you will never miss a small update of the children or grand children of an entity.