Skip to content

Commit 5b0d1bb

Browse files
authored
Merge pull request #82 from tang3w/major/installation
Support installation
2 parents 4cb58b4 + 19d79ec commit 5b0d1bb

File tree

19 files changed

+418
-12
lines changed

19 files changed

+418
-12
lines changed

LeanCloud.podspec

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ Pod::Spec.new do |s|
1313
s.watchos.deployment_target = '3.0'
1414

1515
s.subspec 'Foundation' do |ss|
16-
ss.source_files = 'Sources/Foundation/**/*.swift'
16+
ss.source_files = 'Sources/Foundation/**/*.{h,m,swift}'
17+
ss.private_header_files = 'Sources/Foundation/Polyfill/Polyfill.h'
18+
ss.pod_target_xcconfig = { 'SWIFT_INCLUDE_PATHS' => '"$(PODS_TARGET_SRCROOT)"/**' }
19+
ss.preserve_paths = 'Sources/Foundation/**/*.{modulemap}'
1720
end
1821

1922
s.subspec 'LocalStorage' do |ss|
@@ -29,6 +32,6 @@ Pod::Spec.new do |s|
2932
ss.dependency 'LeanCloud/LocalStorage'
3033

3134
ss.source_files = 'Sources/Storage/**/*.swift'
32-
ss.resources = 'Sources/Storage/AppRouterCache.xcdatamodeld'
35+
ss.resources = 'Sources/Storage/**/*.{xcdatamodeld}'
3336
end
3437
end

