Skip to content

Commit bff7bd4

Browse files
authored
Merge pull request #210 from zapcannon87/master
add some feats for storage
2 parents ed4371c + 976db30 commit bff7bd4

File tree

8 files changed

+149
-43
lines changed

8 files changed

+149
-43
lines changed

LeanCloudTests/LCObjectTestCase.swift

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,27 @@ class LCObjectTestCase: BaseTestCase {
2929
XCTAssertTrue(object.save().isSuccess)
3030
XCTAssertNotNil(object.objectId)
3131
}
32+
33+
func testSaveObjectWithOption() {
34+
let object = TestObject(className: "\(TestObject.self)")
35+
object.numberField = 0
36+
37+
XCTAssertTrue(object.save(options: [.fetchWhenSave]).isSuccess)
38+
39+
if let objectId = object.objectId {
40+
object.numberField = 1
41+
42+
let noResultQuery = LCQuery(className: "\(TestObject.self)")
43+
noResultQuery.whereKey("objectId", .equalTo(UUID().uuidString))
44+
XCTAssertEqual(object.save(options: [.query(noResultQuery)]).error?.code, 305)
45+
46+
let hasResultQuery = LCQuery(className: "\(TestObject.self)")
47+
hasResultQuery.whereKey("objectId", .equalTo(objectId))
48+
XCTAssertTrue(object.save(options: [.query(hasResultQuery)]).isSuccess)
49+
} else {
50+
XCTFail("no objectId")
51+
}
52+
}
3253

3354
func testCircularReference() {
3455
let object1 = TestObject()
@@ -222,6 +243,29 @@ class LCObjectTestCase: BaseTestCase {
222243
XCTAssertEqual(LCError.ServerErrorCode(rawValue: LCObject.fetch([object, notFound]).error!._code), .objectNotFound)
223244
XCTAssertTrue(LCObject.fetch([object, child]).isSuccess)
224245
}
246+
247+
func testFetchWithKeys() {
248+
let object = TestObject(className: "\(TestObject.self)")
249+
object.booleanField = false
250+
object.stringField = "string"
251+
object.numberField = 1
252+
XCTAssertTrue(object.save().isSuccess)
253+
254+
if let objectId = object.objectId {
255+
let replica = TestObject(className: "\(TestObject.self)", objectId: objectId)
256+
replica.booleanField = true
257+
replica.stringField = "changed"
258+
replica.numberField = 2
259+
XCTAssertTrue(replica.save().isSuccess)
260+
261+
XCTAssertTrue(object.fetch(keys: ["booleanField", "numberField"]).isSuccess)
262+
XCTAssertEqual(object.booleanField, replica.booleanField)
263+
XCTAssertEqual(object.numberField, replica.numberField)
264+
XCTAssertNotEqual(object.stringField, replica.stringField)
265+
} else {
266+
XCTFail("no objectId")
267+
}
268+
}
225269

226270
func testDelete() {
227271
let object = TestObject()

LeanCloudTests/LCQueryTestCase.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,5 +559,17 @@ class LCQueryTestCase: BaseTestCase {
559559
let objects = query.find().objects ?? []
560560
XCTAssertTrue(objects.isEmpty)
561561
}
562+
563+
func testIncludeACL() {
564+
let _ = sharedObject
565+
let query = objectQuery()
566+
567+
query.limit = 1
568+
query.includeACL = true
569+
570+
let objects = query.find().objects ?? []
571+
XCTAssertNotNil(objects.first)
572+
XCTAssertNotNil(objects.first?.ACL)
573+
}
562574

563575
}

Sources/Storage/BatchRequest.swift

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,18 @@ class BatchRequest {
1212
let object: LCObject
1313
let method: HTTPClient.Method?
1414
let operationTable: OperationTable?
15-
16-
init(object: LCObject, method: HTTPClient.Method? = nil, operationTable: OperationTable? = nil) {
15+
let parameters: [String: Any]?
16+
17+
init(
18+
object: LCObject,
19+
method: HTTPClient.Method? = nil,
20+
operationTable: OperationTable? = nil,
21+
parameters: [String: Any]? = nil)
22+
{
1723
self.object = object
1824
self.method = method
1925
self.operationTable = operationTable
26+
self.parameters = parameters
2027
}
2128

2229
var isNewborn: Bool {
@@ -81,6 +88,9 @@ class BatchRequest {
8188
"path": path,
8289
"method": method.rawValue
8390
]
91+
if let params: [String: Any] = self.parameters {
92+
request["params"] = params
93+
}
8494

8595
switch method {
8696
case .get:
@@ -107,9 +117,9 @@ class BatchRequestBuilder {
107117

108118
- returns: A list of request.
109119
*/
110-
static func buildRequests(_ object: LCObject) throws -> [BatchRequest] {
120+
static func buildRequests(_ object: LCObject, parameters: [String: Any]?) throws -> [BatchRequest] {
111121
return try operationTableList(object).map { element in
112-
BatchRequest(object: object, operationTable: element)
122+
BatchRequest(object: object, operationTable: element, parameters: parameters)
113123
}
114124
}
115125

Sources/Storage/DataType/File.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,18 @@ public class LCFile: LCObject {
106106
public final override class func objectClassName() -> String {
107107
return "_File"
108108
}
109+
110+
@available(*, unavailable)
111+
public override func save(options: [LCObject.SaveOption] = []) -> LCBooleanResult {
112+
fatalError("not support")
113+
}
114+
115+
@available(*, unavailable)
116+
public override func save(options: [LCObject.SaveOption] = [], completion: @escaping (LCBooleanResult) -> Void) -> LCRequest {
117+
fatalError("not support")
118+
}
109119

110-
public override func save() -> LCBooleanResult {
120+
public func save() -> LCBooleanResult {
111121
return expect { fulfill in
112122
self.save(
113123
progressInBackground: { _ in /* Nop */ },
@@ -117,9 +127,7 @@ public class LCFile: LCObject {
117127
}
118128
}
119129

120-
public override func save(
121-
_ completion: @escaping (LCBooleanResult) -> Void) -> LCRequest
122-
{
130+
public func save(_ completion: @escaping (LCBooleanResult) -> Void) -> LCRequest {
123131
return save(
124132
progress: { _ in /* Nop */ },
125133
completion: completion)

Sources/Storage/DataType/Object.swift

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

Sources/Storage/DataType/User.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ open class LCUser: LCObject {
8888

8989
@discardableResult
9090
private func signUp(completionInBackground completion: @escaping (LCBooleanResult) -> Void) -> LCRequest {
91-
return type(of: self).save([self], completionInBackground: completion)
91+
return type(of: self).save([self], options: [], completionInBackground: completion)
9292
}
9393

9494
// MARK: Log in with username and password

0 commit comments

Comments
 (0)