Skip to content

Instantly share code, notes, and snippets.

@bertobettencourt
Created April 4, 2014 18:07
Show Gist options
  • Save bertobettencourt/9979990 to your computer and use it in GitHub Desktop.
Save bertobettencourt/9979990 to your computer and use it in GitHub Desktop.
Background Fetch Sample
// AppDelegate.m
#import "AppDelegate.h"
#import "LiveTableViewController.h" // Import the view controller you want to update in the background
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
return YES;
}
-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
UINavigationController *navigationController = (UINavigationController*)self.window.rootViewController;
id topViewController = navigationController.topViewController;
if ([topViewController isKindOfClass:[LiveTableViewController class]]) {
[(LiveTableViewController *)topViewController insertNewObjectForFetchWithCompletionHandler:completionHandler];
} else {
NSLog(@"Not the right class %@.", [topViewController class]);
completionHandler(UIBackgroundFetchResultFailed);
}
}
// LiveTableViewController.h
#import <UIKit/UIKit.h>
#import "LiveViewCell.h"
#import "XMLDictionary.h"
#import "LiveMatchObject.h"
#import "MatchDetailViewController.h"
@interface LiveTableViewController : UITableViewController
- (void)insertNewObjectForFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler; // create a method where you will call the methods you want to run in the background
// LiveTableViewController.m
- (void)insertNewObjectForFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
NSLog(@"Update the tableview.");
[self parseXMLLiveMatch]; // method that parses the XML data
completionHandler(UIBackgroundFetchResultNewData);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment