合并Future Publisher不会被释放 [英] Combine Future Publisher is not getting deallocated

查看:35
本文介绍了合并Future Publisher不会被释放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Combine Future来包装异步块操作,并向该发布者添加订阅者以接收值.XCode内存图和仪器泄漏图本身未显示对这些将来对象的引用.我不知道他们为什么还在附近.

I am using the Combine Future to wrap around an async block operation and adding a subscriber to that publisher to receive the values.. I am noticing the future object is not getting deallocated, even after the subscribers are deallocated. The XCode memory graph and instruments leaks graph itself shows no reference to these future objects. I am puzzled why are they still around.

   func getUsers(forceRefresh: Bool =  false) -> AnyPublisher<[User], Error> {
        let future = Future<[User], Error> { [weak self] promise in
            guard let params = self?.params else {
                promise(.failure(CustomErrors.invalidData))
                return
            }
            
            self?.restApi.getUsers(params: params, forceRefresh: forceRefresh, success: { (users: [User]?, _) in
                guard let users = users else {
                    return promise(.failure(CustomErrors.invalidData))
                }
                
                promise(.success(users))
            }) { (error: Error) in
                promise(.failure(error))
            }
        }
        
        return future.eraseToAnyPublisher()
   }

这是我添加订阅的方式:

Here's how I am adding a subscription:

   self.userDataService?.getUsers(forceRefresh: forceRefresh)
        .sink(receiveCompletion: { [weak self] completion in
            self?.isLoading = false
            if case let .failure(error) = completion {
                self?.publisher.send(.error(error))
                return
            }
            
            guard let users = self?.users, !users.isEmpty else {
                self?.publisher.send(.empty)
                return
            }
            
            self?.publisher.send(.data(users))
        }) { [weak self] (response: Array<User>) in
            self?.users = response
    }.store(in: &self.subscribers)

    deinit {
        self.subscribers.removeAll()
    }

这是上面创建的将来内存泄漏的屏幕截图.即使订阅者全部删除,它仍然存在.仪器还显示了类似的内存图.关于可能导致此问题的任何想法?

This is the screenshot of the leaked memory for the future that got created above.. It's still staying around even after the subscribers are all deleted. Instruments is also showing a similar memory graph. Any thoughts on what could be causing this ??

推荐答案

未来调用在创建后立即关闭,这可能会对此产生影响.您可以尝试将Future包装在推迟中,这样就不会创建它了直到发生订阅为止(无论如何,这都是您期望通过扫描代码获得的结果).

Future invokes its closure immediately upon creation, which may be impacting this. You might try wrapping the Future in Deferred so that it isn't created until a subscription happens (which may be what you're expecting anyway from scanning the code).

它立即创建一个事实是(我认为)没有订阅者时在列出的对象中得到反映.

The fact that it's creating one immediately is what (I think) is being reflected in the objects listed when there are no subscribers.

这篇关于合并Future Publisher不会被释放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