Skip to content

Commit

Permalink
save endpoint in ondemandservice as weak var
Browse files Browse the repository at this point in the history
  • Loading branch information
kosyloa committed Apr 23, 2024
1 parent 96e2392 commit ab828f5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
21 changes: 16 additions & 5 deletions DXFeedFramework/OnDemand/OnDemandService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,43 @@ import Foundation
public class OnDemandService {
var native: NativeOnDemandService

private init(native: NativeOnDemandService) {
private init(native: NativeOnDemandService, endpoint: DXEndpoint?) {
self.native = native
self.pEndpoint = endpoint
}

/// Returns on-demand service for the default ``DXEndpoint`` instance with
/// ``DXEndpoint/Role-swift.enum/onDemandFeed``role that is
/// not connected to any other real-time or delayed data feed.
///
/// it is shortcut for ``getInstance(endpoint:)`` with ``DXEndpoint/getInstance(_:)`` for ``DXEndpoint/Role-swift.enum/onDemandFeed``
/// - Returns: ``OnDemandService``
/// - Throws: ``GraalException``. Rethrows exception from Java.
public static func getInstance() throws -> OnDemandService {
return OnDemandService(native: try NativeOnDemandService.getInstance())
return OnDemandService(native: try NativeOnDemandService.getInstance(), endpoint: nil)
}

/// Returns on-demand service for the specified ``DXEndpoint``
/// - Returns: ``OnDemandService``
/// - Throws: ``GraalException``. Rethrows exception from Java.
public static func getInstance(endpoint: DXEndpoint) throws -> OnDemandService {
return OnDemandService(native: try NativeOnDemandService.getInstance(endpoint: endpoint.nativeEndpoint))
return OnDemandService(native: try NativeOnDemandService.getInstance(endpoint: endpoint.nativeEndpoint), endpoint: endpoint)
}

private weak var pEndpoint: DXEndpoint?

/// Returns ``DXEndpoint`` that is associated with this on-demand service.
public lazy var endpoint: DXEndpoint? = {
private lazy var endpoint: DXEndpoint? = {
return try? native.getEndpoint()
}()

/// Returns ``DXEndpoint`` that is associated with this on-demand service.
public func getEndpoint() -> DXEndpoint? {
if let endpoint = pEndpoint {
return endpoint
}
return endpoint
}

/// Returns true when on-demand historical data replay mode is supported.
public var isReplaySupported: Bool? {
return native.isReplaySupported
Expand Down
14 changes: 10 additions & 4 deletions DXFeedFrameworkTests/DXOnDemandServiceTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ import DXFeedFramework
final class DXOnDemandServiceTest: XCTestCase {
func testCreateService() throws {
let service = try OnDemandService.getInstance()
XCTAssertNotNil(service.getEndpoint())
XCTAssert(service.getEndpoint() === service.getEndpoint())
checkNullBalues(service: service)
let endpoint = try DXEndpoint.create(.onDemandFeed)
let service2 = try OnDemandService.getInstance(endpoint: endpoint)
var endpoint: DXEndpoint? = try DXEndpoint.create(.onDemandFeed)
let service2 = try OnDemandService.getInstance(endpoint: endpoint!)
checkNullBalues(service: service2)
let endpoint2 = service2.endpoint
let endpoint3 = service2.endpoint
weak var endpoint2 = service2.getEndpoint()
weak var endpoint3 = service2.getEndpoint()
XCTAssert(endpoint2 === endpoint3)
XCTAssert(endpoint === endpoint3)
endpoint = nil
XCTAssert(endpoint2 !== service2.getEndpoint())

}

private func checkNullBalues(service: OnDemandService) {
Expand Down

0 comments on commit ab828f5

Please sign in to comment.