From ab828f567d315c88f6caac6d9d50a76b366fff86 Mon Sep 17 00:00:00 2001 From: kosyloa Date: Tue, 23 Apr 2024 17:01:47 +0200 Subject: [PATCH] save endpoint in ondemandservice as weak var --- .../OnDemand/OnDemandService.swift | 21 ++++++++++++++----- .../DXOnDemandServiceTest.swift | 14 +++++++++---- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/DXFeedFramework/OnDemand/OnDemandService.swift b/DXFeedFramework/OnDemand/OnDemandService.swift index 4443d2cb0..d4efb8f23 100644 --- a/DXFeedFramework/OnDemand/OnDemandService.swift +++ b/DXFeedFramework/OnDemand/OnDemandService.swift @@ -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 diff --git a/DXFeedFrameworkTests/DXOnDemandServiceTest.swift b/DXFeedFrameworkTests/DXOnDemandServiceTest.swift index ee9107be7..ffa9edf09 100644 --- a/DXFeedFrameworkTests/DXOnDemandServiceTest.swift +++ b/DXFeedFrameworkTests/DXOnDemandServiceTest.swift @@ -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) {