My Today Extensions works perfectly on beta 4, and I have implement a simple cache when Today Extensions first loaded.
let defaults = NSUserDefaults(suiteName: "group.ReadWidget") // app group
var feed = [String: String]()
override func viewDidLoad() {
getCache()
// The feed is empty
}
func parseRSS() {
...
// Parse in background
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
self.parser = NSXMLParser(contentsOfURL:NSURL(string:url))
self.parser.delegate = self
self.parser.parse() // populate the feed
self.saveCache() // Save the feed
...
})
})
}
func saveCache() {
defaults.setObject(feed, forKey:"feed")
defaults.synchronize()
}
func getCache() {
if defaults.objectForKey("feed") {
feed = defaults.dictionaryForKey("feed") as [String : String]
}
else {
feed = [:]
}
}
In beta 5, after saveCache()
, and when getCache()
back in Today Extension reloaded, I always got back an empty dictionary.
I am using NSUserDefaults(suiteName:...)
to store cache. I have also tried NSUserDefaults.standardUserDefaults()
and the result is the same.
btw, which NSUserDefaults
is the preferred method for loading and saving cache? The cache is not shared with the main app.