在发布模式下不连接到RPC服务器,但是在调试模式下可以正常工作

问题描述 投票:0回答:1

我有一个执行以下操作的命令行应用程序:

  • 下载带有种子链接的RSS feed
  • 将其存储在sqlite数据库中,并将其标记为“添加”或“忽略”
  • 连接到传输服务器(在我的本地网络中)
  • 从标记为“已添加”的sqlite加载项目并添加到传输服务器

以上在调试模式下工作正常。但是,当我为发布而构建并尝试直接运行或从启动运行时,它总是超时。最相关的代码在下面的main.swift中。

private func getTransmissionClient() -> Transmission? {
        let client = Transmission(
            baseURL: serverConfig.server,
            username: serverConfig.username,
            password: serverConfig.password)

        var cancellables = Set<AnyCancellable>()

        let group = DispatchGroup()
        group.enter()
        print("[INFO] Connecting to client")
        client.request(.rpcVersion)
            .sink(
                receiveCompletion: { _ in group.leave() },
                receiveValue: { rpcVersion in
                    print("[INFO]: Successfully Connected! RPC Version: \(rpcVersion)")
            })
            .store(in: &cancellables)
        let wallTimeout = DispatchWallTime.now() +
            DispatchTimeInterval.seconds(serverConfig.secondsTimeout ?? 15)
        let res = group.wait(wallTimeout: wallTimeout)

        if res == DispatchTimeoutResult.success {
            return client
        } else {
            return nil
        }

    }

    public func updateTransmission() throws {

        print("[INFO] [\(Date())] Starting Transmission Update")

        let clientOpt = getTransmissionClient()
        guard let client = clientOpt else {
            print("[ERROR] Failed to connect to transmission client")
            exit(1)
        }

        var cancellables = Set<AnyCancellable>()

        let items = try store.getPendingDownload()
        print("[INFO] [\(Date())] Adding \(items.count) new items to transmission")

        let group = DispatchGroup()
        for item in items {

            let linkComponents = "\(item.link)".components(separatedBy: "&")
            assert(linkComponents.count > 0, "Link seems wrong")

            group.enter()
            client.request(.add(url: item.link))
                .sink(receiveCompletion: { completion in
                    if case let .failure(error) = completion {
                        print("[Failure] \(item.title)")
                        print("[Failure] Details: \(error)")

                    }
                    group.leave()
                }, receiveValue: { _ in
                    print("[Success] \(item.title)")
                    do {
                        try self.store.update(item: item, with: .downloaded)

                    } catch {
                        print("[Error] Couldn't save new status to DB")
                    }
                })
                .store(in: &cancellables)
        }


        let wallTimeout = DispatchWallTime.now() +
            DispatchTimeInterval.seconds(serverConfig.secondsTimeout ?? 15)
        let res = group.wait(wallTimeout: wallTimeout)
        if res == DispatchTimeoutResult.success {
            print("Tasks successfully submitted")
        } else {
            print("Timed out")
            exit(1)
        }
    }

奇怪的是,在添加数据库之前,代码似乎可以正常工作。 DispatchGroup和Transmission-Swift客户端已经在那里。我猜我所做的事情被编译器“优化”了吗?尽管只是在看到StackOverflow上的其他问题之后,这只是猜测,但我仍不清楚。

我正在使用macOS 10.15和Swift 5.2.2。

github中提供的完整代码(链接到具有错误的特定提交)

swift rpc combine transmission-daemon
1个回答
0
投票

我在https://forums.swift.org/t/not-connecting-to-rpc-server-in-release-mode-but-works-fine-in-debug-mode/36251的Swift论坛上寻求帮助,这是要点:

  • Debug vs Release错误在Apple生态系统中很常见。
© www.soinside.com 2019 - 2024. All rights reserved.