CADisplayLink由于remove(from:forMode :)没有取消分配视图控制器

时间:2019-05-29 07:43:14

标签: ios swift cadisplaylink

我正面临一个奇怪的问题。

所以我创建了实例并按如下所示进行分配

var displayLink:CADisplayLink?

private func setupDisplayLink () {

    self.displayLink = CADisplayLink(target: self, selector: #selector(displayLinkDidFire(link:)))
    self.displayLink?.preferredFramesPerSecond = 30
    self.displayLink?.add(to: .main, forMode: .common)
}

现在要从此Answer

中删除我拥有的显示链接
self.displayLink?.remove(from: .main, forMode: .common) // comment this line and view controller correctly deallocated 
self.displayLink?.invalidate()

因为我的self.displayLink?.remove(from: .main, forMode: .common)方法的第deinit行未在视图控制器中调用

在内存图中,我无法确定真正的问题。我很幸运,我尝试对此行发表评论,而且行之有效

enter image description here

为什么这样?

1 个答案:

答案 0 :(得分:0)

像这样停止displayLink:

from typing import *
import math

def partition(lst: List[int], K: int) -> int:
    '''\
        Time: O(logN * K * N ^ 2), Space: O(N)
        logN for binary search, and KN^2 for backtrack search
    '''

    if K >= len(lst):
        return max(lst)

    total = sum(lst)
    low = math.ceil(total / K)
    high = total

    def canfit(lst, topbar, K):
        groups = [0] * K
        is_taken = [False] * len(lst)

        def helper(idx_lst, idx_group):
            if idx_lst == len(lst):
                return True
            elif idx_group == len(groups):
                return False
            else:
                for i, num in enumerate(lst):
                    if not is_taken[i] and groups[idx_group] + num <= topbar:
                        groups[idx_group] += num
                        is_taken[i] = True
                        if helper(idx_lst+1, idx_group):
                            return True
                        is_taken[i] = False
                        groups[idx_group] -= num
                if helper(idx_lst, idx_group+1):
                    return True

        return helper(0, 0)

    res = None
    while low <= high:
        mid = (low + high) // 2
        if canfit(lst, mid, K):
            res = mid
            high = mid - 1
        else:
            low = mid + 1

    return res

assert partition([1,4,2,8,5,7], 3) == 9
assert partition([1], 2) == 1