Skip to content

Locally Deprecated Entities

kevindelord edited this page Mar 4, 2016 · 6 revisions

Locally deprecated entities

A locally deprecated entity is a NSManaged object not saved as not deprecated in the DKDBManager.

An object not saved as not deprecated is:

  • An object where the CRUD process didn't go through (isn't sent by the API anymore?).

  • An object marked as invalid (see Invalid model entities section).

  • An object not marked as not deprecated in the DKDBManager.

  • An object where the CRUD process has a state that equals to .Delete

Remove all deprecated entities for every model class within a specific context.

Call this function within a saving context after executing the CRUD process (e.g refreshing the local database from an API).

DKDBManager.saveWithBlock { (savingContext: NSManagedObjectContext) in

	// CRUD process
	Plane.createEntityFromDictionary(data, inContext:savingContext)

	// Remove all deprecated entities (not set as 'not deprecated')
	DKDBManager.removeDeprecatedEntities()

}

Depending on the app and its architecture some entities could get invalid when something important has been removed or updated. Moreover, some could also become invalid after removing the deprecated entities.

In such case, you have to check and remove the deprecated entities manually using deleteIfInvalidInContext:.

class func checkAllDeprecatedEntities(inContext savingContext: NSManagedObjectContext) {

    // Check validity of all planes and remove the invalid ones.
    if let planes = self.allInContext(savingContext) as? [Plane] {
        for plane in planes {
            plane.deleteIfInvalidInContext(savingContext)
        }
    }
}

Save as not deprecated when nothing changed

If an entity did not change in the backend but is still sent through the API, your local database needs to save it as not deprecated. But, as explained previously, if nothing changed the cascading update process will stop on the first entity and not forward the process to its children. By doing so they won't get updated nor saved as not deprecated.

When the + removeDeprecatedEntities occurs those unsaved entities will be removed.

To avoid such problems, implement the function - saveEntityAsNotDeprecated inside your NSManagedObject subclasses. It will be called on the first valid parent model object and will be forwarded to every child.

For more information, see the How to deal with child entities section.

Child entities deprecation

When a parent entity got saved as not deprecated (manually or through the CRUD process) the - updateWithDictionary: function is not called and no CRUD logic reaches the child entities.

To complete the cascading process to save the child entities, each model class should implement the - saveEntityAsNotDeprecated method and forward it to its children.

public override func saveEntityAsNotDeprecated() {
	// Method to save/store the current object AND all its child relations as not deprecated.
	super.saveEntityAsNotDeprecated()

	// Forward the process
	for page in (self.pages as? Set<Page> ?? []) {
		page.saveEntityAsNotDeprecated()
	}
}

For more information about deprecated entities please read the Database matching an API section.