Skip to content

Commit af447a6

Browse files
authored
fix: default cache policy for ParseFile uses SDK default (#274)
* default cache policy for ParseFile uses SDK default * Failing operation set test * improve operation set comparison
1 parent fa1e07f commit af447a6

12 files changed

+91
-47
lines changed

.codecov.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ coverage:
66
status:
77
patch:
88
default:
9-
target: 36
9+
target: auto
1010
changes: false
1111
project:
1212
default:

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22

33
### main
44

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

8+
### 2.2.1
9+
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/2.2.0...2.2.1)
10+
11+
__Fixes__
12+
- Set the default cache policy for `ParseFile` to the default policy set when initializing the SDK ([#274](https://github.com/parse-community/Parse-Swift/pull/274)), thanks to [Corey Baker](https://github.com/cbaker6).
13+
814
### 2.2.0
915
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/2.1.0...2.2.0)
1016

Sources/ParseSwift/Objects/ParseInstallation+combine.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Combine
1212

1313
public extension ParseInstallation {
1414

15-
// MARK: Fetchable - Combine
15+
// MARK: Combine
1616
/**
1717
Fetches the `ParseInstallation` *aynchronously* with the current data from the server
1818
and sets an error if one occurs. Publishes when complete.
@@ -34,7 +34,6 @@ public extension ParseInstallation {
3434
}
3535
}
3636

37-
// MARK: Savable - Combine
3837
/**
3938
Saves the `ParseInstallation` *asynchronously* and publishes when complete.
4039

@@ -51,7 +50,6 @@ public extension ParseInstallation {
5150
}
5251
}
5352

54-
// MARK: Deletable - Combine
5553
/**
5654
Deletes the `ParseInstallation` *asynchronously* and publishes when complete.
5755

Sources/ParseSwift/Objects/ParseObject+combine.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Combine
1212

1313
public extension ParseObject {
1414

15-
// MARK: Fetchable - Combine
15+
// MARK: Combine
1616
/**
1717
Fetches the `ParseObject` *aynchronously* with the current data from the server and sets an error if one occurs.
1818
Publishes when complete.

Sources/ParseSwift/Objects/ParseUser+combine.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public extension ParseUser {
135135
}
136136
}
137137

138-
// MARK: Fetchable - Combine
138+
// MARK: Combine
139139
/**
140140
Fetches the `ParseUser` *aynchronously* with the current data from the server and sets an error if one occurs.
141141
Publishes when complete.
@@ -157,7 +157,6 @@ public extension ParseUser {
157157
}
158158
}
159159

160-
// MARK: Savable - Combine
161160
/**
162161
Saves the `ParseUser` *asynchronously* and publishes when complete.
163162

@@ -186,7 +185,6 @@ public extension ParseUser {
186185
}
187186
}
188187

189-
// MARK: Deletable - Combine
190188
/**
191189
Deletes the `ParseUser` *asynchronously* and publishes when complete.
192190

Sources/ParseSwift/Operations/ParseOperation.swift

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,7 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
3838
throw ParseError(code: .unknownError, message: "Target shouldn't be nil")
3939
}
4040
var mutableOperation = self
41-
if let currentValue = target[keyPath: key.1] as? NSObject,
42-
let updatedValue = value as? NSObject {
43-
if currentValue != updatedValue {
44-
mutableOperation.operations[key.0] = value
45-
mutableOperation.target?[keyPath: key.1] = value
46-
}
47-
} else {
41+
if !isEqual(target[keyPath: key.1], to: value) {
4842
mutableOperation.operations[key.0] = value
4943
mutableOperation.target?[keyPath: key.1] = value
5044
}
@@ -403,6 +397,16 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
403397
try value.encode(to: encoder)
404398
}
405399
}
400+
401+
func isEqual(_ lhs: Encodable, to rhs: Encodable) -> Bool {
402+
guard let lhsData = try? ParseCoding.parseEncoder().encode(lhs),
403+
let lhsString = String(data: lhsData, encoding: .utf8),
404+
let rhsData = try? ParseCoding.parseEncoder().encode(rhs),
405+
let rhsString = String(data: rhsData, encoding: .utf8) else {
406+
return false
407+
}
408+
return lhsString == rhsString
409+
}
406410
}
407411

408412
// MARK: Savable

Sources/ParseSwift/ParseConstants.swift

Lines changed: 1 addition & 1 deletion
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 = "2.2.0"
13+
static let version = "2.2.1"
1414
static let fileManagementDirectory = "parse/"
1515
static let fileManagementPrivateDocumentsDirectory = "Private Documents/"
1616
static let fileManagementLibraryDirectory = "Library/"

Sources/ParseSwift/Types/ParseConfig+combine.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Combine
1212

1313
public extension ParseConfig {
1414

15-
// MARK: Fetchable - Combine
15+
// MARK: Combine
1616

1717
/**
1818
Fetch the Config *asynchronously*. Publishes when complete.
@@ -28,8 +28,6 @@ public extension ParseConfig {
2828
}
2929
}
3030

31-
// MARK: Savable - Combine
32-
3331
/**
3432
Update the Config *asynchronously*.
3533
- parameter options: A set of header options sent to the server. Defaults to an empty set.

Sources/ParseSwift/Types/ParseFile.swift

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -266,15 +266,12 @@ extension ParseFile {
266266
- parameter callbackQueue: The queue to return to after synchronous completion.
267267
Default value of .main.
268268
- returns: A saved `ParseFile`.
269-
- note: The default cache policy for this method is `.reloadIgnoringLocalCacheData`. If a developer
270-
desires a different policy, it should be inserted in `options`.
271269
*/
272270
public func save(options: API.Options = [],
273271
stream: InputStream,
274272
callbackQueue: DispatchQueue = .main,
275273
progress: ((URLSessionTask, Int64, Int64, Int64) -> Void)? = nil) throws {
276274
var options = options
277-
options.insert(.cachePolicy(.reloadIgnoringLocalCacheData))
278275
if let mimeType = mimeType {
279276
options.insert(.mimeType(mimeType))
280277
} else {
@@ -300,13 +297,10 @@ extension ParseFile {
300297
- parameter options: A set of header options sent to the server. Defaults to an empty set.
301298
- parameter callbackQueue: The queue to return to after synchronous completion.
302299
- returns: A saved `ParseFile`.
303-
- note: The default cache policy for this method is `.reloadIgnoringLocalCacheData`. If a developer
304-
desires a different policy, it should be inserted in `options`.
305300
*/
306301
public func save(options: API.Options = [],
307302
callbackQueue: DispatchQueue) throws -> ParseFile {
308303
var options = options
309-
options.insert(.cachePolicy(.reloadIgnoringLocalCacheData))
310304
if let mimeType = mimeType {
311305
options.insert(.mimeType(mimeType))
312306
} else {
@@ -376,14 +370,11 @@ extension ParseFile {
376370
It should have the following argument signature: `(task: URLSessionDownloadTask,
377371
bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64)`.
378372
- returns: A saved `ParseFile`.
379-
- note: The default cache policy for this method is `.reloadIgnoringLocalCacheData`. If a developer
380-
desires a different policy, it should be inserted in `options`.
381373
*/
382374
public func save(options: API.Options = [],
383375
callbackQueue: DispatchQueue = .main,
384376
progress: ((URLSessionTask, Int64, Int64, Int64) -> Void)?) throws -> ParseFile {
385377
var options = options
386-
options.insert(.cachePolicy(.reloadIgnoringLocalCacheData))
387378
if let mimeType = mimeType {
388379
options.insert(.mimeType(mimeType))
389380
} else {
@@ -538,8 +529,6 @@ extension ParseFile {
538529
- parameter callbackQueue: The queue to return to after synchronous completion.
539530
Default value of .main.
540531
- returns: A saved `ParseFile`.
541-
- note: The default cache policy for this method is `.reloadIgnoringLocalCacheData`. If a developer
542-
desires a different policy, it should be inserted in `options`.
543532
*/
544533
public func fetch(options: API.Options = [],
545534
stream: InputStream,
@@ -556,7 +545,6 @@ extension ParseFile {
556545
if let tags = tags {
557546
options.insert(.tags(tags))
558547
}
559-
options.insert(.cachePolicy(.reloadIgnoringLocalCacheData))
560548
options = options.union(self.options)
561549
return try downloadFileCommand()
562550
.executeStream(options: options,
@@ -570,8 +558,6 @@ extension ParseFile {
570558
- parameter options: A set of header options sent to the server. Defaults to an empty set.
571559
- parameter callbackQueue: The queue to return to after synchronous completion.
572560
- returns: A saved `ParseFile`.
573-
- note: The default cache policy for this method is `.reloadIgnoringLocalCacheData`. If a developer
574-
desires a different policy, it should be inserted in `options`.
575561
*/
576562
public func fetch(includeKeys: [String]? = nil,
577563
options: API.Options = [],
@@ -588,7 +574,6 @@ extension ParseFile {
588574
if let tags = tags {
589575
options.insert(.tags(tags))
590576
}
591-
options.insert(.cachePolicy(.reloadIgnoringLocalCacheData))
592577
options = options.union(self.options)
593578
return try downloadFileCommand()
594579
.execute(options: options,
@@ -600,8 +585,6 @@ extension ParseFile {
600585
- parameter includeKeys: Currently not used for `ParseFile`.
601586
- parameter options: A set of header options sent to the server. Defaults to an empty set.
602587
- returns: A saved `ParseFile`.
603-
- note: The default cache policy for this method is `.reloadIgnoringLocalCacheData`. If a developer
604-
desires a different policy, it should be inserted in `options`.
605588
*/
606589
public func fetch(includeKeys: [String]? = nil,
607590
options: API.Options = []) throws -> ParseFile {
@@ -649,8 +632,6 @@ extension ParseFile {
649632
It should have the following argument signature: `(task: URLSessionDownloadTask,
650633
bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64)`.
651634
- returns: A saved `ParseFile`.
652-
- note: The default cache policy for this method is `.reloadIgnoringLocalCacheData`. If a developer
653-
desires a different policy, it should be inserted in `options`.
654635
*/
655636
public func fetch(options: API.Options = [],
656637
callbackQueue: DispatchQueue = .main,
@@ -667,7 +648,6 @@ extension ParseFile {
667648
if let tags = tags {
668649
options.insert(.tags(tags))
669650
}
670-
options.insert(.cachePolicy(.reloadIgnoringLocalCacheData))
671651
options = options.union(self.options)
672652
return try downloadFileCommand()
673653
.execute(options: options,
@@ -715,9 +695,7 @@ extension ParseFile {
715695
It should have the following argument signature: `(task: URLSessionDownloadTask,
716696
bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64)`.
717697
- parameter completion: A block that will be called when file fetches or fails.
718-
It should have the following argument signature: `(Result<Self, ParseError>)`
719-
- note: The default cache policy for this method is `.reloadIgnoringLocalCacheData`. If a developer
720-
desires a different policy, it should be inserted in `options`.
698+
It should have the following argument signature: `(Result<Self, ParseError>)`.
721699
*/
722700
public func fetch(options: API.Options = [],
723701
callbackQueue: DispatchQueue = .main,
@@ -735,7 +713,6 @@ extension ParseFile {
735713
if let tags = tags {
736714
options.insert(.tags(tags))
737715
}
738-
options.insert(.cachePolicy(.reloadIgnoringLocalCacheData))
739716
options = options.union(self.options)
740717
downloadFileCommand()
741718
.executeAsync(options: options,

Sources/ParseSwift/Types/Pointer.swift

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@ protocol ParsePointer: Encodable {
1212
extension ParsePointer {
1313
/**
1414
Determines if two objects have the same objectId.
15-
1615
- parameter as: Object to compare.
17-
1816
- returns: Returns a `true` if the other object has the same `objectId` or `false` if unsuccessful.
1917
*/
20-
public func hasSameObjectId(as other: ParsePointer) -> Bool {
18+
func hasSameObjectId(as other: ParsePointer) -> Bool {
2119
return other.className == className && other.objectId == objectId
2220
}
2321
}
@@ -82,6 +80,24 @@ public struct Pointer<T: ParseObject>: ParsePointer, Fetchable, Encodable, Hasha
8280

8381
public extension Pointer {
8482

83+
/**
84+
Determines if a `ParseObject` and `Pointer`have the same `objectId`.
85+
- parameter as: `ParseObject` to compare.
86+
- returns: Returns a `true` if the other object has the same `objectId` or `false` if unsuccessful.
87+
*/
88+
func hasSameObjectId(as other: T) -> Bool {
89+
return other.className == className && other.objectId == objectId
90+
}
91+
92+
/**
93+
Determines if two `Pointer`'s have the same `objectId`.
94+
- parameter as: `Pointer` to compare.
95+
- returns: Returns a `true` if the other object has the same `objectId` or `false` if unsuccessful.
96+
*/
97+
func hasSameObjectId(as other: Self) -> Bool {
98+
return other.className == className && other.objectId == objectId
99+
}
100+
85101
/**
86102
Fetches the `ParseObject` *synchronously* with the current data from the server and sets an error if one occurs.
87103
- parameter includeKeys: The name(s) of the key(s) to include that are

Tests/ParseSwiftTests/ParseOperationTests.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,15 @@ class ParseOperationTests: XCTestCase {
503503
let decoded = try XCTUnwrap(String(data: encoded, encoding: .utf8))
504504
XCTAssertEqual(decoded, expected)
505505
XCTAssertEqual(operations.target?.score, 15)
506+
var level = Level(level: 12)
507+
level.members = ["hello", "world"]
508+
let operations2 = try score.operation.set(("previous", \.previous), value: [level])
509+
let expected2 = "{\"previous\":[{\"level\":12,\"members\":[\"hello\",\"world\"]}]}"
510+
let encoded2 = try ParseCoding.parseEncoder()
511+
.encode(operations2)
512+
let decoded2 = try XCTUnwrap(String(data: encoded2, encoding: .utf8))
513+
XCTAssertEqual(decoded2, expected2)
514+
XCTAssertEqual(operations2.target?.previous, [level])
506515
}
507516
#endif
508517

@@ -516,6 +525,17 @@ class ParseOperationTests: XCTestCase {
516525
.encode(operations)
517526
let decoded = try XCTUnwrap(String(data: encoded, encoding: .utf8))
518527
XCTAssertEqual(decoded, expected)
528+
XCTAssertEqual(operations.target?.objectId, "test")
529+
var level = Level(level: 12)
530+
level.members = ["hello", "world"]
531+
score.previous = [level]
532+
let expected2 = "{}"
533+
let operations2 = try score.operation.set(("previous", \.previous), value: [level])
534+
let encoded2 = try ParseCoding.parseEncoder()
535+
.encode(operations2)
536+
let decoded2 = try XCTUnwrap(String(data: encoded2, encoding: .utf8))
537+
XCTAssertEqual(decoded2, expected2)
538+
XCTAssertEqual(operations2.target?.previous, [level])
519539
}
520540

521541
func testUnchangedSet() throws {

Tests/ParseSwiftTests/ParsePointerTests.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,33 @@ class ParsePointerTests: XCTestCase {
9292
XCTAssertEqual(pointer.objectId, score.objectId)
9393
}
9494

95+
func testHasSameObjectId() throws {
96+
var score = GameScore(score: 10)
97+
let objectId = "yarr"
98+
score.objectId = objectId
99+
let pointer = try score.toPointer()
100+
let pointer2 = pointer
101+
XCTAssertTrue(pointer.hasSameObjectId(as: pointer2))
102+
XCTAssertTrue(pointer.hasSameObjectId(as: score))
103+
score.objectId = "hello"
104+
let pointer3 = try score.toPointer()
105+
XCTAssertFalse(pointer.hasSameObjectId(as: pointer3))
106+
XCTAssertFalse(pointer.hasSameObjectId(as: score))
107+
}
108+
109+
func testPointerEquality() throws {
110+
var score = GameScore(score: 10)
111+
let objectId = "yarr"
112+
score.objectId = objectId
113+
let pointer = try score.toPointer()
114+
var score2 = GameScore(score: 10)
115+
score2.objectId = objectId
116+
var pointer2 = try score2.toPointer()
117+
XCTAssertEqual(pointer, pointer2)
118+
pointer2.objectId = "hello"
119+
XCTAssertNotEqual(pointer, pointer2)
120+
}
121+
95122
func testDetectCircularDependency() throws {
96123
var score = GameScore(score: 10)
97124
score.objectId = "nice"

0 commit comments

Comments
 (0)