I am in the midst of revising my app, and one of the things is that I can't get the most recent data when my app is backgrounded. Nor can I delegate my main app to do some call for me when I am using my Today extension.
So this seems to leave me with the option of having my extension reach out to the server and sort the data. And thus, I would have to do the same for my WatchKit app. Is this frowned upon, and if so, is there a better solution?
So in my Main View Controller I have this method:
- (void)loadTableData{
if ([self.numberField.text isEqualToString:@""]) {
tableViewController.busTimeArray = nil;
}
else{
if([self.busNumber.text isEqualToString:@""]){
TimeTableParser *timeParser = [[TimeTableParser alloc] initWithArray:tableViewController.busTimeArray];
tableViewController.busTimeArray = [timeParser parseXML:self.numberField.text];
}
else{
SortedTimeTableParser *timeParser = [[SortedTimeTableParser alloc] initWithArray:tableViewController.busTimeArray];
tableViewController.busTimeArray = [timeParser parseXMLForStop:self.numberField.text andBusNumber:self.busNumber.text];
}
}
//Shows the table
[_busTimeTable reloadData];
//Resign keyboard
[self.view endEditing:true];
[self passInformationToNCWidget];
//Make sure the refresh circle is dismissed
[_refreshController endRefreshing];
}
As you can see, I am passing the info to Today Extension. That is being done through NSUserDefaults
and is updated in the background whenever iOS decides it needs updating. On load, my extension reads the standardUserDefaults
and formats the data to be presented to the user. However, I have gotten feedback that due to the nature of the app (getting bus times), users expect more current data. Which makes sense. So I need a way to get the most recent data to the extension. Since extension can't directly call to apps controllers, my only option currently seems to be using the sorting methods I have (in a separate class) which do most of the heavy lifting.
Whether it's "frowned upon" is difficult to discern, but I have apps in the App Store currently with both Today extensions and WatchKit apps that make network calls.
That doesn't necessarily mean that it's the right thing to do, as every network call you make slows down your user's experience, but Apple seems to be okay with it in my experience. You may want to use a healthy amount of caching and include failsafes, but it's certainly okay with Apple, and it appears to work just fine.
If you're looking for an alternative, though, there's always NSUserDefaults
, which you can use to share small amounts of data between your WatchKit app/Today extension and your companion iOS app.
From the code you've provided, it appears that you're already using NSUserDefaults
, and your users' and your concerns with latency are valid – with something like bus times, the most recent data is needed whenever possible.
For an application of Today extensions like this, I think network calls are justifiable (with the proper fallbacks in place) because you need up to the minute information.
I'd also take a look at the Today extension guide under "Updating Content" for more details on how the extension manages new data to be displayed, as there are limitations.