Saturday, November 21, 2026
HomeiOS Developmentios - Can I take advantage of RunLoop for realtime object observing...

ios – Can I take advantage of RunLoop for realtime object observing in Swift?

[ad_1]

I’ve a Course of object (is form of Mannequin). Inside a course of, I’ve an array of tuples

var listeners: [(WeakContainer<AnyObject>, (S) -> ())]

WeakContainer handles a weak ref inside. I must take away a component from the listeners array when Conainer’s worth turns into nil. My code appears to be like like this:

class Course of<S> {
    
    typealias Handler = (S) -> ()
    typealias Listener = (container: Weak<AnyObject>, handler: StateHandler)
    
    var listeners: [Listener] = []
    non-public let runLoop = RunLoop()
    
    init() {
        runLoop.run(mode: .default, earlier than: .now)
        runLoop.carry out { [weak self] in
            self?.state.listeners.removeAll {
                $0.container.worth == nil
            }
        }
    }
    
}

Is it right utilization of RunLoop for my targets or not? Why?
I can be grateful in case you suggest a extra appropriate different for my case (monitoring for the item turns into nil).

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments