Skip to content

Commit 85e8bd7

Browse files
authored
fix: Saving ParseFiles occurs on the background queue (#230)
* Save ParseFiles on background queue * Fix test cases and allow to specify return queue on sync * Add internal convenience initializer * Test files in Playgrounds * Test multiple files in playground * Fix warnings in Playgrounds
1 parent f767242 commit 85e8bd7

File tree

11 files changed

+176
-36
lines changed

11 files changed

+176
-36
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.9.8...main)
55
* _Contributing to this repo? Add info about your change here to be included in the next release_
66

7+
### 1.9.8
8+
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.9.8...1.9.9)
9+
10+
__Fixes__
11+
- Saving ParseObjects with ParseFile properties now saves files on background queue ([#230](https://github.com/parse-community/Parse-Swift/pull/230)), thanks to [Corey Baker](https://github.com/cbaker6).
12+
713
### 1.9.8
814
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.9.7...1.9.8)
915

ParseSwift.playground/Pages/10 - Cloud Code.xcplaygroundpage/Contents.swift

+8-2
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,16 @@ cloudError.runFunction { result in
6767
case 3000:
6868
print("Received Cloud Code error: \(error)")
6969
default:
70-
assertionFailure("Should have received code \"3000\"")
70+
assertionFailure("""
71+
Should have received code \"3000\"
72+
Instead received \(error)
73+
""")
7174
}
7275
default:
73-
assertionFailure("Should have been case \"other\"")
76+
assertionFailure("""
77+
Should have received code \"other\"
78+
Instead received \(error)
79+
""")
7480
}
7581
}
7682
}

