Skip to content

Add batch transactions #89

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

Merged
merged 11 commits into from
Mar 14, 2021
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
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# Parse-Swift Changelog

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

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

__New features__
- Add transaction support to batch saveAll and deleteAll ([#89](https://github.com/parse-community/Parse-Swift/pull/89)), thanks to [Corey Baker](https://github.com/cbaker6).
- Add modifiers to containsString, hasPrefix, hasSuffix ([#85](https://github.com/parse-community/Parse-Swift/pull/85)), thanks to [Corey Baker](https://github.com/cbaker6).

__Improvements__
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
//: For this page, make sure your build target is set to ParseSwift (macOS) and targeting
//: `My Mac` or whatever the name of your mac is. Also be sure your `Playground Settings`
//: in the `File Inspector` is `Platform = macOS`. This is because
//: Keychain in iOS Playgrounds behaves differently. Every page in Playgrounds should
//: be set to build for `macOS` unless specified.

import PlaygroundSupport
import Foundation
import ParseSwift
Expand Down Expand Up @@ -38,7 +44,7 @@ let score = GameScore(score: 10)
let score2 = GameScore(score: 3)

/*: Save asynchronously (preferred way) - Performs work on background
queue and returns to designated on designated callbackQueue.
queue and returns to specified callbackQueue.
If no callbackQueue is specified it returns to main queue.
*/
score.save { result in
Expand Down Expand Up @@ -101,6 +107,29 @@ var score2ForFetchedLater: GameScore?
}
}

//: Saving multiple GameScores at once using a transaction.
[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")
if index == 1 {
score2ForFetchedLater = savedScore
}
index += 1
case .failure(let error):
assertionFailure("Error saving: \(error)")
}
}

case .failure(let error):
assertionFailure("Error saving: \(error)")
}
}

