Observer pattern wrapper class is just not clear

What am I missing about the implementation of the wrapper class? I don’t understand the code in Observable.swift at all. Please help me with a broad overview of what each class, struct, property, and method are doing in this swift file. I don’t just want to drag and drop this wrapper class in my project and use it everywhere. Please help.

@jrg.developer Can you please help with this when you get a chance? Thank you - much appreciated! :]

Hey @rahzadesign !

I’m not sure if you’re still having trouble with this, but just in case you are or if others have the same question, let me explain this further! :]

A prerequisite to understanding Observable<Type> is that it’s a generic class: it operates on any Type instance, not a specific type. If you’re new to generics, you should go through this tutorial first.

The implementation for Observable<Type> is given around page 133 in version 2.0 of the book (this may be a few pages before/after if you’re reading another version).

At a very high level, Observable<Type> simply creates and maintains Callback objects, which is a private class that’s used for a few purposes:

  1. It’s used so you don’t create a strong reference to whatever observer is registered. That is, if you simply inserted an observer into an Array directly, you’d likely wind up creating a strong reference cycle to it– wherein the observer may actually have the Observable as a property and register as an observer of it as well. This would look like this basically:

    Observer → Observable
    Observable → Array → Observer

    This would cause memory leaks! By using Callback, however, you break this cycle because Callback holds onto the observer as a weak property!

  2. It’s also used to register in which cases the observer should be notified. This is defined as ObservableOptions.

  3. Lastly, it holds onto the closure that should be executed to actually call the observer’s desired code!

With this core understanding, all that’s left in Observable<Type> are methods to add, remove and clean up observers! These should be pretty well explained in the text, but do let me know if you have any questions about how they work in particular.

You may also be wondering, “Why are observers needed at all?” Technically, they aren’t!

However, they are very useful for one reason: They prevent you from having to manually remove registered closures.

This would be a problem, for example, if different class instances (that aren’t owners of an Observable<Type> instance) registered as observers. Imagine, for example, that a singleton had an observable property, which other class instances registered closures against.

By tying the closures to the lifecycle of the observers, you can not call a closure if/when its observer becomes nil! Hence, you don’t have to explicitly have to manually remove any closures, unless you want to do so. ;]

I hope this answer helps clear up confusion about this class! In the next revision of the book, I’ll see if I can add more details to this chapter to help ensure this is very clear from the text.

If you have any further questions about the above, please let me know! :]

1 Like