Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions LeanCloudTests/IMClientTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,10 @@ class IMClientTestCase: RTMBaseTestCase {
}
}

RTMConnectionManager.default.protobuf1Map.removeAll()
RTMConnectionManager.default.protobuf3Map.removeAll()
RTMConnectionManager.default
.imProtobuf1Registry.removeAll()
RTMConnectionManager.default
.imProtobuf3Registry.removeAll()
LCApplication.default._currentInstallation = nil

let installation2 = LCApplication.default.currentInstallation
Expand Down
18 changes: 12 additions & 6 deletions LeanCloudTests/IMConversationTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -489,8 +489,10 @@ class IMConversationTestCase: RTMBaseTestCase {
return
}

RTMConnectionManager.default.protobuf1Map.removeAll()
RTMConnectionManager.default.protobuf3Map.removeAll()
RTMConnectionManager.default
.imProtobuf1Registry.removeAll()
RTMConnectionManager.default
.imProtobuf3Registry.removeAll()

let clientB = try! IMClient(
ID: clientBID,
Expand Down Expand Up @@ -1454,8 +1456,10 @@ class IMConversationTestCase: RTMBaseTestCase {
return
}

RTMConnectionManager.default.protobuf1Map.removeAll()
RTMConnectionManager.default.protobuf3Map.removeAll()
RTMConnectionManager.default
.imProtobuf1Registry.removeAll()
RTMConnectionManager.default
.imProtobuf3Registry.removeAll()

guard let clientB = newOpenedClient() else {
XCTFail()
Expand Down Expand Up @@ -1558,8 +1562,10 @@ class IMConversationTestCase: RTMBaseTestCase {
let delegatorA = IMClientTestCase.Delegator()
clientA.delegate = delegatorA

RTMConnectionManager.default.protobuf1Map.removeAll()
RTMConnectionManager.default.protobuf3Map.removeAll()
RTMConnectionManager.default
.imProtobuf1Registry.removeAll()
RTMConnectionManager.default
.imProtobuf3Registry.removeAll()

guard let clientB = newOpenedClient() else {
return
Expand Down
6 changes: 4 additions & 2 deletions LeanCloudTests/IMMessageTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1495,8 +1495,10 @@ extension IMMessageTestCase {
delegatorMap[client.ID] = delegator
clientIDs.append(client.ID)
if !shouldConnectionShared {
RTMConnectionManager.default.protobuf1Map.removeAll()
RTMConnectionManager.default.protobuf3Map.removeAll()
RTMConnectionManager.default
.imProtobuf1Registry.removeAll()
RTMConnectionManager.default
.imProtobuf3Registry.removeAll()
}
}
if let clientID: String = clientIDs.first,
Expand Down
41 changes: 26 additions & 15 deletions LeanCloudTests/RTMConnectionTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ class RTMConnectionTestCase: RTMBaseTestCase {

override func setUp() {
super.setUp()
RTMConnectionManager.default.protobuf1Map.removeAll()
RTMConnectionManager.default.protobuf3Map.removeAll()
RTMConnectionManager.default
.imProtobuf1Registry.removeAll()
RTMConnectionManager.default
.imProtobuf3Registry.removeAll()
}

override func tearDown() {
RTMConnectionManager.default.protobuf1Map.removeAll()
RTMConnectionManager.default.protobuf3Map.removeAll()
RTMConnectionManager.default
.imProtobuf1Registry.removeAll()
RTMConnectionManager.default
.imProtobuf3Registry.removeAll()
super.tearDown()
}

Expand All @@ -34,8 +38,15 @@ class RTMConnectionTestCase: RTMBaseTestCase {
[RTMConnection.LCIMProtocol.protobuf1,
RTMConnection.LCIMProtocol.protobuf3]
{
let connectionMap: () -> [String : [String : RTMConnection]] = {
RTMConnectionManager.default.getMap(protocol: imProtocol)
let connectionRegistry: () -> RTMConnectionManager.InstantMessagingRegistry = {
let registry: RTMConnectionManager.InstantMessagingRegistry
switch imProtocol {
case .protobuf3:
registry = RTMConnectionManager.default.imProtobuf3Registry
case .protobuf1:
registry = RTMConnectionManager.default.imProtobuf1Registry
}
return registry
}
let peerID1 = "peerID1"
let peerID2 = "peerID2"
Expand All @@ -44,8 +55,8 @@ class RTMConnectionTestCase: RTMBaseTestCase {
application: application,
service: .instantMessaging(ID: peerID1, protocol: imProtocol)
)
XCTAssertNotNil(connectionMap()[application.id]?[peerID1])
XCTAssertEqual(connectionMap()[application.id]?.count, 1)
XCTAssertNotNil(connectionRegistry()[application.id]?[peerID1])
XCTAssertEqual(connectionRegistry()[application.id]?.count, 1)
} catch {
XCTFail("\(error)")
}
Expand All @@ -63,8 +74,8 @@ class RTMConnectionTestCase: RTMBaseTestCase {
application: application,
service: .instantMessaging(ID: peerID2, protocol: imProtocol)
)
XCTAssertTrue(connectionMap()[application.id]?[peerID1] === connectionMap()[application.id]?[peerID2])
XCTAssertEqual(connectionMap()[application.id]?.count, 2)
XCTAssertTrue(connectionRegistry()[application.id]?[peerID1] === connectionRegistry()[application.id]?[peerID2])
XCTAssertEqual(connectionRegistry()[application.id]?.count, 2)

RTMConnectionManager.default.unregister(
application: application,
Expand All @@ -75,17 +86,17 @@ class RTMConnectionTestCase: RTMBaseTestCase {
service: .instantMessaging(ID: peerID2, protocol: imProtocol)
)

XCTAssertEqual(connectionMap()[application.id]?.count, 0)
XCTAssertEqual(connectionRegistry()[application.id]?.count, 0)

_ = try! RTMConnectionManager.default.register(
application: application,
service: .instantMessaging(ID: peerID1, protocol: imProtocol)
)
XCTAssertNotNil(connectionMap()[application.id]?[peerID1])
XCTAssertEqual(connectionMap()[application.id]?.count, 1)
XCTAssertNotNil(connectionRegistry()[application.id]?[peerID1])
XCTAssertEqual(connectionRegistry()[application.id]?.count, 1)
}
XCTAssertEqual(RTMConnectionManager.default.protobuf1Map[application.id]?.count, 1)
XCTAssertEqual(RTMConnectionManager.default.protobuf3Map[application.id]?.count, 1)
XCTAssertEqual(RTMConnectionManager.default.imProtobuf1Registry[application.id]?.count, 1)
XCTAssertEqual(RTMConnectionManager.default.imProtobuf3Registry[application.id]?.count, 1)

application.unregister()
}
Expand Down
88 changes: 41 additions & 47 deletions Sources/Foundation/Application.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@

import Foundation

/**
LeanCloud application.

An `LCApplication` object is an abstract of remote LeanCloud application.

It is a context of application-specific settings and objects.
*/
/// LeanCloud Application.
///
/// an `LCApplication` object is an abstraction of LeanCloud application,
/// it is the context of application-specific settings and objects.
public class LCApplication {

// MARK: Registry
Expand All @@ -23,7 +20,6 @@ public class LCApplication {

// MARK: Log

/// Application log level.
public enum LogLevel: Int, Comparable {
case off
case error
Expand All @@ -36,7 +32,7 @@ public class LCApplication {
}
}

/// Log level.
/// Console log level, default is `LogLevel.off`.
public static var logLevel: LogLevel = .off

// MARK: Basic
Expand All @@ -57,7 +53,7 @@ public class LCApplication {

// MARK: Configuration

/// Module of Customizable Server.
/// Module of customizable server.
public enum ServerCustomizableModule {
case api(_ url: String)
case push(_ url: String)
Expand All @@ -78,69 +74,67 @@ public class LCApplication {
}
}

/// Environment of the Application
/// Environment of the application.
public struct Environment: OptionSet {
public let rawValue: Int

public init(rawValue: Int) {
self.rawValue = rawValue
}

/// development environment of Cloud Engine
/// Development environment of Cloud Engine Service.
public static let cloudEngineDevelopment = Environment(rawValue: 1 << 0)

/// development environment of Push
/// Development environment of Push Service.
public static let pushDevelopment = Environment(rawValue: 1 << 1)

/// default is production environment
/// Default is production environment.
public static let `default`: Environment = []
}

var cloudEngineMode: String {
return self.configuration
.environment
return self.configuration.environment
.contains(.cloudEngineDevelopment)
? "0" : "1"
}

var pushMode: String {
return self.configuration
.environment
return self.configuration.environment
.contains(.pushDevelopment)
? "dev" : "prod"
}

/// Application Configuration.
/// Configuration of the application.
public struct Configuration: CustomDebugStringConvertible {
public static let `default` = Configuration()

/// Customized Servers
/// Customized Servers, default is `[]`.
public var customizedServers: [ServerCustomizableModule]

/// Environment
/// Environment, default is `Environment.default`.
public var environment: Environment

/// HTTP Request Timeout Interval, default is 60.0 second.
/// HTTP Request Timeout Interval, default is `60.0` second.
public var HTTPRequestTimeoutInterval: TimeInterval

/// URL Cache for HTTP Response, default is nil.
/// URL Cache for HTTP Response, default is `nil`.
public var HTTPURLCache: URLCache?

/// RTM Connecting Timeout Interval, default is 15.0 second.
/// RTM Connecting Timeout Interval, default is `60.0` second.
public var RTMConnectingTimeoutInterval: TimeInterval

/// RTM Command Timeout Interval, default is 30.0 second.
/// RTM Command Timeout Interval, default is `30.0` second.
public var RTMCommandTimeoutInterval: TimeInterval

/// RTM Custom Server URL.
/// RTM Custom Server URL, default is `nil`.
public var RTMCustomServerURL: URL?

public init(
customizedServers: [ServerCustomizableModule] = [],
environment: Environment = .default,
HTTPRequestTimeoutInterval: TimeInterval = 60.0,
HTTPURLCache: URLCache? = nil,
RTMConnectingTimeoutInterval: TimeInterval = 15.0,
RTMConnectingTimeoutInterval: TimeInterval = 60.0,
RTMCommandTimeoutInterval: TimeInterval = 30.0,
RTMCustomServerURL: URL? = nil)
{
Expand Down Expand Up @@ -179,7 +173,7 @@ public class LCApplication {
}
}

/// Application Configuration.
/// Application configuration.
public private(set) var configuration: Configuration = .default

// MARK: Region
Expand Down Expand Up @@ -224,7 +218,7 @@ public class LCApplication {

// MARK: Current Installation

/// Current Installation.
/// Current installation.
public var currentInstallation: LCInstallation {
if let installation = self._currentInstallation {
return installation
Expand All @@ -251,7 +245,7 @@ public class LCApplication {

// MARK: Current User

/// Current User.
/// Current user.
public var currentUser: LCUser? {
set {
self._currentUser = newValue
Expand Down Expand Up @@ -289,10 +283,12 @@ public class LCApplication {
init() {}

/// Create an application.
/// - Parameter id: The ID.
/// - Parameter key: The Key.
/// - Parameter serverURL: The server URL string.
/// - Parameter configuration: The Configuration.
/// - Parameters:
/// - id: see property `id`.
/// - key: see property `key`.
/// - serverURL: see property `serverURL`, default is `nil`, MUST provide if the application is in China.
/// - configuration: see property `configuration`, default is `Configuration.default`
/// - Throws: If `serverURL` not provide in some region.
public init(
id: String,
key: String,
Expand All @@ -305,34 +301,32 @@ public class LCApplication {
key: key,
serverURL: serverURL,
configuration: configuration)

LCApplication.registry[id] = self
}

/// Setup the application.
/// - Parameter id: The ID.
/// - Parameter key: The Key.
/// - Parameter serverURL: The server URL string.
/// - Parameter configuration: The Configuration.
/// Set up an application.
/// - Parameters:
/// - id: see property `id`.
/// - key: see property `key`.
/// - serverURL: see property `serverURL`, default is `nil`, MUST provide if the application is in China.
/// - configuration: see property `configuration`, default is `Configuration.default`
/// - Throws: If `serverURL` not provide in some region.
public func set(
id: String,
key: String,
serverURL: String? = nil,
configuration: Configuration = .default)
throws
{
if let oldID = self.id {
self._currentInstallation = nil
self._currentUser = nil
LCApplication.registry.removeValue(forKey: oldID)
if let _ = self.id {
// clean previous context
self.unregister()
}

try self.doInitializing(
id: id,
key: key,
serverURL: serverURL,
configuration: configuration)

LCApplication.registry[id] = self
}

Expand Down Expand Up @@ -381,7 +375,7 @@ public class LCApplication {

// MARK: Deinit

/// should unregister the application before releasing it's memory.
/// Before release the instance of the application, should unregister it to break circular reference.
public func unregister() {
LCApplication.registry.removeValue(forKey: self.id)
self._currentInstallation = nil
Expand Down
5 changes: 4 additions & 1 deletion Sources/RTM/LiveQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ public class LiveQuery {
var queryID: RemoteQueryID?

deinit {
LiveQueryClientManager.default.releaseLocalInstanceID(self.localInstanceID)
LiveQueryClientManager.default
.unregister(application: self.application)
LiveQueryClientManager.default
.releaseLocalInstanceID(self.localInstanceID)
}

public init(
Expand Down
Loading