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

Fix objective-c header compatibility, due to swift compiler bugs. #18

Merged
merged 1 commit into from
Mar 28, 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
73 changes: 44 additions & 29 deletions Sources/ParseLiveQuery/ObjCCompat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,51 +61,47 @@ public protocol ObjCCompat_SubscriptionHandling {
optional func didUnsubscribe(query: PFQuery, client: Client)
}

// HACK: Compiler bug causes enums that are declared in structs that are marked as @objc to not actually be emitted by
// the compiler (lolwut?). Moving this to global scope fixes the problem, but we can't change the objc name of an enum
// either, so we pollute the swift namespace here.
// TODO: Fix this eventually.

/**
A type of an update event on a specific object from the live query server.
*/
@objc
public enum PFLiveQueryEventType: Int {
/// The object has been updated, and is now included in the query.
case Entered
/// The object has been updated, and is no longer included in the query.
case Left
/// The object has been created, and is a part of the query.
case Created
/// The object has been updated, and is still a part of the query.
case Updated
/// The object has been deleted, and is no longer included in the query.
case Deleted
}

/**
This struct wraps up all of our Objective-C compatibility layer. You should never need to touch this if you're using Swift.
*/
public struct ObjCCompat {
private init() { }

/**
A type of an update event on a specific object from the live query server.
*/
@objc
public enum PFLiveQueryEventType: Int {
/// The object has been updated, and is now included in the query.
case Entered
/// The object has been updated, and is no longer included in the query.
case Left
/// The object has been created, and is a part of the query.
case Created
/// The object has been updated, and is still a part of the query.
case Updated
/// The object has been deleted, and is no longer included in the query.
case Deleted
}

/**
Represents an update on a specific object from the live query server.
*/
@objc(PFLiveQueryEvent)
public class Event: NSObject {
/// Type of the event.
@objc
public let type: PFLiveQueryEventType

/// Object this event is for.
@objc
public let object: PFObject

init<T>(event: ParseLiveQuery.Event<T>) {
(type, object) = {
switch event {
case .Entered(let object): return (.Entered, object)
case .Left(let object): return (.Left, object)
case .Created(let object): return (.Created, object)
case .Updated(let object): return (.Updated, object)
case .Deleted(let object): return (.Deleted, object)
}
}()
}

init(type: PFLiveQueryEventType, object: PFObject) {
self.type = type
self.object = object
Expand Down Expand Up @@ -329,6 +325,25 @@ extension Client {
}
}

// HACK: Another compiler bug - if you have a required initializer with a generic type, the compiler simply refuses to
// emit the entire class altogether. Moving this to an extension for now solves the issue.

extension ObjCCompat.Event {
convenience init<T>(event: ParseLiveQuery.Event<T>) {
let results: (type: PFLiveQueryEventType, object: PFObject) = {
switch event {
case .Entered(let object): return (.Entered, object)
case .Left(let object): return (.Left, object)
case .Created(let object): return (.Created, object)
case .Updated(let object): return (.Updated, object)
case .Deleted(let object): return (.Deleted, object)
}
}()

self.init(type: results.type, object: results.object)
}
}

extension PFQuery {
/**
Register this PFQuery for updates with Live Queries.
Expand Down