Skip to content

Commit

Permalink
docs: add async/await playground example (#283)
Browse files Browse the repository at this point in the history
* docs: add async/await playground example

* Remove new test for linux

* nit

* playgrounds defaults to using macOS

* add Windows to README
  • Loading branch information
cbaker6 authored Nov 16, 2021
1 parent a121bde commit 863781b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)")
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
![parse-repository-header-sdk-swift](https://user-images.githubusercontent.com/5673677/138289926-a26ca0bd-1713-4c30-b69a-acd840ccead0.png)

<h3 align="center">iOS · macOS · watchOS · tvOS · Linux</h3>
<h3 align="center">iOS · macOS · watchOS · tvOS · Linux · Windows</h3>

---

Expand Down
8 changes: 8 additions & 0 deletions Tests/ParseSwiftTests/ParseQueryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 863781b

Please sign in to comment.