Skip to content

Commit b3a4736

Browse files
authored
Merge pull request #279 from zapcannon87/developer
refactor(IM): make APIs of local storage unavailable when can not import GRDB
2 parents be38455 + 29dcdad commit b3a4736

File tree

5 files changed

+71
-59
lines changed

5 files changed

+71
-59
lines changed

LeanCloudTests/IMClientTestCase.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ class IMClientTestCase: RTMBaseTestCase {
436436
wait(for: [queryExp2], timeout: timeout)
437437
}
438438

439+
#if canImport(GRDB)
439440
func testPrepareLocalStorage() {
440441
expecting { (exp) in
441442
let notUseLocalStorageClient = try! IMClient(ID: uuid, options: [])
@@ -582,7 +583,7 @@ class IMClientTestCase: RTMBaseTestCase {
582583

583584
XCTAssertEqual(client.convCollection.count, 2)
584585
}
585-
586+
#endif
586587
}
587588

588589
extension IMClientTestCase {

LeanCloudTests/IMLocalStorageTestCase.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import XCTest
1010
@testable import LeanCloud
1111

1212
class IMLocalStorageTestCase: RTMBaseTestCase {
13-
13+
#if canImport(GRDB)
1414
func testInitAndDeinit() {
1515
var client: IMClient! = clientUsingLocalStorage()
1616
XCTAssertNotNil(client.localStorage)
@@ -835,8 +835,10 @@ class IMLocalStorageTestCase: RTMBaseTestCase {
835835
exp.fulfill()
836836
}
837837
}
838+
#endif
838839
}
839840

841+
#if canImport(GRDB)
840842
extension IMLocalStorageTestCase {
841843

842844
func clientUsingLocalStorage() -> IMClient {
@@ -853,5 +855,5 @@ extension IMLocalStorageTestCase {
853855
}
854856
}
855857
}
856-
857858
}
859+
#endif

Sources/RTM/IMClient.swift

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ public class IMClient {
5555

5656
let connectionDelegator: RTMConnection.Delegator
5757

58+
#if canImport(GRDB)
5859
private(set) var localStorage: IMLocalStorage?
60+
#endif
5961

6062
/// The client delegate.
6163
public weak var delegate: IMClientDelegate?
@@ -106,16 +108,22 @@ public class IMClient {
106108
}
107109

108110
/// Default option is `receiveUnreadMessageCountAfterSessionDidOpen`.
109-
public static let `default`: Options = [
110-
.receiveUnreadMessageCountAfterSessionDidOpen,
111-
.usingLocalStorage
112-
]
111+
public static let `default`: Options = {
112+
#if canImport(GRDB)
113+
return [.receiveUnreadMessageCountAfterSessionDidOpen,
114+
.usingLocalStorage]
115+
#else
116+
return [.receiveUnreadMessageCountAfterSessionDidOpen]
117+
#endif
118+
}()
113119

114120
/// Receive unread message count after session did open.
115121
public static let receiveUnreadMessageCountAfterSessionDidOpen = Options(rawValue: 1 << 0)
116122

123+
#if canImport(GRDB)
117124
/// Use local storage.
118125
public static let usingLocalStorage = Options(rawValue: 1 << 1)
126+
#endif
119127

120128
var lcimProtocol: RTMConnection.LCIMProtocol {
121129
if contains(.receiveUnreadMessageCountAfterSessionDidOpen) {
@@ -202,6 +210,7 @@ public class IMClient {
202210
self.underlyingLocalRecord = localRecord
203211
}
204212

213+
#if canImport(GRDB)
205214
if options.contains(.usingLocalStorage) {
206215
let databaseURL = try localStorageContext.fileURL(
207216
place: .persistentData,
@@ -215,6 +224,7 @@ public class IMClient {
215224
initialize success.
216225
""")
217226
}
227+
#endif
218228
}
219229

220230
// directly init `connection` is better, lazy init is not a good choice.
@@ -936,6 +946,7 @@ extension IMClient {
936946

937947
// MARK: Local Storage
938948

949+
#if canImport(GRDB)
939950
extension IMClient {
940951

941952
/// Open database of the local storage.
@@ -1059,6 +1070,7 @@ extension IMClient {
10591070
}
10601071

10611072
}
1073+
#endif
10621074

10631075
// MARK: Internal
10641076

@@ -1300,7 +1312,9 @@ extension IMClient {
13001312
if let invalidLocalConvCache = droppable["invalidLocalConvCache"] as? Bool, invalidLocalConvCache {
13011313
for conv in convsSnapshot {
13021314
conv.isOutdated = true
1315+
#if canImport(GRDB)
13031316
conv.tryUpdateLocalStorageData(client: self, outdated: true)
1317+
#endif
13041318
}
13051319
} else if let notifications = droppable["notifications"] as? [[String: Any]] {
13061320
mapReduce(notifications)
@@ -1848,7 +1862,9 @@ extension IMClient {
18481862
let _ = self.validInFetchingNotificationsCachedConvMapSnapshot?[conversationID]
18491863
{
18501864
conversation.isOutdated = true
1865+
#if canImport(GRDB)
18511866
conversation.tryUpdateLocalStorageData(client: client, outdated: true)
1867+
#endif
18521868
}
18531869
if let event: IMConversationEvent = tuple.event {
18541870
client.eventQueue.async {

0 commit comments

Comments
 (0)