Skip to content

Commit 04bc020

Browse files
committed
minor improvement to child saving
1 parent cf72311 commit 04bc020

File tree

3 files changed

+78
-20
lines changed

3 files changed

+78
-20
lines changed

Sources/ParseSwift/API/API+Commands.swift

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,49 @@ internal extension API.Command {
341341
mapper: mapper)
342342
}
343343

344+
// MARK: Saving ParseObjects - Encodable
345+
static func saveCommand(object: T) throws -> API.Command<T, PointerType> where T: Encodable {
346+
guard let objectable = object as? Objectable else {
347+
throw ParseError(code: .unknownError, message: "Not able to cast to objectable. Not saving")
348+
}
349+
if objectable.isSaved {
350+
return try updateCommand(object: object)
351+
} else {
352+
return try createCommand(object: object)
353+
}
354+
}
355+
356+
// MARK: Saving ParseObjects - [Encodable] - private
357+
private static func createCommand(object: T) throws -> API.Command<T, PointerType> where T: Encodable {
358+
guard var objectable = object as? Objectable else {
359+
throw ParseError(code: .unknownError, message: "Not able to cast to objectable. Not saving")
360+
}
361+
let mapper = { (data: Data) -> PointerType in
362+
let baseObjectable = try ParseCoding.jsonDecoder().decode(BaseObjectable.self, from: data)
363+
objectable.objectId = baseObjectable.objectId
364+
return try objectable.toPointer()
365+
}
366+
return API.Command<T, PointerType>(method: .POST,
367+
path: objectable.endpoint,
368+
body: object,
369+
mapper: mapper)
370+
}
371+
372+
private static func updateCommand(object: T) throws -> API.Command<T, PointerType> where T: Encodable {
373+
guard var objectable = object as? Objectable else {
374+
throw ParseError(code: .unknownError, message: "Not able to cast to objectable. Not saving")
375+
}
376+
let mapper = { (data: Data) -> PointerType in
377+
let baseObjectable = try ParseCoding.jsonDecoder().decode(BaseObjectable.self, from: data)
378+
objectable.objectId = baseObjectable.objectId
379+
return try objectable.toPointer()
380+
}
381+
return API.Command<T, PointerType>(method: .PUT,
382+
path: objectable.endpoint,
383+
body: object,
384+
mapper: mapper)
385+
}
386+
344387
// MARK: Saving ParseObjects - Encodable
345388
static func saveCommand<T>(_ object: T) throws -> API.Command<T, PointerType> where T: Encodable {
346389
guard let objectable = object as? Objectable else {
@@ -501,7 +544,7 @@ extension API.Command where T: ParseObject {
501544

502545
//This has been disabled, looking into getting it working in the future.
503546
//It's only needed for sending batches of childObjects which currently isn't being used.
504-
/*
547+
505548
// MARK: Batch - Child Objects
506549
extension API.Command where T: ParseType {
507550

@@ -510,7 +553,8 @@ extension API.Command where T: ParseType {
510553
return try? ParseCoding.jsonEncoder().encode(body)
511554
}
512555

513-
static func batch(commands: [API.Command<T, PointerType>]) -> RESTBatchCommandTypeEncodable<T> {
556+
static func batch(commands: [API.Command<T, PointerType>],
557+
transaction: Bool) -> RESTBatchCommandTypeEncodable<T> {
514558
let commands = commands.compactMap { (command) -> API.Command<T, PointerType>? in
515559
let path = ParseConfiguration.mountPath + command.path.urlComponent
516560
guard let body = command.body else {
@@ -551,10 +595,10 @@ extension API.Command where T: ParseType {
551595
return [(.failure(parseError))]
552596
}
553597
}
554-
let batchCommand = BatchCommand(requests: commands)
598+
let batchCommand = BatchCommand(requests: commands, transaction: transaction)
555599
return RESTBatchCommandTypeEncodable<T>(method: .POST, path: .batch, body: batchCommand, mapper: mapper)
556600
}
557-
}*/
601+
}
558602

559603
// MARK: API.NonParseBodyCommand
560604
internal extension API {

Sources/ParseSwift/API/BatchUtils.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ typealias RESTBatchCommandType<T> = API.Command<ParseObjectBatchCommand<T>, Pars
1616
typealias ParseObjectBatchCommandNoBody<T> = BatchCommandNoBody<NoBody, NoBody>
1717
typealias ParseObjectBatchResponseNoBody<NoBody> = [(Result<Void, ParseError>)]
1818
typealias RESTBatchCommandNoBodyType<T> = API.NonParseBodyCommand<ParseObjectBatchCommandNoBody<T>, ParseObjectBatchResponseNoBody<T>> where T: Encodable
19-
/*
19+
2020
typealias ParseObjectBatchCommandEncodable<T> = BatchCommand<T, PointerType> where T: ParseType
2121
typealias ParseObjectBatchResponseEncodable<U> = [(Result<PointerType, ParseError>)]
2222
// swiftlint:disable line_length
2323
typealias RESTBatchCommandTypeEncodable<T> = API.Command<ParseObjectBatchCommandEncodable<T>, ParseObjectBatchResponseEncodable<PointerType>> where T: ParseType
2424
// swiftlint:enable line_length
25-
*/
25+
2626
internal struct BatchCommand<T, U>: ParseType where T: ParseType {
2727
let requests: [API.Command<T, U>]
2828
var transaction: Bool

Sources/ParseSwift/Objects/ParseObject.swift

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -477,11 +477,12 @@ public extension Sequence where Element: ParseObject {
477477
- returns: Returns a Result enum with the object if a save was successful or a `ParseError` if it failed.
478478
- throws: `ParseError`
479479
*/
480-
func saveAll(options: API.Options = []) throws -> [(Result<PointerType, ParseError>)] {
480+
func saveAll(transaction: Bool = true,
481+
options: API.Options = []) throws -> [(Result<PointerType, ParseError>)] {
481482
let commands = try map { try $0.saveCommand() }
482483
return try API.Command<Self.Element, PointerType>
483-
.batch(commands: commands)
484-
.execute(options: options)
484+
.batch(commands: commands, transaction: transaction)
485+
.execute(options: options, callbackQueue: .main)
485486
}
486487
}*/
487488

@@ -669,7 +670,7 @@ extension ParseObject {
669670
var waitingToBeSaved = object.unsavedChildren
670671

671672
while waitingToBeSaved.count > 0 {
672-
var savableObjects = [Encodable]()
673+
var savableObjects = [ParseType]()
673674
var savableFiles = [ParseFile]()
674675
var nextBatch = [ParseType]()
675676
try waitingToBeSaved.forEach { parseType in
@@ -706,14 +707,21 @@ extension ParseObject {
706707
}
707708

708709
//Currently, batch isn't working for Encodable
709-
/*if let parseTypes = savableObjects as? [ParseType] {
710-
let savedChildObjects = try self.saveAll(options: options, objects: parseTypes)
710+
/*let savedChildObjects = try Self.saveAll(objects: savableObjects,
711+
options: options)
712+
let savedChildPointers = try savedChildObjects.compactMap { try $0.get() }
713+
if savedChildPointers.count != savableObjects.count {
714+
throw ParseError(code: .unknownError, message: "Couldn't save all child objects")
715+
}
716+
for (index, object) in savableObjects.enumerated() {
717+
let hash = try BaseObjectable.createHash(object)
718+
objectsFinishedSaving[hash] = savedChildPointers[index]
711719
}*/
720+
721+
//Saving children individually
712722
try savableObjects.forEach {
713723
let hash = try BaseObjectable.createHash($0)
714-
if let parseType = $0 as? ParseType {
715-
objectsFinishedSaving[hash] = try parseType.save(options: options)
716-
}
724+
objectsFinishedSaving[hash] = try $0.save(options: options)
717725
}
718726

719727
try savableFiles.forEach {
@@ -747,11 +755,17 @@ internal extension ParseType {
747755
try API.Command<Self, PointerType>.saveCommand(self)
748756
}
749757
/*
750-
func saveAll<T: ParseType>(options: API.Options = [], objects: [T]) throws -> [(Result<PointerType, ParseError>)] {
751-
let commands = try objects.map { try API.Command<T, PointerType>.saveCommand($0) }
752-
return try API.Command<T, PointerType>
753-
.batch(commands: commands)
754-
.execute(options: options)
758+
static func saveAll(objects: [ParseType],
759+
transaction: Bool = true,
760+
options: API.Options = []) throws -> [(Result<PointerType, ParseError>)] {
761+
let commands = try objects.map {
762+
try API.Command<Self, PointerType>.saveCommand(object: $0)
763+
}
764+
return try API.Command<Self, PointerType>
765+
.batch(commands: commands,
766+
transaction: transaction)
767+
.execute(options: options,
768+
callbackQueue: .main)
755769
}*/
756770
}
757771

0 commit comments

Comments
 (0)