samedi 20 mars 2021

Swift design pattern for faulting. Similar to faulting NSManagedObject

CoreData has the principle of faulting. That means you get an object that is "in fault". When you want to access its values, the object "fires". Which means that it transparently loads it data from, in this case, the database.

CoreData was/is largely written in Objective-c. In Objective-c one object can pretend to be another. So when the object "fires", the faulted (i.e. placeholder) object is switched out with the real object.

When using NSManagedObject subclasses, there is no need to consider faulting or a requirement to write a "real" class and a "fault" class. It just works behind the screens.

How do you do something like this in Swift (which is statically typed)? What is the underlying design pattern?

It kind of looks like a "proxy" design pattern. Though with a proxy you would have to write two classes: a proxy class and the "real" class.

Aucun commentaire:

Enregistrer un commentaire