LeanCloud.xcodeproj/project.pbxproj

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
116A682621427442004141A5 /* RouterTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 116A682521427442004141A5 /* RouterTestCase.swift */; };
1414
1178D10F213F7B9600429131 /* APITestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1178D10E213F7B9600429131 /* APITestCase.swift */; };
1515
1178D132214019F800429131 /* HTTPRouter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1178D131214019F800429131 /* HTTPRouter.swift */; };
16+
117A2D0C21704620007C509A /* Installation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 117A2D0B21704620007C509A /* Installation.swift */; };
17+
118763AB2176DF110078645B /* StorageContext.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 118763A92176DF110078645B /* StorageContext.xcdatamodeld */; };
18+
118763AD2176DF440078645B /* StorageContextCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 118763AC2176DF440078645B /* StorageContextCache.swift */; };
19+
118763AF2176EA3A0078645B /* InstallationTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 118763AE2176EA3A0078645B /* InstallationTestCase.swift */; };
1620
11A166CD214F57360094AF13 /* LocalStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11A166CC214F57360094AF13 /* LocalStorage.swift */; };
1721
11A166DE214F5E440094AF13 /* PersistentController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11A166DD214F5E440094AF13 /* PersistentController.swift */; };
1822
11A166E221509F020094AF13 /* AppRouterCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11A166E121509F020094AF13 /* AppRouterCache.swift */; };
@@ -23,6 +27,9 @@
2327
11D9C101216DFB2D00A620B7 /* MD5.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11D9C100216DFB2D00A620B7 /* MD5.swift */; };
2428
11FBD1E5215BDDCA00370C61 /* CoreTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11FBD1E4215BDDCA00370C61 /* CoreTestCase.swift */; };
2529
11FBD1E8215BEEB400370C61 /* THE RIEMANN HYPOTHESIS.zip in Resources */ = {isa = PBXBuildFile; fileRef = 11FBD1E7215BEEB400370C61 /* THE RIEMANN HYPOTHESIS.zip */; };
30+
11FD150E21742CCE008389D4 /* Polyfill.h in Headers */ = {isa = PBXBuildFile; fileRef = 11FD150D21742CCD008389D4 /* Polyfill.h */; };
31+
11FD151021742CD3008389D4 /* Polyfill.m in Sources */ = {isa = PBXBuildFile; fileRef = 11FD150F21742CD3008389D4 /* Polyfill.m */; };
32+
11FD151421743A09008389D4 /* Lazyload.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11FD151321743A09008389D4 /* Lazyload.swift */; };
2633
8334561C1CE589E500D42725 /* EngineTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8334561B1CE589E500D42725 /* EngineTestCase.swift */; };
2734
8342FCC61C7B13A700C3CF15 /* LeanCloud.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8342FCBB1C7B13A700C3CF15 /* LeanCloud.framework */; };
2835
8342FCCB1C7B13A700C3CF15 /* BaseTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8342FCCA1C7B13A700C3CF15 /* BaseTestCase.swift */; };
@@ -133,6 +140,10 @@
133140
116A682521427442004141A5 /* RouterTestCase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RouterTestCase.swift; sourceTree = "<group>"; };
134141
1178D10E213F7B9600429131 /* APITestCase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = APITestCase.swift; sourceTree = "<group>"; };
135142
1178D131214019F800429131 /* HTTPRouter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HTTPRouter.swift; sourceTree = "<group>"; };
143+
117A2D0B21704620007C509A /* Installation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Installation.swift; sourceTree = "<group>"; };
144+
118763AA2176DF110078645B /* StorageContext.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = StorageContext.xcdatamodel; sourceTree = "<group>"; };
145+
118763AC2176DF440078645B /* StorageContextCache.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StorageContextCache.swift; sourceTree = "<group>"; };
146+
118763AE2176EA3A0078645B /* InstallationTestCase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InstallationTestCase.swift; sourceTree = "<group>"; };
136147
11A166CC214F57360094AF13 /* LocalStorage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocalStorage.swift; sourceTree = "<group>"; };
137148
11A166DD214F5E440094AF13 /* PersistentController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PersistentController.swift; sourceTree = "<group>"; };
138149
11A166E121509F020094AF13 /* AppRouterCache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppRouterCache.swift; sourceTree = "<group>"; };
@@ -143,6 +154,9 @@
143154
11D9C100216DFB2D00A620B7 /* MD5.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MD5.swift; sourceTree = "<group>"; };
144155
11FBD1E4215BDDCA00370C61 /* CoreTestCase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoreTestCase.swift; sourceTree = "<group>"; };
145156
11FBD1E7215BEEB400370C61 /* THE RIEMANN HYPOTHESIS.zip */ = {isa = PBXFileReference; lastKnownFileType = archive.zip; path = "THE RIEMANN HYPOTHESIS.zip"; sourceTree = "<group>"; };
157+
11FD150D21742CCD008389D4 /* Polyfill.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Polyfill.h; sourceTree = "<group>"; };
158+
11FD150F21742CD3008389D4 /* Polyfill.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Polyfill.m; sourceTree = "<group>"; };
159+
11FD151321743A09008389D4 /* Lazyload.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Lazyload.swift; sourceTree = "<group>"; };
146160
8334561B1CE589E500D42725 /* EngineTestCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EngineTestCase.swift; sourceTree = "<group>"; };
147161
8342FCBB1C7B13A700C3CF15 /* LeanCloud.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = LeanCloud.framework; sourceTree = BUILT_PRODUCTS_DIR; };
148162
8342FCC01C7B13A700C3CF15 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@@ -223,8 +237,10 @@
223237
11D9C0F7216DF63B00A620B7 /* Foundation */ = {
224238
isa = PBXGroup;
225239
children = (
240+
11FD150021742CAF008389D4 /* Polyfill */,
226241
11D9C100216DFB2D00A620B7 /* MD5.swift */,
227242
11D9C0F8216DF63B00A620B7 /* Error.swift */,
243+
11FD151321743A09008389D4 /* Lazyload.swift */,
228244
11D9C0F9216DF63B00A620B7 /* Application.swift */,
229245
11D9C0FE216DF91A00A620B7 /* Logger.swift */,
230246
);
@@ -239,6 +255,15 @@
239255
path = Resources;
240256
sourceTree = "<group>";
241257
};
258+
11FD150021742CAF008389D4 /* Polyfill */ = {
259+
isa = PBXGroup;
260+
children = (
261+
11FD150D21742CCD008389D4 /* Polyfill.h */,
262+
11FD150F21742CD3008389D4 /* Polyfill.m */,
263+
);
264+
path = Polyfill;
265+
sourceTree = "<group>";
266+
};
242267
8342FCB11C7B13A600C3CF15 = {
243268
isa = PBXGroup;
244269
children = (
@@ -284,6 +309,7 @@
284309
8352B5221D2BAAE600531785 /* RelationTestCase.swift */,
285310
834EA7141D2A139A00108DDC /* UserTestCase.swift */,
286311
114E1E842153A3ED00DA7DB0 /* FileTestCase.swift */,
312+
118763AE2176EA3A0078645B /* InstallationTestCase.swift */,
287313
8342FCCC1C7B13A700C3CF15 /* Info.plist */,
288314
);
289315
path = LeanCloudTests;
@@ -313,6 +339,8 @@
313339
1178D131214019F800429131 /* HTTPRouter.swift */,
314340
11A166E121509F020094AF13 /* AppRouterCache.swift */,
315341
11A166E62150A39F0094AF13 /* AppRouterCache.xcdatamodeld */,
342+
118763AC2176DF440078645B /* StorageContextCache.swift */,
343+
118763A92176DF110078645B /* StorageContext.xcdatamodeld */,
316344
835F5D5C1D7E6808004D1A0E /* Operation.swift */,
317345
835F5D5A1D7E6808004D1A0E /* ObjectProfiler.swift */,
318346
835F5D5B1D7E6808004D1A0E /* ObjectUpdater.swift */,
@@ -347,6 +375,7 @@
347375
835F5D511D7E6808004D1A0E /* String.swift */,
348376
835F5D521D7E6808004D1A0E /* User.swift */,
349377
114E1E762151FFB600DA7DB0 /* File.swift */,
378+
117A2D0B21704620007C509A /* Installation.swift */,
350379
);
351380
path = DataType;
352381
sourceTree = "<group>";
@@ -373,6 +402,7 @@
373402
buildActionMask = 2147483647;
374403
files = (
375404
835F5D7A1D7E6808004D1A0E /* LeanCloud.h in Headers */,
405+
11FD150E21742CCE008389D4 /* Polyfill.h in Headers */,
376406
);
377407
runOnlyForDeploymentPostprocessing = 0;
378408
};
@@ -543,6 +573,7 @@
543573
11A166DE214F5E440094AF13 /* PersistentController.swift in Sources */,
544574
835F5D881D7E6808004D1A0E /* Value.swift in Sources */,
545575
835F5D6F1D7E6808004D1A0E /* GeoPoint.swift in Sources */,
576+
11FD151421743A09008389D4 /* Lazyload.swift in Sources */,
546577
835F5D841D7E6808004D1A0E /* Result.swift in Sources */,
547578
835F5D771D7E6808004D1A0E /* Engine.swift in Sources */,
548579
835F5D761D7E6808004D1A0E /* User.swift in Sources */,
@@ -554,18 +585,22 @@
554585
114E1E812152500200DA7DB0 /* FileUploader.swift in Sources */,
555586
835F5D711D7E6808004D1A0E /* Number.swift in Sources */,
556587
835F5D871D7E6808004D1A0E /* Utility.swift in Sources */,
588+
117A2D0C21704620007C509A /* Installation.swift in Sources */,
557589
835F5D7F1D7E6808004D1A0E /* Operation.swift in Sources */,
558590
11A166CD214F57360094AF13 /* LocalStorage.swift in Sources */,
559591
835F5D661D7E6808004D1A0E /* BatchRequest.swift in Sources */,
560592
835F5D851D7E6808004D1A0E /* Runtime.swift in Sources */,
561593
835F5D721D7E6808004D1A0E /* Object.swift in Sources */,
562594
1178D132214019F800429131 /* HTTPRouter.swift in Sources */,
595+
118763AB2176DF110078645B /* StorageContext.xcdatamodeld in Sources */,
563596
11D9C0FF216DF91A00A620B7 /* Logger.swift in Sources */,
564597
835F5D811D7E6808004D1A0E /* Request.swift in Sources */,
565598
835F5D681D7E6808004D1A0E /* CQLClient.swift in Sources */,
599+
118763AD2176DF440078645B /* StorageContextCache.swift in Sources */,
566600
11A166E82150A39F0094AF13 /* AppRouterCache.xcdatamodeld in Sources */,
567601
835F5D791D7E6808004D1A0E /* Extension.swift in Sources */,
568602
835F5D7D1D7E6808004D1A0E /* ObjectProfiler.swift in Sources */,
603+
11FD151021742CD3008389D4 /* Polyfill.m in Sources */,
569604
835F5D821D7E6808004D1A0E /* Response.swift in Sources */,
570605
835F5D861D7E6808004D1A0E /* SMSClient.swift in Sources */,
571606
835F5D7E1D7E6808004D1A0E /* ObjectUpdater.swift in Sources */,
@@ -595,6 +630,7 @@
595630
1178D10F213F7B9600429131 /* APITestCase.swift in Sources */,
596631
8334561C1CE589E500D42725 /* EngineTestCase.swift in Sources */,
597632
116A682621427442004141A5 /* RouterTestCase.swift in Sources */,
633+
118763AF2176EA3A0078645B /* InstallationTestCase.swift in Sources */,
598634
8365CFA81D79849D006B856E /* TypeTestCase.swift in Sources */,
599635
);
600636
runOnlyForDeploymentPostprocessing = 0;
@@ -736,6 +772,7 @@
736772
PRODUCT_BUNDLE_IDENTIFIER = cn.leancloud.LeanCloud;
737773
PRODUCT_NAME = "$(TARGET_NAME)";
738774
SKIP_INSTALL = YES;
775+
SWIFT_INCLUDE_PATHS = "\"$(SRCROOT)\"/**";
739776
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
740777
SWIFT_VERSION = 4.2;
741778
};
@@ -757,6 +794,7 @@
757794
PRODUCT_BUNDLE_IDENTIFIER = cn.leancloud.LeanCloud;
758795
PRODUCT_NAME = "$(TARGET_NAME)";
759796
SKIP_INSTALL = YES;
797+
SWIFT_INCLUDE_PATHS = "\"$(SRCROOT)\"/**";
760798
SWIFT_VERSION = 4.2;
761799
};
762800
name = Release;
@@ -822,6 +860,16 @@
822860
/* End XCConfigurationList section */
823861

