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

Properly deliver events back to objective-c #11

Merged
merged 1 commit into from
Mar 24, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions Sources/ParseLiveQuery/ObjCCompat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -254,26 +254,29 @@ extension Client {
private class HandlerConverter: SubscriptionHandling {
typealias PFObjectSubclass = PFObject

private let handler: ObjCCompat_SubscriptionHandling
private static var associatedObjectKey: Int = 0
private weak var handler: ObjCCompat_SubscriptionHandling?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we expect this ever to become nil? Not sure on why optional.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weak vars must be optional in swift. Compile error if not.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, I completely missed the fact that it's weak now.


init(handler: ObjCCompat_SubscriptionHandling) {
self.handler = handler

objc_setAssociatedObject(handler, &HandlerConverter.associatedObjectKey, self, .OBJC_ASSOCIATION_RETAIN)
}

private func didReceive(event: Event<PFObjectSubclass>, forQuery query: PFQuery, inClient client: Client) {
handler.didRecieveEvent?(query, event: ObjCCompat.Event(event: event), client: client)
handler?.didRecieveEvent?(query, event: ObjCCompat.Event(event: event), client: client)
}

private func didEncounter(error: ErrorType, forQuery query: PFQuery, inClient client: Client) {
handler.didRecieveError?(query, error: error as NSError, client: client)
handler?.didRecieveError?(query, error: error as NSError, client: client)
}

private func didSubscribe(toQuery query: PFQuery, inClient client: Client) {
handler.didSubscribe?(query, client: client)
handler?.didSubscribe?(query, client: client)
}

private func didUnsubscribe(fromQuery query: PFQuery, inClient client: Client) {
handler.didUnsubscribe?(query, client: client)
handler?.didUnsubscribe?(query, client: client)
}
}

Expand Down