Skip to content

Avoiding Useless Update Processes

kevindelord edited this page Jun 28, 2016 · 5 revisions

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?

Override this function to inform the manager whether it should update the current entity or not.

This function receives as parameter a NSDictionary object containing information about the database entity to be updated with. You can use the current entity and the parameter to check if an update is needed or not.

For example you might want to verify the updated_at attribute or a version number from the given dictionary against the current entity.

  • If they match then return false to NOT update the entity and avoid some useless actions.

  • Otherwise return true and the current entity will be updated with the updateWithDictionary:inContext: function.

Here is an implementation of the function checking the lastest update_at:

override func shouldUpdateEntityWithDictionary(dictionary: [NSObject : AnyObject]?, inContext savingContext: NSManagedObjectContext) -> 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)
}

Regarding child entities

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. Its children will not get updated and the DKDBManager will not even ask them if they should be.

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.