824862
/* Begin XCVersionGroup section */
863+
118763A92176DF110078645B /* StorageContext.xcdatamodeld */ = {
864+
isa = XCVersionGroup;
865+
children = (
866+
118763AA2176DF110078645B /* StorageContext.xcdatamodel */,
867+
);
868+
currentVersion = 118763AA2176DF110078645B /* StorageContext.xcdatamodel */;
869+
path = StorageContext.xcdatamodeld;
870+
sourceTree = "<group>";
871+
versionGroupType = wrapper.xcdatamodel;
872+
};
825873
11A166E62150A39F0094AF13 /* AppRouterCache.xcdatamodeld */ = {
826874
isa = XCVersionGroup;
827875
children = (
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// InstallationTestCase.swift
3+
// LeanCloudTests
4+
//
5+
// Created by Tianyong Tang on 2018/10/17.
6+
// Copyright © 2018 LeanCloud. All rights reserved.
7+
//
8+
9+
import XCTest
10+
@testable import LeanCloud
11+
12+
class InstallationTestCase: BaseTestCase {
13+
14+
func testCurrentInstallation() {
15+
let installation = LCApplication.default.currentInstallation
16+
17+
installation.deviceToken = "01010101010101010101010101"
18+
19+
XCTAssertTrue(installation.save().isSuccess)
20+
21+
let cachedInstallation = LCApplication.default.storageContextCache.installation
22+
23+
XCTAssertFalse(installation === cachedInstallation)
24+
XCTAssertEqual(installation, cachedInstallation)
25+
}
26+
27+
}

Sources/Foundation/Application.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import Foundation
1515

1616
It is a context of application-specific settings and objects.
1717
*/
18-
public final class LCApplication {
18+
public final class LCApplication: NSObject {
1919

2020
/**
2121
Application region.
@@ -103,7 +103,7 @@ public final class LCApplication {
103103

104104
- note: We make initializer internal before multi-applicaiton is supported.
105105
*/
106-
init() {
106+
override init() {
107107
/* Nop */
108108
}
109109

Sources/Foundation/Lazyload.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// Lazyload.swift
3+
// LeanCloud
4+
//
5+
// Created by Tianyong Tang on 2018/10/15.
6+
// Copyright © 2018 LeanCloud. All rights reserved.
7+
//
8+
9+
import Foundation
10+
import Polyfill.Private
11+
12+
extension NSObject {
13+
14+
func lc_lazyload<T: Any>(_ key: String, _ policy: objc_AssociationPolicy, _ object: () -> T) -> T {
15+
return lc_lazyload(key, policy, object())
16+
}
17+
18+
func lc_lazyload<T: Any>(_ key: String, _ policy: objc_AssociationPolicy, _ object: @autoclosure () -> T) -> T {
19+
objc_sync_enter(self)
20+
21+
defer {
22+
objc_sync_exit(self)
23+
}
24+
25+
if let object = lc_associatedObject(forKey: key) as? T {
26+
return object
27+
} else {
28+
let object = object()
29+
lc_associateObject(object, forKey: key, policy: policy)
30+
return object
31+
}
32+
}
33+
34+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// Polyfill.h
3+
// LeanCloud
4+
//
5+
// Created by Tianyong Tang on 2018/10/15.
6+
// Copyright © 2018 LeanCloud. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
#import <objc/runtime.h>
11+
12+
NS_ASSUME_NONNULL_BEGIN
13+
14+
@interface NSObject (LeanCloud)
15+
16+
- (nullable id)lc_associatedObjectForKey:(NSString *)key;
17+
18+
- (void)lc_associateObject:(nullable id)object forKey:(NSString *)key policy:(objc_AssociationPolicy)policy;
19+
20+
@end
21+
22+
NS_ASSUME_NONNULL_END
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// Polyfill.m
3+
// LeanCloud
4+
//
5+
// Created by Tianyong Tang on 2018/10/15.
6+
// Copyright © 2018 LeanCloud. All rights reserved.
7+
//
8+
9+
#import "Polyfill.h"
10+
11+
@implementation NSObject (LeanCloud)
12+
13+
- (id)lc_associatedObjectForKey:(NSString *)key {
14+
SEL selector = sel_registerName(key.UTF8String);
15+
id object = objc_getAssociatedObject(self, selector);
16+
return object;
17+
}
18+
19+
- (void)lc_associateObject:(id)object forKey:(NSString *)key policy:(objc_AssociationPolicy)policy {
20+
SEL selector = sel_registerName(key.UTF8String);
21+
objc_setAssociatedObject(self, selector, object, policy);
22+
}
23+
24+
@end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module Polyfill {
2+
export *
3+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
explicit module Polyfill.Private {
2+
header "Polyfill.h"
3+
export *
4+
}

Sources/LocalStorage/LocalStorage.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,17 @@ class LocalStorage {
235235
}
236236
}
237237

238+
/**
239+
Create a singleton and perform task.
240+
*/
241+
@discardableResult
242+
func withSingleton<S: NSManagedObject, T>(body: (S, NSManagedObjectContext) throws -> T) throws -> T {
243+
return try perform { context in
244+
let singleton: S = try createSingleton()
245+
return try body(singleton, context)
246+
}
247+
}
248+
238249
/**
239250
Fetch only one object with optional predicate.
240251
*/

0 commit comments

Comments
 (0)