2016-06-25 1 views
0

J'ai une application de liste de tâches. à l'intérieur de ce todolist, j'ai des articles, chaque article ont une date et une fréquence. ex: objet Itemsws nsTimer pour mettre à jour les éléments de données de base

  • Nom: Sarah pick-up à l'aéroport
  • DueDate: 26/06/16 (26 juin)
  • Fréquence: hebdomadaire

Comment reprogrammer automatiquement article ? des applications en vie ou en arrière-plan, etc ... dans notre exemple: Après le 26/06/16, cet élément sera rééchelonner automatiquement 03/07/16 (03 juillet)

Dois-je utiliser NSTimer func appeler une fonction pour vérifier si un article doit être replanifié? :

Où dois-je créer/initialiser ce NSTimer (délégué dans l'application et comment?) Mode d'arrière-plan?

Beaucoup de questions de mon côté? Quelqu'un at-il un exemple de ce genre d'utilisation?

Ce n'est pas seulement un NSTimer dans un UIViewController.

grâce

Répondre

0

Si vous recherchez le Web pour nsnotification local scheduled, l'un des sites qui se pose est this, dans lequel il vous montre comment faire une grande partie de ce que vous décrivez. En particulier, son tutoriel comprend ce code:

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) 

Le concept clé que vous recherchez est d'utiliser un UILocalNotification et régler la fireDate sur ce point.

+0

merci tutoriel Feldur très utile – h3630fr