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

Live query update #81

Merged
merged 4 commits into from
Feb 23, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Adds support for update operator
  • Loading branch information
flovilmart committed Oct 25, 2016
commit 22069d8e08b2d00f58b9e35175b62bf41f72b16f
35 changes: 32 additions & 3 deletions Sources/ParseLiveQuery/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,31 @@ extension Client {
return handler
}

public func update<T>(
_ fromQuery: PFQuery<T>,
toQuery query: PFQuery<T>,
subclassType: T.Type = T.self
) where T: PFObject {
update(toQuery: query) { $0.query == fromQuery }
}

public func update<T>(
_ subscription: Subscription<T>,
toQuery query: PFQuery<T>) where T: PFObject {
update(toQuery: query) {
$0.subscriptionHandler === subscription
}
}

func update<T>(
toQuery query: PFQuery<T>,
matching matcher: @escaping (SubscriptionRecord) -> Bool
) where T: PFObject {
subscriptions(matching: matcher).forEach {
_ = sendOperationAsync(.update(requestId: $0.requestId, query: query as! PFQuery<PFObject>))
}
}

/**
Unsubscribes all current subscriptions for a given query.

Expand All @@ -184,12 +209,16 @@ extension Client {
}

func unsubscribe(matching matcher: @escaping (SubscriptionRecord) -> Bool) {
subscriptions.filter {
matcher($0)
}.forEach {
subscriptions(matching: matcher).forEach {
_ = sendOperationAsync(.unsubscribe(requestId: $0.requestId))
}
}

func subscriptions(matching matcher: @escaping (SubscriptionRecord) -> Bool) -> [SubscriptionRecord] {
return subscriptions.filter {
matcher($0)
}
}

}

Expand Down
4 changes: 4 additions & 0 deletions Sources/ParseLiveQuery/Internal/Operation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Parse
enum ClientOperation {
case connect(applicationId: String, sessionToken: String)
case subscribe(requestId: Client.RequestId, query: PFQuery<PFObject>)
case update(requestId: Client.RequestId, query: PFQuery<PFObject>)
case unsubscribe(requestId: Client.RequestId)

var JSONObjectRepresentation: [String : Any] {
Expand All @@ -23,6 +24,9 @@ enum ClientOperation {
case .subscribe(let requestId, let query):
return [ "op": "subscribe", "requestId": requestId.value, "query": Dictionary<String, AnyObject>(query: query) ]

case .update(let requestId, let query):
return [ "op": "update", "requestId": requestId.value, "query": Dictionary<String, AnyObject>(query: query) ]

case .unsubscribe(let requestId):
return [ "op": "unsubscribe", "requestId": requestId.value ]
}
Expand Down