Skip to content
This repository was archived by the owner on Jun 13, 2023. It is now read-only.

Commit 480c267

Browse files
Properly deliver events back to objective-c
This was bugged, as HandlerConverter would be destroyed as soon as its scope was exited, causing no events to be delivered. By adding an associated object on the subscription that should be held onto by objective-c, it now only gets destroyed when the subscription does. Fixes #7.
1 parent 4531b82 commit 480c267

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

Sources/ParseLiveQuery/ObjCCompat.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -254,26 +254,29 @@ extension Client {
254254
private class HandlerConverter: SubscriptionHandling {
255255
typealias PFObjectSubclass = PFObject
256256

257-
private let handler: ObjCCompat_SubscriptionHandling
257+
private static var associatedObjectKey: Int = 0
258+
private weak var handler: ObjCCompat_SubscriptionHandling?
258259

259260
init(handler: ObjCCompat_SubscriptionHandling) {
260261
self.handler = handler
262+
263+
objc_setAssociatedObject(handler, &HandlerConverter.associatedObjectKey, self, .OBJC_ASSOCIATION_RETAIN)
261264
}
262265

263266
private func didReceive(event: Event<PFObjectSubclass>, forQuery query: PFQuery, inClient client: Client) {
264-
handler.didRecieveEvent?(query, event: ObjCCompat.Event(event: event), client: client)
267+
handler?.didRecieveEvent?(query, event: ObjCCompat.Event(event: event), client: client)
265268
}
266269

267270
private func didEncounter(error: ErrorType, forQuery query: PFQuery, inClient client: Client) {
268-
handler.didRecieveError?(query, error: error as NSError, client: client)
271+
handler?.didRecieveError?(query, error: error as NSError, client: client)
269272
}
270273

271274
private func didSubscribe(toQuery query: PFQuery, inClient client: Client) {
272-
handler.didSubscribe?(query, client: client)
275+
handler?.didSubscribe?(query, client: client)
273276
}
274277

275278
private func didUnsubscribe(fromQuery query: PFQuery, inClient client: Client) {
276-
handler.didUnsubscribe?(query, client: client)
279+
handler?.didUnsubscribe?(query, client: client)
277280
}
278281
}
279282

0 commit comments

Comments
 (0)