From 863781bcab29b0f44437814bf5c03e5fb1ad9527 Mon Sep 17 00:00:00 2001 From: Corey Date: Tue, 16 Nov 2021 15:30:03 -0500 Subject: [PATCH] docs: add async/await playground example (#283) * docs: add async/await playground example * Remove new test for linux * nit * playgrounds defaults to using macOS * add Windows to README --- CHANGELOG.md | 2 +- .../Contents.swift | 24 +++++++++++++++++-- README.md | 2 +- Tests/ParseSwiftTests/ParseQueryTests.swift | 8 +++++++ 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 29b8254b0..e947255d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -497,7 +497,7 @@ __Improvements__ __Fixes__ - Delete current installation during logout ([#52](https://github.com/parse-community/Parse-Swift/pull/52)), thanks to [Corey Baker](https://github.com/cbaker6). -- Parse server supports `$eq`, but this isn't supported by LiveQueryServer, switched to supported ([#48](https://github.com/parse-community/Parse-Swift/pull/48)), thanks to [Corey Baker](https://github.com/cbaker6). +- Parse server supports `$eq`, but this isn't supported by LiveQueryServer, switched to supported ([#49](https://github.com/parse-community/Parse-Swift/pull/49)), thanks to [Corey Baker](https://github.com/cbaker6). - Bug when updating a ParseObject bug where objects was accidently converted to pointers ([#48](https://github.com/parse-community/Parse-Swift/pull/48)), thanks to [Corey Baker](https://github.com/cbaker6). - User logout was calling the wrong endpoint ([#43](https://github.com/parse-community/Parse-Swift/pull/43)), thanks to [Corey Baker](https://github.com/cbaker6) and [Tom Fox](https://github.com/TomWFox). - Fix an issue where ACL was overwritten with nil ([#40](https://github.com/parse-community/Parse-Swift/pull/40)), thanks to [Corey Baker](https://github.com/cbaker6). diff --git a/ParseSwift.playground/Pages/2 - Finding Objects.xcplaygroundpage/Contents.swift b/ParseSwift.playground/Pages/2 - Finding Objects.xcplaygroundpage/Contents.swift index 3a6dcdacc..cdaa88742 100644 --- a/ParseSwift.playground/Pages/2 - Finding Objects.xcplaygroundpage/Contents.swift +++ b/ParseSwift.playground/Pages/2 - Finding Objects.xcplaygroundpage/Contents.swift @@ -23,11 +23,13 @@ struct GameScore: ParseObject { var score: Int? var timeStamp: Date? = Date() var oldScore: Int? + var isHighest: Bool? } var score = GameScore() score.score = 200 score.oldScore = 10 +score.isHighest = true do { try score.save() } catch { @@ -47,7 +49,7 @@ query.limit(2).find(callbackQueue: .main) { results in case .success(let scores): assert(scores.count >= 1) - scores.forEach { (score) in + scores.forEach { score in guard let createdAt = score.createdAt else { fatalError() } assert(createdAt.timeIntervalSince1970 > afterDate.timeIntervalSince1970, "date should be ok") print("Found score: \(score)") @@ -65,12 +67,30 @@ query.limit(2).find(callbackQueue: .main) { results in //: Query synchronously (not preferred - all operations on main queue). let results = try query.find() assert(results.count >= 1) -results.forEach { (score) in +results.forEach { score in guard let createdAt = score.createdAt else { fatalError() } assert(createdAt.timeIntervalSince1970 > afterDate.timeIntervalSince1970, "date should be ok") print("Found score: \(score)") } +//: Query highest score using async/await +#if swift(>=5.5) && canImport(_Concurrency) +import _Concurrency +if #available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *) { + let highestScoresQuery = GameScore.query("isHighest" == true) + Task { + do { + let highestScores = try await highestScoresQuery.find() + highestScores.forEach { score in + print("Found highest score: \(score)") + } + } catch { + print("Error: \(error)") + } + } +} +#endif + //: 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. diff --git a/README.md b/README.md index 726da5ae7..e3031f09f 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ ![parse-repository-header-sdk-swift](https://user-images.githubusercontent.com/5673677/138289926-a26ca0bd-1713-4c30-b69a-acd840ccead0.png) -

iOS · macOS · watchOS · tvOS · Linux

+

iOS · macOS · watchOS · tvOS · Linux · Windows

--- diff --git a/Tests/ParseSwiftTests/ParseQueryTests.swift b/Tests/ParseSwiftTests/ParseQueryTests.swift index fe0ac32ac..667a87e7c 100644 --- a/Tests/ParseSwiftTests/ParseQueryTests.swift +++ b/Tests/ParseSwiftTests/ParseQueryTests.swift @@ -21,6 +21,7 @@ class ParseQueryTests: XCTestCase { // swiftlint:disable:this type_body_length //: Your own properties var score: Int + var isCounts: Bool? //: a custom initializer init() { @@ -1120,6 +1121,13 @@ class ParseQueryTests: XCTestCase { // swiftlint:disable:this type_body_length } #if !os(Linux) && !os(Android) && !os(Windows) + func testWhereKeyEqualToBool() throws { + let query = GameScore.query("isCounts" == true) + let expected = "GameScore ({\"limit\":100,\"skip\":0,\"_method\":\"GET\",\"where\":{\"isCounts\":true}})" + XCTAssertEqual(query.debugDescription, expected) + XCTAssertEqual(query.description, expected) + } + func testWhereKeyEqualToParseObject() throws { var compareObject = GameScore(score: 11) compareObject.objectId = "hello"