Skip to content

Commit

Permalink
fix: query count and withCount always returning 0 (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
cbaker6 authored Feb 24, 2023
1 parent 56077f0 commit 9c8f139
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ __New features__
* The max connection attempts for LiveQuery can now be changed when initializing the SDK ([#43](https://github.com/netreconlab/Parse-Swift/pull/43)), thanks to [Corey Baker](https://github.com/cbaker6).

__Fixes__
* Fixed query count and withCount returning 0 when the SDK is configured to use GET for queries ([#61](https://github.com/netreconlab/Parse-Swift/pull/61)), thanks to [Corey Baker](https://github.com/cbaker6).
* Fixed ambiguous ParseAnalytics trackAppOpenned ([#55](https://github.com/netreconlab/Parse-Swift/pull/55)), thanks to [Corey Baker](https://github.com/cbaker6).
* Refactored playground mount to be "/parse" instead "/1". Also do not require url when decoding a ParseFile ([#52](https://github.com/netreconlab/Parse-Swift/pull/52)), thanks to [Corey Baker](https://github.com/cbaker6).
* Fixed issues that can cause cache misses when querying ([#46](https://github.com/netreconlab/Parse-Swift/pull/46)), thanks to [Corey Baker](https://github.com/cbaker6).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,20 @@ query.first { results in
//: Query first asynchronously (preferred way) - Performs work on background
//: queue and returns to specified callbackQueue.
//: If no callbackQueue is specified it returns to main queue.
query.count { results in
switch results {
case .success(let count):
print("Found total scores: \(count)")

case .failure(let error):
if error.containedIn([.objectNotFound, .invalidQuery]) {
assertionFailure("The query is invalid or the object is not found.")
} else {
assertionFailure("Error querying: \(error)")
}
}
}

query.withCount { results in
switch results {
case .success(let (score, count)):
Expand Down
2 changes: 1 addition & 1 deletion Sources/ParseSwift/ParseConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation

enum ParseConstants {
static let sdk = "swift"
static let version = "5.0.0-beta.7"
static let version = "5.0.0-beta.8"
static let fileManagementDirectory = "parse/"
static let fileManagementPrivateDocumentsDirectory = "Private Documents/"
static let fileManagementLibraryDirectory = "Library/"
Expand Down
8 changes: 4 additions & 4 deletions Sources/ParseSwift/Types/Query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1462,7 +1462,7 @@ extension Query {
if !Parse.configuration.isUsingPostForQuery {
return API.NonParseBodyCommand(method: .GET,
path: query.endpoint,
params: try getQueryParameters()) {
params: try query.getQueryParameters()) {
if let decoded = try ParseCoding.jsonDecoder().decode(QueryResponse<T>.self, from: $0).results.first {
return decoded
}
Expand All @@ -1482,12 +1482,12 @@ extension Query {

func countCommand() throws -> API.NonParseBodyCommand<Query<ResultType>, Int> {
var query = self
query.limit = 1
query.limit = 0
query.isCount = true
if !Parse.configuration.isUsingPostForQuery {
return API.NonParseBodyCommand(method: .GET,
path: query.endpoint,
params: try getQueryParameters()) {
params: try query.getQueryParameters()) {
try ParseCoding.jsonDecoder().decode(QueryResponse<T>.self, from: $0).count ?? 0
}
} else {
Expand All @@ -1505,7 +1505,7 @@ extension Query {
if !Parse.configuration.isUsingPostForQuery {
return API.NonParseBodyCommand(method: .GET,
path: query.endpoint,
params: try getQueryParameters()) {
params: try query.getQueryParameters()) {
let decoded = try ParseCoding.jsonDecoder().decode(QueryResponse<T>.self, from: $0)
return (decoded.results, decoded.count ?? 0)
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/ParseSwiftTests/ParseQueryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1018,7 +1018,7 @@ class ParseQueryTests: XCTestCase { // swiftlint:disable:this type_body_length
let query = GameScore.query()
let command = try query.countCommand()
// swiftlint:disable:next line_length
let expected = "{\"body\":{\"_method\":\"GET\",\"count\":true,\"limit\":1,\"skip\":0,\"where\":{}},\"method\":\"POST\",\"path\":\"\\/classes\\/GameScore\"}"
let expected = "{\"body\":{\"_method\":\"GET\",\"count\":true,\"limit\":0,\"skip\":0,\"where\":{}},\"method\":\"POST\",\"path\":\"\\/classes\\/GameScore\"}"
let encoded = try ParseCoding.jsonEncoder()
.encode(command)
let decoded = try XCTUnwrap(String(data: encoded, encoding: .utf8))
Expand Down

0 comments on commit 9c8f139

Please sign in to comment.