@@ -694,6 +694,11 @@ open class LCObject: NSObject, LCValue, LCValueExtension, Sequence {
694694 }
695695
696696 // MARK: Save object
697+
698+ public enum SaveOption {
699+ case fetchWhenSave
700+ case query( LCQuery )
701+ }
697702
698703 /**
699704 Save a batch of objects in one request synchronously.
@@ -702,10 +707,10 @@ open class LCObject: NSObject, LCValue, LCValueExtension, Sequence {
702707
703708 - returns: The result of deletion request.
704709 */
705- public static func save( _ objects: [ LCObject ] ) -> LCBooleanResult {
710+ public static func save( _ objects: [ LCObject ] , options : [ SaveOption ] = [ ] ) -> LCBooleanResult {
706711 assert ( self . assertObjectsApplication ( objects) , " objects with multiple applications. " )
707712 return expect { fulfill in
708- save ( objects, completionInBackground: { result in
713+ save ( objects, options : options , completionInBackground: { result in
709714 fulfill ( result)
710715 } )
711716 }
@@ -719,28 +724,40 @@ open class LCObject: NSObject, LCValue, LCValueExtension, Sequence {
719724
720725 - returns: The request of saving.
721726 */
722- public static func save( _ objects: [ LCObject ] , completion: @escaping ( LCBooleanResult ) -> Void ) -> LCRequest {
727+ @discardableResult
728+ public static func save( _ objects: [ LCObject ] , options: [ SaveOption ] = [ ] , completion: @escaping ( LCBooleanResult ) -> Void ) -> LCRequest {
723729 assert ( self . assertObjectsApplication ( objects) , " objects with multiple applications. " )
724- return save ( objects, completionInBackground: { result in
730+ return save ( objects, options : options , completionInBackground: { result in
725731 mainQueueAsync {
726732 completion ( result)
727733 }
728734 } )
729735 }
730736
731737 @discardableResult
732- static func save( _ objects: [ LCObject ] , completionInBackground completion: @escaping ( LCBooleanResult ) -> Void ) -> LCRequest {
738+ static func save( _ objects: [ LCObject ] , options : [ SaveOption ] , completionInBackground completion: @escaping ( LCBooleanResult ) -> Void ) -> LCRequest {
733739 assert ( self . assertObjectsApplication ( objects) , " objects with multiple applications. " )
734- return ObjectUpdater . save ( objects, completionInBackground: completion)
740+ var parameters : [ String : Any ] = [ : ]
741+ for option in options {
742+ switch option {
743+ case . fetchWhenSave:
744+ parameters [ " fetchWhenSave " ] = true
745+ case . query( let query) :
746+ if let whereDictionary: Any = query. lconValue [ " where " ] {
747+ parameters [ " where " ] = whereDictionary
748+ }
749+ }
750+ }
751+ return ObjectUpdater . save ( objects, parameters: ( parameters. isEmpty ? nil : parameters) , completionInBackground: completion)
735752 }
736753
737754 /**
738755 Save object and its all descendant objects synchronously.
739756
740757 - returns: The result of saving request.
741758 */
742- public func save( ) -> LCBooleanResult {
743- return type ( of: self ) . save ( [ self ] )
759+ public func save( options : [ SaveOption ] = [ ] ) -> LCBooleanResult {
760+ return type ( of: self ) . save ( [ self ] , options : options )
744761 }
745762
746763 /**
@@ -750,8 +767,9 @@ open class LCObject: NSObject, LCValue, LCValueExtension, Sequence {
750767
751768 - returns: The request of saving.
752769 */
753- public func save( _ completion: @escaping ( LCBooleanResult ) -> Void ) -> LCRequest {
754- return type ( of: self ) . save ( [ self ] , completion: completion)
770+ @discardableResult
771+ public func save( options: [ SaveOption ] = [ ] , completion: @escaping ( LCBooleanResult ) -> Void ) -> LCRequest {
772+ return type ( of: self ) . save ( [ self ] , options: options, completion: completion)
755773 }
756774
757775 // MARK: Delete object
@@ -778,6 +796,7 @@ open class LCObject: NSObject, LCValue, LCValueExtension, Sequence {
778796
779797 - returns: The request of deletion.
780798 */
799+ @discardableResult
781800 public static func delete( _ objects: [ LCObject ] , completion: @escaping ( LCBooleanResult ) -> Void ) -> LCRequest {
782801 assert ( self . assertObjectsApplication ( objects) , " objects with multiple applications. " )
783802 return delete ( objects, completionInBackground: { result in mainQueueAsync { completion ( result) } } )
@@ -805,6 +824,7 @@ open class LCObject: NSObject, LCValue, LCValueExtension, Sequence {
805824
806825 - returns: The request of deletion.
807826 */
827+ @discardableResult
808828 public func delete( _ completion: @escaping ( LCBooleanResult ) -> Void ) -> LCRequest {
809829 return type ( of: self ) . delete ( [ self ] , completion: completion)
810830 }
@@ -818,10 +838,10 @@ open class LCObject: NSObject, LCValue, LCValueExtension, Sequence {
818838
819839 - returns: The result of fetching request.
820840 */
821- public static func fetch( _ objects: [ LCObject ] ) -> LCBooleanResult {
841+ public static func fetch( _ objects: [ LCObject ] , keys : [ String ] ? = nil ) -> LCBooleanResult {
822842 assert ( self . assertObjectsApplication ( objects) , " objects with multiple applications. " )
823843 return expect { fulfill in
824- fetch ( objects, completionInBackground: { result in
844+ fetch ( objects, keys : keys , completionInBackground: { result in
825845 fulfill ( result)
826846 } )
827847 }
@@ -835,36 +855,38 @@ open class LCObject: NSObject, LCValue, LCValueExtension, Sequence {
835855
836856 - returns: The request of fetching.
837857 */
838- public static func fetch( _ objects: [ LCObject ] , completion: @escaping ( LCBooleanResult ) -> Void ) -> LCRequest {
858+ @discardableResult
859+ public static func fetch( _ objects: [ LCObject ] , keys: [ String ] ? = nil , completion: @escaping ( LCBooleanResult ) -> Void ) -> LCRequest {
839860 assert ( self . assertObjectsApplication ( objects) , " objects with multiple applications. " )
840- return fetch ( objects, completionInBackground: { result in
861+ return fetch ( objects, keys : keys , completionInBackground: { result in
841862 mainQueueAsync {
842863 completion ( result)
843864 }
844865 } )
845866 }
846867
847868 @discardableResult
848- private static func fetch( _ objects: [ LCObject ] , completionInBackground completion: @escaping ( LCBooleanResult ) -> Void ) -> LCRequest {
869+ private static func fetch( _ objects: [ LCObject ] , keys : [ String ] ? , completionInBackground completion: @escaping ( LCBooleanResult ) -> Void ) -> LCRequest {
849870 assert ( self . assertObjectsApplication ( objects) , " objects with multiple applications. " )
850- return ObjectUpdater . fetch ( objects, completionInBackground: completion )
871+ return ObjectUpdater . fetch ( objects, keys : keys , completionInBackground: completion)
851872 }
852873
853874 /**
854875 Fetch object from server synchronously.
855876
856877 - returns: The result of fetching request.
857878 */
858- public func fetch( ) -> LCBooleanResult {
859- return type ( of: self ) . fetch ( [ self ] )
879+ public func fetch( keys : [ String ] ? = nil ) -> LCBooleanResult {
880+ return type ( of: self ) . fetch ( [ self ] , keys : keys )
860881 }
861882
862883 /**
863884 Fetch object from server asynchronously.
864885
865886 - parameter completion: The completion callback closure.
866887 */
867- public func fetch( _ completion: @escaping ( LCBooleanResult ) -> Void ) -> LCRequest {
868- return type ( of: self ) . fetch ( [ self ] , completion: completion)
888+ @discardableResult
889+ public func fetch( keys: [ String ] ? = nil , completion: @escaping ( LCBooleanResult ) -> Void ) -> LCRequest {
890+ return type ( of: self ) . fetch ( [ self ] , keys: keys, completion: completion)
869891 }
870892}
0 commit comments