//: Save synchronously (not preferred - all operations on main queue).
let savedScore: GameScore?
do {
Expand Down Expand Up @@ -235,7 +264,7 @@ do {
}

//: Asynchronously (preferred way) deleteAll GameScores based on it's objectId alone.
[scoreToFetch, score2ToFetch].deleteAll { result in
[scoreToFetch, score2ToFetch].deleteAll(transaction: true) { result in
switch result {
case .success(let deletedScores):
deletedScores.forEach { result in
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
//: [Previous](@previous)

//: For this page, make sure your build target is set to ParseSwift (macOS) and targeting
//: `My Mac` or whatever the name of your mac is. Also be sure your `Playground Settings`
//: in the `File Inspector` is `Platform = macOS`. This is because
//: Keychain in iOS Playgrounds behaves differently. Every page in Playgrounds should
//: be set to build for `macOS` unless specified.

import PlaygroundSupport
import Foundation
import ParseSwift
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
//: [Previous](@previous)

//: For this page, make sure your build target is set to ParseSwift (iOS) and targeting
//: an iPhone, iPod, or iPad. Also be sure your `Playground Settings`
//: in the `File Inspector` is `Platform = iOS`. This is because
//: SwiftUI in macOS Playgrounds doesn't seem to build correctly
//: Be sure to switch your target and `Playground Settings` back to
//: macOS after leaving this page.

import PlaygroundSupport
import Foundation
import ParseSwift
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
//: [Previous](@previous)

//: For this page, make sure your build target is set to ParseSwift (macOS) and targeting
//: `My Mac` or whatever the name of your mac is. Also be sure your `Playground Settings`
//: in the `File Inspector` is `Platform = macOS`. This is because
//: Keychain in iOS Playgrounds behaves differently. Every page in Playgrounds should
//: be set to build for `macOS` unless specified.

import PlaygroundSupport
import Foundation
import ParseSwift
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
//: [Previous](@previous)

//: For this page, make sure your build target is set to ParseSwift (macOS) and targeting
//: `My Mac` or whatever the name of your mac is. Also be sure your `Playground Settings`
//: in the `File Inspector` is `Platform = macOS`. This is because
//: Keychain in iOS Playgrounds behaves differently. Every page in Playgrounds should
//: be set to build for `macOS` unless specified.

import PlaygroundSupport
import Foundation
import ParseSwift
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
//: [Previous](@previous)

//: For this page, make sure your build target is set to ParseSwift (macOS) and targeting
//: `My Mac` or whatever the name of your mac is. Also be sure your `Playground Settings`
//: in the `File Inspector` is `Platform = macOS`. This is because
//: Keychain in iOS Playgrounds behaves differently. Every page in Playgrounds should
//: be set to build for `macOS` unless specified.

import PlaygroundSupport
import Foundation
import ParseSwift
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
//: [Previous](@previous)

//: For this page, make sure your build target is set to ParseSwift (macOS) and targeting
//: `My Mac` or whatever the name of your mac is. Also be sure your `Playground Settings`
//: in the `File Inspector` is `Platform = macOS`. This is because
//: Keychain in iOS Playgrounds behaves differently. Every page in Playgrounds should
//: be set to build for `macOS` unless specified.

import PlaygroundSupport
import Foundation
import ParseSwift
Expand Down Expand Up @@ -31,9 +37,9 @@ var query = GameScore.query("score" > 50,
"createdAt" > afterDate)
.order([.descending("score")])

// Query asynchronously (preferred way) - Performs work on background
// queue and returns to designated on designated callbackQueue.
// If no callbackQueue is specified it returns to main queue.
//: Query asynchronously (preferred way) - Performs work on background
//: queue and returns to specified callbackQueue.
//: If no callbackQueue is specified it returns to main queue.
query.limit(2).find(callbackQueue: .main) { results in
switch results {
case .success(let scores):
Expand All @@ -50,7 +56,7 @@ query.limit(2).find(callbackQueue: .main) { results in
}
}

// Query synchronously (not preferred - all operations on main queue).
//: Query synchronously (not preferred - all operations on main queue).
let results = try query.find()
assert(results.count >= 1)
results.forEach { (score) in
Expand All @@ -59,9 +65,9 @@ results.forEach { (score) in
print("Found score: \(score)")
}

// Query first asynchronously (preferred way) - Performs work on background
// queue and returns to designated on designated callbackQueue.
// If no callbackQueue is specified it returns to main queue.
//: 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.first { results in
switch results {
case .success(let score):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
//: [Previous](@previous)

//: For this page, make sure your build target is set to ParseSwift (macOS) and targeting
//: `My Mac` or whatever the name of your mac is. Also be sure your `Playground Settings`
//: in the `File Inspector` is `Platform = macOS`. This is because
//: Keychain in iOS Playgrounds behaves differently. Every page in Playgrounds should
//: be set to build for `macOS` unless specified.

import PlaygroundSupport
import Foundation
PlaygroundPage.current.needsIndefiniteExecution = true
Expand All @@ -25,7 +31,7 @@ struct User: ParseUser {
}

/*: Sign up user asynchronously - Performs work on background
queue and returns to designated on designated callbackQueue.
queue and returns to specified callbackQueue.
If no callbackQueue is specified it returns to main queue.
*/
User.signup(username: "hello", password: "world") { results in
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
//: [Previous](@previous)

//: For this page, make sure your build target is set to ParseSwift (macOS) and targeting
//: `My Mac` or whatever the name of your mac is. Also be sure your `Playground Settings`
//: in the `File Inspector` is `Platform = macOS`. This is because
//: Keychain in iOS Playgrounds behaves differently. Every page in Playgrounds should
//: be set to build for `macOS` unless specified.

import PlaygroundSupport
import Foundation
import ParseSwift
Expand Down Expand Up @@ -47,24 +53,6 @@ struct GameScore: ParseObject {
}
}

/*: Save your first customKey value to your `ParseUser`
Asynchrounously - Performs work on background
queue and returns to designated on designated callbackQueue.
If no callbackQueue is specified it returns to main queue.
*/
User.current?.customKey = "myCustom"
User.current?.score = GameScore(score: 12)
User.current?.targetScore = GameScore(score: 100)
User.current?.save { results in

switch results {
case .success(let updatedUser):
print("Successfully save custom fields of User to ParseServer: \(updatedUser)")
case .failure(let error):
print("Failed to update user: \(error)")
}
}

//: Logging out - synchronously
do {
try User.logout()
Expand All @@ -74,7 +62,7 @@ do {
}

/*: Login - asynchronously - Performs work on background
queue and returns to designated on designated callbackQueue.
queue and returns to specified callbackQueue.
If no callbackQueue is specified it returns to main queue.
*/
User.login(username: "hello", password: "world") { results in
Expand All @@ -94,9 +82,27 @@ User.login(username: "hello", password: "world") { results in
}
}

/*: Save your first `customKey` value to your `ParseUser`
Asynchrounously - Performs work on background
queue and returns to specified callbackQueue.
If no callbackQueue is specified it returns to main queue.
*/
User.current?.customKey = "myCustom"
User.current?.score = GameScore(score: 12)
User.current?.targetScore = GameScore(score: 100)
User.current?.save { results in

switch results {
case .success(let updatedUser):
print("Successfully save custom fields of User to ParseServer: \(updatedUser)")
case .failure(let error):
print("Failed to update user: \(error)")
}
}

//: Looking at the output of user from the previous login, it only has
//: a pointer to the `score`and `targetScore` fields. You can fetch using `include` to
//: get the score.
//: a pointer to the `score` and `targetScore` fields. You can
//: fetch using `include` to get the score.
User.current?.fetch(includeKeys: ["score"]) { result in
switch result {
case .success:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
//: [Previous](@previous)

//: For this page, make sure your build target is set to ParseSwift (macOS) and targeting
//: `My Mac` or whatever the name of your mac is. Also be sure your `Playground Settings`
//: in the `File Inspector` is `Platform = macOS`. This is because
//: Keychain in iOS Playgrounds behaves differently. Every page in Playgrounds should
//: be set to build for `macOS` unless specified.

import PlaygroundSupport
import Foundation
import ParseSwift
Expand Down Expand Up @@ -40,7 +46,7 @@ var score = GameScore(score: 40)
score.ACL = try? ParseACL.defaultACL()

/*: Save asynchronously (preferred way) - Performs work on background
queue and returns to designated on designated callbackQueue.
queue and returns to specified callbackQueue.
If no callbackQueue is specified it returns to main queue.
*/
score.save { result in
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
//: [Previous](@previous)

//: For this page, make sure your build target is set to ParseSwift (macOS) and targeting
//: `My Mac` or whatever the name of your mac is. Also be sure your `Playground Settings`
//: in the `File Inspector` is `Platform = macOS`. This is because
//: Keychain in iOS Playgrounds behaves differently. Every page in Playgrounds should
//: be set to build for `macOS` unless specified.

import PlaygroundSupport
import Foundation
import ParseSwift
Expand Down Expand Up @@ -34,7 +40,7 @@ struct Installation: ParseInstallation {
//: WARNING: All calls on Installation need to be done on the main queue
DispatchQueue.main.async {

/*: Save your first customKey value to your `ParseInstallation`.
/*: Save your first `customKey` value to your `ParseInstallation`.
Performs work on background queue and returns to designated on
designated callbackQueue. If no callbackQueue is specified it
returns to main queue.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
//: [Previous](@previous)

//: For this page, make sure your build target is set to ParseSwift (macOS) and targeting
//: `My Mac` or whatever the name of your mac is. Also be sure your `Playground Settings`
//: in the `File Inspector` is `Platform = macOS`. This is because
//: Keychain in iOS Playgrounds behaves differently. Every page in Playgrounds should
//: be set to build for `macOS` unless specified.

import PlaygroundSupport
import Foundation
import ParseSwift
Expand Down Expand Up @@ -29,7 +35,7 @@ var score = GameScore(score: 10)
score.location = ParseGeoPoint(latitude: 40.0, longitude: -30.0)

/*: Save asynchronously (preferred way) - performs work on background
queue and returns to designated on designated callbackQueue.
queue and returns to specified callbackQueue.
If no callbackQueue is specified it returns to main queue.
*/
score.save { result in
Expand Down
Loading