Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add score property, withCount, and matchesText options #306

Merged
merged 5 commits into from
Jan 1, 2022
Merged
Show file tree
Hide file tree
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
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@

### main

[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/2.5.1...main)
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/3.0.0...main)
* _Contributing to this repo? Add info about your change here to be included in the next release_

### 3.0.0
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/2.5.1...3.0.0)

__Improvements__
- (Breaking Change) Adds options to matchesText query constraint along with the ability to see matching score. The compiler should recommend the new score property to all ParseObjects ([#306](https://github.com/parse-community/Parse-Swift/pull/306)), thanks to [Corey Baker](https://github.com/cbaker6).
- Adds withCount query ([#306](https://github.com/parse-community/Parse-Swift/pull/306)), thanks to [Corey Baker](https://github.com/cbaker6).

### 2.5.1
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/2.5.0...2.5.1)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,18 @@ struct GameScore: ParseObject, ParseObjectMutable {
var createdAt: Date?
var updatedAt: Date?
var ACL: ParseACL?
var score: Double?

//: Your own properties.
var score: Int = 0
var points: Int = 0
}

//: It's recommended to place custom initializers in an extension
//: to preserve the convenience initializer.
extension GameScore {

init(score: Int) {
self.score = score
init(points: Int) {
self.points = points
}

init(objectId: String?) {
Expand All @@ -60,6 +61,7 @@ struct GameData: ParseObject {
var createdAt: Date?
var updatedAt: Date?
var ACL: ParseACL?
var score: Double?

//: Your own properties.
var polygon: ParsePolygon?
Expand All @@ -79,8 +81,8 @@ extension GameData {
}

//: Define initial GameScores.
let score = GameScore(score: 10)
let score2 = GameScore(score: 3)
let score = GameScore(points: 10)
let score2 = GameScore(points: 3)

/*: Save asynchronously (preferred way) - Performs work on background
queue and returns to specified callbackQueue.
Expand All @@ -92,25 +94,25 @@ score.save { result in
assert(savedScore.objectId != nil)
assert(savedScore.createdAt != nil)
assert(savedScore.updatedAt != nil)
assert(savedScore.score == 10)
assert(savedScore.points == 10)

/*: To modify, need to make it a var as the value type
was initialized as immutable. Using `mutable`
allows you to only send the updated keys to the
parse server as opposed to the whole object.
*/
var changedScore = savedScore.mutable
changedScore.score = 200
changedScore.points = 200
changedScore.save { result in
switch result {
case .success(var savedChangedScore):
assert(savedChangedScore.score == 200)
assert(savedChangedScore.points == 200)
assert(savedScore.objectId == savedChangedScore.objectId)

/*: Note that savedChangedScore is mutable since it's
a var after success.
*/
savedChangedScore.score = 500
savedChangedScore.points = 500

case .failure(let error):
assertionFailure("Error saving: \(error)")
Expand All @@ -132,7 +134,7 @@ var score2ForFetchedLater: GameScore?
otherResults.forEach { otherResult in
switch otherResult {
case .success(let savedScore):
print("Saved \"\(savedScore.className)\" with score \(savedScore.score) successfully")
print("Saved \"\(savedScore.className)\" with points \(savedScore.points) successfully")
if index == 1 {
score2ForFetchedLater = savedScore
}
Expand All @@ -148,15 +150,15 @@ var score2ForFetchedLater: GameScore?
}

//: Saving multiple GameScores at once using a transaction.
//: Currently doesn't work on mongo
//: May not work on MongoDB depending on your configuration.
/*[score, score2].saveAll(transaction: true) { results in
switch results {
case .success(let otherResults):
var index = 0
otherResults.forEach { otherResult in
switch otherResult {
case .success(let savedScore):
print("Saved \"\(savedScore.className)\" with score \(savedScore.score) successfully")
print("Saved \"\(savedScore.className)\" with points \(savedScore.points) successfully")
if index == 1 {
score2ForFetchedLater = savedScore
}
Expand Down Expand Up @@ -184,7 +186,7 @@ assert(savedScore != nil)
assert(savedScore?.objectId != nil)
assert(savedScore?.createdAt != nil)
assert(savedScore?.updatedAt != nil)
assert(savedScore?.score == 10)
assert(savedScore?.points == 10)

/*: To modify, need to make it a var as the value type
was initialized as immutable. Using `mutable`
Expand All @@ -194,7 +196,7 @@ assert(savedScore?.score == 10)
guard var changedScore = savedScore?.mutable else {
fatalError()
}
changedScore.score = 200
changedScore.points = 200

let savedChangedScore: GameScore?
do {
Expand All @@ -205,7 +207,7 @@ do {
}

assert(savedChangedScore != nil)
assert(savedChangedScore!.score == 200)
assert(savedChangedScore!.points == 200)
assert(savedScore!.objectId == savedChangedScore!.objectId)

let otherResults: [(Result<GameScore, ParseError>)]?
Expand All @@ -220,14 +222,14 @@ assert(otherResults != nil)
otherResults!.forEach { result in
switch result {
case .success(let savedScore):
print("Saved \"\(savedScore.className)\" with score \(savedScore.score) successfully")
print("Saved \"\(savedScore.className)\" with points \(savedScore.points) successfully")
case .failure(let error):
assertionFailure("Error saving: \(error)")
}
}

//: Now we will create another object and delete it.
let score3 = GameScore(score: 30)
let score3 = GameScore(points: 30)

//: Save the score and store it in "scoreToDelete".
var scoreToDelete: GameScore!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,28 @@ PlaygroundPage.current.needsIndefiniteExecution = true
initializeParse()

//: Create your own value typed `ParseCloud` type.
struct Cloud: ParseCloud {
struct Hello: ParseCloud {

//: Return type of your Cloud Function
typealias ReturnType = String

//: These are required by `ParseCloud`
var functionJobName: String
//: These are required by `ParseCloud`, you can set the default value to make it easier
//: to use.
var functionJobName: String = "hello"

//: If your cloud function takes arguments, they can be passed by creating properties:
//var argument1: [String: Int] = ["test": 5]
}

//: Create another `ParseCloud` type.
struct TestCloudCode: ParseCloud {

//: Return type of your Cloud Function
typealias ReturnType = String

//: These are required by `ParseCloud`, you can set the default value to make it easier
//: to use.
var functionJobName: String = "testCloudCode"

//: If your cloud function takes arguments, they can be passed by creating properties:
//var argument1: [String: Int] = ["test": 5]
Expand All @@ -32,9 +47,9 @@ struct Cloud: ParseCloud {
return 'Hello world!';
});
*/
let cloud = Cloud(functionJobName: "hello")
let hello = Hello()

cloud.runFunction { result in
hello.runFunction { result in
switch result {
case .success(let response):
print("Response from cloud function: \(response)")
Expand All @@ -50,9 +65,9 @@ cloud.runFunction { result in
throw new Parse.Error(3000, "cloud has an error on purpose.");
});
*/
let cloudError = Cloud(functionJobName: "testCloudCode")
let testCloudCode = TestCloudCode()

cloudError.runFunction { result in
testCloudCode.runFunction { result in
switch result {
case .success:
assertionFailure("Should have thrown a custom error")
Expand Down Expand Up @@ -91,17 +106,18 @@ struct GameScore: ParseObject {
var createdAt: Date?
var updatedAt: Date?
var ACL: ParseACL?
var score: Double?

//: Your own properties.
var score: Int = 0
var points: Int = 0
}

//: It's recommended to place custom initializers in an extension
//: to preserve the convenience initializer.
extension GameScore {
//: Custom initializer.
init(score: Int) {
self.score = score
init(points: Int) {
self.points = points
}

init(objectId: String?) {
Expand All @@ -110,7 +126,7 @@ extension GameScore {
}

//: Define a GameScore.
let score = GameScore(score: 10)
let score = GameScore(points: 10)

//: Save asynchronously (preferred way) with the context option.
score.save(options: [.context(["hello": "world"])]) { result in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ struct GameScore: ParseObject {
var createdAt: Date?
var updatedAt: Date?
var ACL: ParseACL?
var score: Double?

//: Your own properties.
var score: Int = 0
var points: Int = 0
var location: ParseGeoPoint?
var name: String?
}
Expand All @@ -27,9 +28,9 @@ struct GameScore: ParseObject {
//: to preserve the convenience initializer.
extension GameScore {
//: Custom initializer.
init(name: String, score: Int) {
init(name: String, points: Int) {
self.name = name
self.score = score
self.points = points
}
}

Expand All @@ -54,7 +55,7 @@ if let socket = ParseLiveQuery.getDefault() {
}

//: Create a query just as you normally would.
var query = GameScore.query("score" < 11)
var query = GameScore.query("points" < 11)

//: This is how you subscribe to your created query using callbacks.
let subscription = query.subscribeCallback!
Expand Down Expand Up @@ -126,10 +127,10 @@ ParseLiveQuery.client?.sendPing { error in
}

//: Create a new query.
var query2 = GameScore.query("score" > 50)
var query2 = GameScore.query("points" > 50)

//: Select the fields you are interested in receiving.
query2.fields("score")
query2.fields("points")

//: Subscribe to your new query.
let subscription2 = query2.subscribeCallback!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ struct User: ParseUser {
var createdAt: Date?
var updatedAt: Date?
var ACL: ParseACL?
var score: Double?

//: These are required by `ParseUser`.
var username: String?
Expand All @@ -38,6 +39,7 @@ struct Role<RoleUser: ParseUser>: ParseRole {
var createdAt: Date?
var updatedAt: Date?
var ACL: ParseACL?
var score: Double?

//: Provided by Role.
var name: String
Expand All @@ -54,17 +56,18 @@ struct GameScore: ParseObject, ParseObjectMutable {
var createdAt: Date?
var updatedAt: Date?
var ACL: ParseACL?
var score: Double?

//: Your own properties.
var score: Int = 0
var points: Int = 0
}

//: It's recommended to place custom initializers in an extension
//: to preserve the convenience initializer.
extension GameScore {

init(score: Int) {
self.score = score
init(points: Int) {
self.points = points
}

init(objectId: String?) {
Expand Down Expand Up @@ -248,8 +251,8 @@ do {
//: All `ParseObject`s have a `ParseRelation` attribute that be used on instances.
//: For example, the User has:
var relation = User.current!.relation
let score1 = GameScore(score: 53)
let score2 = GameScore(score: 57)
let score1 = GameScore(points: 53)
let score2 = GameScore(points: 57)

//: Add new child relationships.
[score1, score2].saveAll { result in
Expand All @@ -263,7 +266,7 @@ let score2 = GameScore(score: 57)
switch result {
case .success(let saved):
print("The relation saved successfully: \(saved)")
print("Check \"scores\" field in your \"_User\" class in Parse Dashboard.")
print("Check \"pointss\" field in your \"_User\" class in Parse Dashboard.")

case .failure(let error):
print("Error saving role: \(error)")
Expand Down
Loading