Skip to content

Commit fd761cf

Browse files
committed
Turn emptyObject into computed property
1 parent 601e56c commit fd761cf

6 files changed

Lines changed: 25 additions & 26 deletions

File tree

ParseSwift.playground/Pages/1 - Your first Object.xcplaygroundpage/Contents.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ score.save { result in
100100
allows you to only send the updated keys to the
101101
parse server as opposed to the whole object.
102102
*/
103-
var changedScore = savedScore.emptyObject()
103+
var changedScore = savedScore.emptyObject
104104
changedScore.score = 200
105105
changedScore.save { result in
106106
switch result {
@@ -192,7 +192,7 @@ assert(savedScore?.score == 10)
192192
allows you to only send the updated keys to the
193193
parse server as opposed to the whole object.
194194
*/
195-
guard var changedScore = savedScore?.emptyObject() else {
195+
guard var changedScore = savedScore?.emptyObject else {
196196
fatalError()
197197
}
198198
changedScore.score = 200

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ score.save { result in
7272
allows you to only send the updated keys to the
7373
parse server as opposed to the whole object.
7474
*/
75-
var changedScore = savedScore.emptyObject()
75+
var changedScore = savedScore.emptyObject
7676
changedScore.score = 200
7777
changedScore.save { result in
7878
switch result {

ParseSwift.playground/Pages/4 - User - Continued.xcplaygroundpage/Contents.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ User.login(username: "hello", password: "world") { result in
106106
Using `emptyObject` allows you to only send the updated keys to the
107107
parse server as opposed to the whole object.
108108
*/
109-
var currentUser = User.current?.emptyObject()
109+
var currentUser = User.current?.emptyObject
110110
currentUser?.customKey = "myCustom"
111111
currentUser?.score = GameScore(score: 12)
112112
currentUser?.targetScore = GameScore(score: 100)
@@ -210,7 +210,7 @@ User.anonymous.login { result in
210210
}
211211

212212
//: Convert the anonymous user to a real new user.
213-
var currentUser2 = User.current?.emptyObject()
213+
var currentUser2 = User.current?.emptyObject
214214
currentUser2?.username = "bye"
215215
currentUser2?.password = "world"
216216
currentUser2?.signup { result in

ParseSwift.playground/Pages/6 - Installation.xcplaygroundpage/Contents.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ currentInstallation?.save { results in
6161
send the updated keys to the parse server as opposed to the
6262
whole object.
6363
*/
64-
currentInstallation = currentInstallation?.emptyObject()
64+
currentInstallation = currentInstallation?.emptyObject
6565
currentInstallation?.customKey = "updatedValue"
6666
currentInstallation?.save { results in
6767

Sources/ParseSwift/Objects/ParseObject.swift

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,38 +40,37 @@ public protocol ParseObject: Objectable,
4040
}
4141

4242
// MARK: Default Implementations
43-
extension ParseObject {
43+
public extension ParseObject {
44+
45+
/**
46+
Gets an empty version of the respective object. This can be used when you only need to update a
47+
a subset of the fields of an object as oppose to updating every field of an object.
48+
- note: Using an empty object and updating a subset of the fields reduces the amount of data sent between
49+
client and server when using `save` and `saveAll` to update objects.
50+
*/
51+
var emptyObject: Self {
52+
var object = Self()
53+
object.objectId = objectId
54+
object.createdAt = createdAt
55+
return object
56+
}
4457

4558
/**
4659
Determines if two objects have the same objectId.
4760
- parameter as: Object to compare.
4861
- returns: Returns a `true` if the other object has the same `objectId` or `false` if unsuccessful.
4962
*/
50-
public func hasSameObjectId<T: ParseObject>(as other: T) -> Bool {
63+
func hasSameObjectId<T: ParseObject>(as other: T) -> Bool {
5164
return other.className == className && other.objectId == objectId && objectId != nil
5265
}
5366

5467
/**
5568
Gets a Pointer referencing this object.
5669
- returns: Pointer<Self>
5770
*/
58-
public func toPointer() throws -> Pointer<Self> {
71+
func toPointer() throws -> Pointer<Self> {
5972
return try Pointer(self)
6073
}
61-
62-
/**
63-
Gets an empty version of the respective object. This can be used when you only need to update a
64-
a subset of the fields of an object as oppose to updating every field of an object.
65-
- note: Using an empty object and updating a subset of the fields reduces the amount of data sent between
66-
client and server when using `save` and `saveAll` to update objects.
67-
- returns: Self
68-
*/
69-
public func emptyObject() -> Self {
70-
var object = Self()
71-
object.objectId = objectId
72-
object.createdAt = createdAt
73-
return object
74-
}
7574
}
7675

7776
// MARK: Batch Support

Tests/ParseSwiftTests/ParseObjectTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ class ParseObjectTests: XCTestCase { // swiftlint:disable:this type_body_length
244244
var score = GameScore(score: 19, name: "fire")
245245
score.objectId = "yolo"
246246
score.createdAt = Date()
247-
let empty = score.emptyObject()
247+
let empty = score.emptyObject
248248
XCTAssertTrue(score.hasSameObjectId(as: empty))
249249
XCTAssertEqual(score.createdAt, empty.createdAt)
250250
}
@@ -578,7 +578,7 @@ class ParseObjectTests: XCTestCase { // swiftlint:disable:this type_body_length
578578
score.createdAt = Date()
579579
score.updatedAt = score.createdAt
580580

581-
let command = try score.emptyObject().saveCommand()
581+
let command = try score.emptyObject.saveCommand()
582582
XCTAssertNotNil(command)
583583
XCTAssertEqual(command.path.urlComponent, "/classes/\(className)/\(objectId)")
584584
XCTAssertEqual(command.method, API.Method.PUT)
@@ -597,7 +597,7 @@ class ParseObjectTests: XCTestCase { // swiftlint:disable:this type_body_length
597597
let decoded = try XCTUnwrap(String(data: encoded, encoding: .utf8))
598598
XCTAssertEqual(decoded, expected)
599599

600-
var empty = score.emptyObject()
600+
var empty = score.emptyObject
601601
empty.player = "Jennifer"
602602
let command2 = try empty.saveCommand()
603603
guard let body2 = command2.body else {

0 commit comments

Comments
 (0)