首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何让ClockKit生成100个以上的时间线条目?

如何让ClockKit生成100个以上的时间线条目?
EN

Stack Overflow用户
提问于 2018-08-31 10:02:53
回答 1查看 331关注 0票数 4

我正在尝试创建一个ClockKit复杂性,为一个人的下一个班次开始提供数据,但是没有足够的时间线条目被生成或生成得足够频繁,所以有时,数据在一定时间后是不准确的。

我试过调试,得出的结论是只有100个时间线条目正在创建,对于每个方向的1:40小时的日期来说都很好,对于我的应用程序来说还不够。我已经阅读了文档和用于扩展时间线的方法,但发现它每天只能使用一定的次数。

我在我的getTimelineEntries(complication:date:limit:handler)中包含了print(String(limit) + " After"),以查看它生成了多少。

我该怎么做才能使我的并发症从00:00延长到23:59?此外,为什么时间线不会在它超过未来最多的条目时自动扩展?这似乎有悖于苹果公司制造复杂局面的意图。

我在下面加上了我的ComplicationController.swift`。

代码语言:javascript
复制
//
//  ComplicationController.swift
//  Watch Bell Schedule Extension
//
//  Created by Joseph on 8/23/18.
//  Copyright © 2018 juniorRubyist. All rights reserved.
//

import ClockKit


class ComplicationController: NSObject, CLKComplicationDataSource {

    // MARK: - Timeline Configuration

    func getSupportedTimeTravelDirections(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTimeTravelDirections) -> Void) {
        handler([.forward, .backward])
    }

    func getTimelineStartDate(for complication: CLKComplication, withHandler handler: @escaping (Date?) -> Void) {
        handler(Date().addingTimeInterval(-256200))
    }

    func getTimelineEndDate(for complication: CLKComplication, withHandler handler: @escaping (Date?) -> Void) {
        handler(Date().addingTimeInterval(256200))
    }

    func getPrivacyBehavior(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationPrivacyBehavior) -> Void) {
        handler(.showOnLockScreen)
    }

    // MARK: - Timeline Population

    func getCurrentTimelineEntry(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTimelineEntry?) -> Void) {
        let date = Date()
        let outputFormat = DateFormatter()
        outputFormat.locale = Locale(identifier:"en_US")
        outputFormat.dateFormat = "e"
        let override = 0
        let currentSchedule = currentSch((outputFormat.string(from: date)), unless: override)
        let nextPeriodObj = nextPeriod(on: currentSchedule, at: date)
        outputFormat.dateFormat = "hh:mm"

        switch complication.family {
        case .utilitarianLarge:
            let complicationTemplate = CLKComplicationTemplateUtilitarianLargeFlat()
            let compText: String
            if nextPeriodObj != Period(" ", 0, 0) {
                compText = "? \(outputFormat.string(from: nextPeriodObj.time)) ? \(nextPeriodObj.name)"
            } else {
                compText = "? None Today"
            }
            complicationTemplate.textProvider = CLKSimpleTextProvider(text: compText)

            let timelineEntry = CLKComplicationTimelineEntry(date: date, complicationTemplate: complicationTemplate)
            handler(timelineEntry)

        case .utilitarianSmall, .utilitarianSmallFlat:
            let complicationTemplate = CLKComplicationTemplateUtilitarianSmallFlat()
            let compText: String
            if nextPeriodObj != Period(" ", 0, 0) {
                compText = "? \(outputFormat.string(from: nextPeriodObj.time))"
            } else {
                compText = "?"
            }
            complicationTemplate.textProvider = CLKSimpleTextProvider(text: compText)

            let timelineEntry = CLKComplicationTimelineEntry(date: date, complicationTemplate: complicationTemplate)
            handler(timelineEntry)

        case .modularLarge:
            let complicationTemplate = CLKComplicationTemplateModularLargeStandardBody()
            let headerText, body1Text, body2Text: String

            if nextPeriodObj != Period(" ", 0, 0) {
                headerText = "Bell Schedule"
                body1Text = "\(nextPeriodObj.name)"
                body2Text = "\(outputFormat.string(from: nextPeriodObj.time))"
            } else {
                headerText = "No more bells."
                body1Text = ""
                body2Text = ""
            }

            complicationTemplate.headerTextProvider = CLKSimpleTextProvider(text: headerText)
            complicationTemplate.headerTextProvider.tintColor = TitanColors.red
            complicationTemplate.body1TextProvider = CLKSimpleTextProvider(text: body1Text)
            complicationTemplate.body2TextProvider = CLKSimpleTextProvider(text: body2Text)

            let timelineEntry = CLKComplicationTimelineEntry(date: date, complicationTemplate: complicationTemplate)
            handler(timelineEntry)

        case .modularSmall, .circularSmall, .extraLarge:

            let body1Text, body2Text: String

            if nextPeriodObj != Period(" ", 0, 0) {
                body1Text = "\(nextPeriodObj.name)"
                body2Text = "\(outputFormat.string(from: nextPeriodObj.time))"
            } else {
                body1Text = "NO"
                body2Text = "BELL"
            }

            if complication.family == .modularSmall {
                let complicationTemplate = CLKComplicationTemplateModularSmallStackText()
                complicationTemplate.line1TextProvider = CLKSimpleTextProvider(text: body1Text)
                complicationTemplate.line1TextProvider.tintColor = TitanColors.red
                complicationTemplate.line2TextProvider = CLKSimpleTextProvider(text: body2Text)

                let timelineEntry = CLKComplicationTimelineEntry(date: date, complicationTemplate: complicationTemplate)
                handler(timelineEntry)
            } else if complication.family == .circularSmall {
                let complicationTemplate = CLKComplicationTemplateCircularSmallStackText()
                complicationTemplate.line1TextProvider = CLKSimpleTextProvider(text: body1Text)
                complicationTemplate.line2TextProvider = CLKSimpleTextProvider(text: body2Text)

                let timelineEntry = CLKComplicationTimelineEntry(date: date, complicationTemplate: complicationTemplate)
                handler(timelineEntry)
            } else if complication.family == .extraLarge {
                let complicationTemplate = CLKComplicationTemplateExtraLargeStackText()
                complicationTemplate.line1TextProvider = CLKSimpleTextProvider(text: body1Text)
                complicationTemplate.line1TextProvider.tintColor = TitanColors.red
                complicationTemplate.line2TextProvider = CLKSimpleTextProvider(text: body2Text)

                let timelineEntry = CLKComplicationTimelineEntry(date: date, complicationTemplate: complicationTemplate)
                handler(timelineEntry)
            }
        }
    }

    func getTimelineEntries(for complication: CLKComplication, before originalDate: Date, limit: Int, withHandler handler: @escaping ([CLKComplicationTimelineEntry]?) -> Void) {
        print(String(limit) + " Before")
        var entries = [CLKComplicationTimelineEntry]()
        for i in (1...(limit + 1)).reversed() {
            var date = originalDate
            date.addTimeInterval(TimeInterval(-1 * (60 * i)))
            let outputFormat = DateFormatter()
            outputFormat.locale = Locale(identifier:"en_US")
            outputFormat.dateFormat = "e"
            let override = 0
            let currentSchedule = currentSch((outputFormat.string(from: date)), unless: override)
            let nextPeriodObj = nextPeriod(on: currentSchedule, at: date)
            outputFormat.dateFormat = "hh:mm"

            switch complication.family {
            case .utilitarianLarge:
                let complicationTemplate = CLKComplicationTemplateUtilitarianLargeFlat()
                let compText: String
                if nextPeriodObj != Period(" ", 0, 0) {
                    compText = "? \(outputFormat.string(from: nextPeriodObj.time)) ? \(nextPeriodObj.name)"
                } else {
                    compText = "? None Today"
                }
                complicationTemplate.textProvider = CLKSimpleTextProvider(text: compText)

                let timelineEntry = CLKComplicationTimelineEntry(date: date, complicationTemplate: complicationTemplate)
                entries.append(timelineEntry)

            case .utilitarianSmall, .utilitarianSmallFlat:
                let complicationTemplate = CLKComplicationTemplateUtilitarianSmallFlat()
                let compText: String
                if nextPeriodObj != Period(" ", 0, 0) {
                    compText = "? \(outputFormat.string(from: nextPeriodObj.time))"
                } else {
                    compText = "?"
                }
                complicationTemplate.textProvider = CLKSimpleTextProvider(text: compText)

                let timelineEntry = CLKComplicationTimelineEntry(date: date, complicationTemplate: complicationTemplate)
                entries.append(timelineEntry)

            case .modularLarge:
                let complicationTemplate = CLKComplicationTemplateModularLargeStandardBody()
                let headerText, body1Text, body2Text: String

                if nextPeriodObj != Period(" ", 0, 0) {
                    headerText = "Bell Schedule"
                    body1Text = "\(nextPeriodObj.name)"
                    body2Text = "\(outputFormat.string(from: nextPeriodObj.time))"
                } else {
                    headerText = "No more bells."
                    body1Text = ""
                    body2Text = ""
                }

                complicationTemplate.headerTextProvider = CLKSimpleTextProvider(text: headerText)
                complicationTemplate.headerTextProvider.tintColor = TitanColors.red
                complicationTemplate.body1TextProvider = CLKSimpleTextProvider(text: body1Text)
                complicationTemplate.body2TextProvider = CLKSimpleTextProvider(text: body2Text)

                let timelineEntry = CLKComplicationTimelineEntry(date: date, complicationTemplate: complicationTemplate)
                entries.append(timelineEntry)

            case .modularSmall, .circularSmall, .extraLarge:

                let body1Text, body2Text: String

                if nextPeriodObj != Period(" ", 0, 0) {
                    body1Text = "\(nextPeriodObj.name)"
                    body2Text = "\(outputFormat.string(from: nextPeriodObj.time))"
                } else {
                    body1Text = "NO"
                    body2Text = "BELL"
                }

                if complication.family == .modularSmall {
                    let complicationTemplate = CLKComplicationTemplateModularSmallStackText()
                    complicationTemplate.line1TextProvider = CLKSimpleTextProvider(text: body1Text)
                    complicationTemplate.line1TextProvider.tintColor = TitanColors.red
                    complicationTemplate.line2TextProvider = CLKSimpleTextProvider(text: body2Text)

                    let timelineEntry = CLKComplicationTimelineEntry(date: date, complicationTemplate: complicationTemplate)
                    entries.append(timelineEntry)
                } else if complication.family == .circularSmall {
                    let complicationTemplate = CLKComplicationTemplateCircularSmallStackText()
                    complicationTemplate.line1TextProvider = CLKSimpleTextProvider(text: body1Text)
                    complicationTemplate.line2TextProvider = CLKSimpleTextProvider(text: body2Text)

                    let timelineEntry = CLKComplicationTimelineEntry(date: date, complicationTemplate: complicationTemplate)
                    entries.append(timelineEntry)
                } else if complication.family == .extraLarge {
                    let complicationTemplate = CLKComplicationTemplateExtraLargeStackText()
                    complicationTemplate.line1TextProvider = CLKSimpleTextProvider(text: body1Text)
                    complicationTemplate.line1TextProvider.tintColor = TitanColors.red
                    complicationTemplate.line2TextProvider = CLKSimpleTextProvider(text: body2Text)

                    let timelineEntry = CLKComplicationTimelineEntry(date: date, complicationTemplate: complicationTemplate)
                    entries.append(timelineEntry)
                }
            }
        }
        handler(entries)
    }

    func getTimelineEntries(for complication: CLKComplication, after originalDate: Date, limit: Int, withHandler handler: @escaping ([CLKComplicationTimelineEntry]?) -> Void) {
        var entries = [CLKComplicationTimelineEntry]()
        print(String(limit) + " After")
        for i in 1...(limit + 1) {
            var date = originalDate
            date.addTimeInterval(TimeInterval(60 * i))
            let outputFormat = DateFormatter()
            outputFormat.locale = Locale(identifier:"en_US")
            outputFormat.dateFormat = "e"
            let override = 0
            let currentSchedule = currentSch((outputFormat.string(from: date)), unless: override)
            let nextPeriodObj = nextPeriod(on: currentSchedule, at: date)
            outputFormat.dateFormat = "hh:mm"

            switch complication.family {
            case .utilitarianLarge:
                let complicationTemplate = CLKComplicationTemplateUtilitarianLargeFlat()
                let compText: String
                if nextPeriodObj != Period(" ", 0, 0) {
                    compText = "? \(outputFormat.string(from: nextPeriodObj.time)) ? \(nextPeriodObj.name)"
                } else {
                    compText = "? None Today"
                }
                complicationTemplate.textProvider = CLKSimpleTextProvider(text: compText)

                let timelineEntry = CLKComplicationTimelineEntry(date: date, complicationTemplate: complicationTemplate)
                entries.append(timelineEntry)

            case .utilitarianSmall, .utilitarianSmallFlat:
                let complicationTemplate = CLKComplicationTemplateUtilitarianSmallFlat()
                let compText: String
                if nextPeriodObj != Period(" ", 0, 0) {
                    compText = "? \(outputFormat.string(from: nextPeriodObj.time))"
                } else {
                    compText = "?"
                }
                complicationTemplate.textProvider = CLKSimpleTextProvider(text: compText)

                let timelineEntry = CLKComplicationTimelineEntry(date: date, complicationTemplate: complicationTemplate)
                entries.append(timelineEntry)

            case .modularLarge:
                let complicationTemplate = CLKComplicationTemplateModularLargeStandardBody()
                let headerText, body1Text, body2Text: String

                if nextPeriodObj != Period(" ", 0, 0) {
                    headerText = "Bell Schedule"
                    body1Text = "\(nextPeriodObj.name)"
                    body2Text = "\(outputFormat.string(from: nextPeriodObj.time))"
                } else {
                    headerText = "No more bells."
                    body1Text = ""
                    body2Text = ""
                }

                complicationTemplate.headerTextProvider = CLKSimpleTextProvider(text: headerText)
                complicationTemplate.headerTextProvider.tintColor = TitanColors.red
                complicationTemplate.body1TextProvider = CLKSimpleTextProvider(text: body1Text)
                complicationTemplate.body2TextProvider = CLKSimpleTextProvider(text: body2Text)

                let timelineEntry = CLKComplicationTimelineEntry(date: date, complicationTemplate: complicationTemplate)
                entries.append(timelineEntry)

            case .modularSmall, .circularSmall, .extraLarge:

                let body1Text, body2Text: String

                if nextPeriodObj != Period(" ", 0, 0) {
                    body1Text = "\(nextPeriodObj.name)"
                    body2Text = "\(outputFormat.string(from: nextPeriodObj.time))"
                } else {
                    body1Text = "NO"
                    body2Text = "BELL"
                }

                if complication.family == .modularSmall {
                    let complicationTemplate = CLKComplicationTemplateModularSmallStackText()
                    complicationTemplate.line1TextProvider = CLKSimpleTextProvider(text: body1Text)
                    complicationTemplate.line1TextProvider.tintColor = TitanColors.red
                    complicationTemplate.line2TextProvider = CLKSimpleTextProvider(text: body2Text)

                    let timelineEntry = CLKComplicationTimelineEntry(date: date, complicationTemplate: complicationTemplate)
                    entries.append(timelineEntry)
                } else if complication.family == .circularSmall {
                    let complicationTemplate = CLKComplicationTemplateCircularSmallStackText()
                    complicationTemplate.line1TextProvider = CLKSimpleTextProvider(text: body1Text)
                    complicationTemplate.line2TextProvider = CLKSimpleTextProvider(text: body2Text)

                    let timelineEntry = CLKComplicationTimelineEntry(date: date, complicationTemplate: complicationTemplate)
                    entries.append(timelineEntry)
                } else if complication.family == .extraLarge {
                    let complicationTemplate = CLKComplicationTemplateExtraLargeStackText()
                    complicationTemplate.line1TextProvider = CLKSimpleTextProvider(text: body1Text)
                    complicationTemplate.line1TextProvider.tintColor = TitanColors.red
                    complicationTemplate.line2TextProvider = CLKSimpleTextProvider(text: body2Text)

                    let timelineEntry = CLKComplicationTimelineEntry(date: date, complicationTemplate: complicationTemplate)
                    entries.append(timelineEntry)
                }
            }
        }
        handler(entries)
    }

    // MARK: - Placeholder Templates

    func getLocalizableSampleTemplate(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTemplate?) -> Void) {
        // This method will be called once per supported complication, and the results will be cached
        handler(nil)
    }

}
EN

回答 1

Stack Overflow用户

发布于 2019-02-07 00:02:38

安排一次backgroundRefresh,比如在未来一小时后做一次extendTimeline

要计划后台刷新,请在ExtensionDelegate的applicationDidFinishLaunching中运行此命令,并且不要忘记在每次刷新期间重新计划它。

let minutesToRefresh = 60 WKExtension.shared().scheduleBackgroundRefresh(withPreferredDate: Date().addingTimeInterval(minutesToRefresh * 60), userInfo: nil, scheduledCompletion: scheduledCompletion)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52107521

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档