简体   繁体   中英

Application crashes when calling invalidateLayout method in iOS 9

I want to update UICollectionView layout every minute. It works well in iOS 10 but it crashes in iOS 9 (I tested it in iPad 2)
invalidateLayout method makes UICollectionView update. and it crashes when calling this method.

    override init() {
        super.init()
        initializeMinuteTick()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    deinit {
        minuteTimer?.invalidate()
    }

    private func initializeMinuteTick() {
        guard let validDate = Date().addMinute(num: 1) else { return }

        minuteTimer = Timer(fireAt: validDate, interval: TimeInterval(60), target: self, selector: #selector(minuteTick), userInfo: nil, repeats: true)

        if let minuteTimer = minuteTimer {
            RunLoop.current.add(minuteTimer, forMode: .defaultRunLoopMode)
        }
    }

    func minuteTick() {
        cachedCurrentTimeComponents.removeAll()

        // CRASH ----------------
        invalidateLayout()
        // CRASH ----------------
    }

Error:

Thread 1: EXC_BAD_ACCESS

Try calling reloadData() on the collectionView . Also make sure that dataSource for the collectionView is already filled with data before calling the reloading functions (maybe there is a place where you force unwrap a value?).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM