swift nsTimer 更新核心数据项

swift nsTimer to update core data items

我有一个待办事项列表应用程序。 在这个待办事项列表中,我有项目,每个项目都有截止日期和频率。 例如:项目对象

如何自动重新安排项目?应用程序活着,或在后台等...... 在我们的例子中: 26/06/16之后,此项目将自动改期至07/03/16(7月3日)

我应该使用 NStimer func 来调用一个函数来检查是否应该重新安排一个项目吗? :

我应该在哪里 create/initialize 这个 NSTimer(在应用程序委托中以及如何?) 后台模式?

我这边有很多问题?有没有人有这种用法的例子?

不仅仅是UIViewController中的NSTimer

谢谢

如果您在网上搜索 nsnotification local scheduled,出现的网站之一是 this,他在其中向您展示了如何执行您描述的大部分内容。特别是,他的教程包含以下代码:

private let ITEMS_KEY = "todoItems"

func addItem(item: TodoItem) { // persist a representation of this todo item in NSUserDefaults
    var todoDictionary = NSUserDefaults.standardUserDefaults().dictionaryForKey(ITEMS_KEY) ?? Dictionary() // if todoItems hasn't been set in user defaults, initialize todoDictionary to an empty dictionary using nil-coalescing operator (??)

    todoDictionary[item.UUID] = ["deadline": item.deadline, "title": item.title, "UUID": item.UUID] // store NSData representation of todo item in dictionary with UUID as key
    NSUserDefaults.standardUserDefaults().setObject(todoDictionary, forKey: ITEMS_KEY) // save/overwrite todo item list

    // create a corresponding local notification
    var notification = UILocalNotification()
    notification.alertBody = "Todo Item \"\(item.title)\" Is Overdue" // text that will be displayed in the notification
    notification.alertAction = "open" // text that is displayed after "slide to..." on the lock screen - defaults to "slide to view"
    notification.fireDate = item.deadline // todo item due date (when notification will be fired)
    notification.soundName = UILocalNotificationDefaultSoundName // play default sound
    notification.userInfo = ["UUID": item.UUID, ] // assign a unique identifier to the notification so that we can retrieve it later
    notification.category = "TODO_CATEGORY"
    UIApplication.sharedApplication().scheduleLocalNotification(notification)

您正在寻找的关键概念是使用 UILocalNotification 并在其上设置 fireDate。