ParseSwift.playground/Pages/15 - Custom ObjectId.xcplaygroundpage/Contents.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ score.save { result in
7070
changedScore.score = 200
7171
changedScore.save { result in
7272
switch result {
73-
case .success(var savedChangedScore):
73+
case .success(let savedChangedScore):
7474
assert(savedChangedScore.score == 200)
7575
assert(savedScore.objectId == savedChangedScore.objectId)
7676
print("Updated score: \(savedChangedScore)")

ParseSwift.playground/Pages/17 - SwiftUI - Finding Objects.xcplaygroundpage/Contents.swift

+40
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ struct GameScore: ParseObject, Identifiable {
3838
var score: Int = 0
3939
var location: ParseGeoPoint?
4040
var name: String?
41+
var myFiles: [ParseFile]?
4142

4243
//: Custom initializer.
4344
init(name: String, score: Int) {
@@ -55,9 +56,43 @@ struct ContentView: View {
5556
@ObservedObject var viewModel = GameScore.query("score" > 2)
5657
.order([.descending("score")])
5758
.viewModel
59+
@State var name = ""
60+
@State var score = ""
61+
@State var isShowingAction = false
62+
@State var savedLabel = ""
5863

5964
var body: some View {
6065
NavigationView {
66+
VStack {
67+
TextField("Name", text: $name)
68+
TextField("Score", text: $score)
69+
Button(action: {
70+
guard let scoreValue = Int(score),
71+
let linkToFile = URL(string: "https://parseplatform.org/img/logo.svg") else {
72+
return
73+
}
74+
var score = GameScore(name: name,
75+
score: scoreValue)
76+
//: Create new `ParseFile` for saving.
77+
let file1 = ParseFile(name: "file1.svg",
78+
cloudURL: linkToFile)
79+
let file2 = ParseFile(name: "file2.svg",
80+
cloudURL: linkToFile)
81+
score.myFiles = [file1, file2]
82+
score.save { result in
83+
switch result {
84+
case .success:
85+
savedLabel = "Saved score"
86+
self.viewModel.find()
87+
case .failure(let error):
88+
savedLabel = "Error: \(error.message)"
89+
}
90+
isShowingAction = true
91+
}
92+
}, label: {
93+
Text("Save score")
94+
})
95+
}
6196
if let error = viewModel.error {
6297
Text(error.description)
6398
} else {
@@ -75,6 +110,11 @@ struct ContentView: View {
75110
Spacer()
76111
}.onAppear(perform: {
77112
viewModel.find()
113+
}).alert(isPresented: $isShowingAction, content: {
114+
Alert(title: Text("GameScore"),
115+
message: Text(savedLabel),
116+
dismissButton: .default(Text("Ok"), action: {
117+
}))
78118
})
79119
}
80120
}

ParseSwift.playground/Pages/7 - GeoPoint.xcplaygroundpage/Contents.swift

+15-6
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ do {
7777

7878
assert(scores.count >= 1)
7979
scores.forEach { (score) in
80-
print("Someone with objectId \"\(score.objectId!)\" has a score of \"\(score.score)\" near me")
80+
print("""
81+
Someone with objectId \"\(score.objectId!)\"
82+
has a score of \"\(String(describing: score.score))\" near me
83+
""")
8184
}
8285

8386
case .failure(let error):
@@ -97,7 +100,10 @@ querySorted.find { results in
97100

98101
assert(scores.count >= 1)
99102
scores.forEach { (score) in
100-
print("Someone with objectId \"\(score.objectId!)\" has a score of \"\(score.score)\" near me")
103+
print("""
104+
Someone with objectId \"\(score.objectId!)\"
105+
has a score of \"\(String(describing: score.score))\" near me
106+
""")
101107
}
102108

103109
case .failure(let error):
@@ -116,7 +122,7 @@ query2.find { results in
116122
scores.forEach { (score) in
117123
print("""
118124
Someone with objectId \"\(score.objectId!)\" has a
119-
score of \"\(score.score)\" near me which is greater than 9
125+
score of \"\(String(describing: score.score))\" near me which is greater than 9
120126
""")
121127
}
122128

@@ -133,7 +139,8 @@ query3.find { results in
133139

134140
scores.forEach { (score) in
135141
print("""
136-
Someone has a score of \"\(score.score)\" with no geopoint \(String(describing: score.location))
142+
Someone has a score of \"\(String(describing: score.score))\"
143+
with no geopoint \(String(describing: score.location))
137144
""")
138145
}
139146

@@ -151,7 +158,8 @@ query4.find { results in
151158
assert(scores.count >= 1)
152159
scores.forEach { (score) in
153160
print("""
154-
Someone has a score of \"\(score.score)\" with geopoint \(String(describing: score.location))
161+
Someone has a score of \"\(String(describing: score.score))\"
162+
with geopoint \(String(describing: score.location))
155163
""")
156164
}
157165

@@ -170,7 +178,8 @@ query7.find { results in
170178

171179
scores.forEach { (score) in
172180
print("""
173-
Someone has a score of \"\(score.score)\" with geopoint using OR \(String(describing: score.location))
181+
Someone has a score of \"\(String(describing: score.score))\"
182+
with geopoint using OR \(String(describing: score.location))
174183
""")
175184
}
176185

Sources/ParseSwift/API/URLSession+extensions.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ extension URLSession {
135135
completion(self.makeResult(request: request,
136136
responseData: responseData,
137137
urlResponse: urlResponse,
138-
responseError: responseError, mapper: mapper))
138+
responseError: responseError,
139+
mapper: mapper))
139140
}.resume()
140141
}
141142
}

Sources/ParseSwift/Objects/ParseObject.swift

+1-2
Original file line numberDiff line numberDiff line change
@@ -789,8 +789,7 @@ extension ParseObject {
789789
}
790790

791791
try savableFiles.forEach {
792-
let file = $0
793-
filesFinishedSaving[file.localId] = try $0.save(options: options)
792+
filesFinishedSaving[$0.localId] = try $0.save(options: options, callbackQueue: queue)
794793
}
795794
}
796795
completion(objectsFinishedSaving, filesFinishedSaving, nil)

Sources/ParseSwift/ParseConstants.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Foundation
1010

1111
enum ParseConstants {
1212
static let sdk = "swift"
13-
static let version = "1.9.8"
13+
static let version = "1.9.9"
1414
static let fileManagementDirectory = "parse/"
1515
static let fileManagementPrivateDocumentsDirectory = "Private Documents/"
1616
static let fileManagementLibraryDirectory = "Library/"

Sources/ParseSwift/Types/ParseError.swift

+10-5
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ public struct ParseError: ParseType, Decodable, Swift.Error {
1818
public let message: String
1919
/// An error value representing a custom error from the Parse Server.
2020
public let otherCode: Int?
21-
init(code: Code, message: String) {
22-
self.code = code
23-
self.message = message
24-
self.otherCode = nil
25-
}
2621

2722
enum CodingKeys: String, CodingKey {
2823
case code
@@ -369,6 +364,16 @@ extension ParseError {
369364
}
370365
}
371366

367+
// MARK: Convenience Implementations
368+
extension ParseError {
369+
370+
init(code: Code, message: String) {
371+
self.code = code
372+
self.message = message
373+
self.otherCode = nil
374+
}
375+
}
376+
372377
// MARK: Decodable
373378
extension ParseError {
374379

0 commit comments

Comments
 (0)