Background Fetch ã試ãã¦ã¿ã
å æ¥ããM7 ã¨å°ãã ãæ¯ãã¦ã¿ããã¨ããã¨ã³ããªã§ã¢ã¼ã·ã§ã³ã¢ã¯ãã£ããã£ã試ãã«è§¦ã£ã¦ã¾ããããã®æµãã§èªåã®æ©æ°ãå®æçã« Tweet ããããã«ãã¦ã¿ãããããããããªãã¨æã£ã¦æè§ãªã®ã§ iOS 7 ããå°å ¥ããã Background Fetch ã使ã£ã¦è¦ããã¨æãããã®æã®å 容ãã¡ã¢ã¨ãã¦æ®ãã¦ã¿ããã¨æãã¾ãã
ããã«æ¸ãå®é¨å 容㯠Apple ã®å¯©æ»ãéã£ãå®ç¸¾ã®ãããã®ã§ã¯ãªãç¹ããäºæ¿ãã ããã
Background Fetch ã¨ã¯
- iOS 7 ãã追å ãããæ°ãã Background Mode ã®ã²ã¨ã¤
- OS å´ãä¸å®æï¼OS ã®å¤æã§é©åã ã¨æãããã¿ã¤ãã³ã°ï¼ã§
- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
ã¡ã½ããã Background ã§å¼ã³åºãã¦ããã - å®è¡ãããæçã¤ã³ã¿ã¼ãã«ã®æå®ã
setMinimumBackgroundFetchInterval:
ã¡ã½ããã§å¯è½ - OS ããã®å¼ã³åºãã¿ã¤ãã³ã°ã¯ã¢ããªã®å©ç¨ç¶æ³ï¼ãã¿ã¼ã³ï¼ãå¦ç¿ããä¸ã§æ±ºå®ããã
ã¨ãããã®ã®ããã§ãã
feedtailor ããã® "ããæ°æ¸©" ã§ã使ããã¦ãã¾ãã
Background Fetch ã§ã§ãããã¨
Apple ã®ç´¹ä»ãã¦ããä¾ã¨ãã¦ã¯ã
- SNS ç³»ã¢ããªã®ã¿ã¤ã ã©ã¤ã³ã®äºååå¾
- News ç³»ã¢ããªã®æ°çæ å ±äºååå¾
- 天æ°ç³»ã¢ããªã®æ å ±åå¾ï¼ãã®ãããã feedtailor ããã® "ããæ°æ¸©" ããã£ã¦ããã¨ããï¼
- åçãåç»ã®å ±æï¼å¥ã®æè¡ "Background Transfer" ã¨ã®ä½µç¨ï¼
ãããããã¦ãã¾ãã
å ·ä½çã«ã¯
- Network éä¿¡
- ã¢ããªã±ã¼ã·ã§ã³ããã¸ã®æ´æ°
- Local Notification ã®çºè¡
- ãã¼ã«ã«ãã¡ã¤ã«ã®çæï¼UserDafaults ã®æ¸ãæãããããã DB ç³»æä½ãï¼
ãªã©ãå¯è½ã§ãã
ã¨ãããã¨ã§ãåé ã§ãæ¸ããããã« Background Fetch ãçºç«ããã¿ã¤ãã³ã°ã§ç¾å¨ã®æ©æ°ç´¯è¨ã Background Fetch 㧠Tweet ãã¦ã¾ããã
å®è£
å¿ è¦ãªã®ä½æ¥ã¯ä»¥ä¸ã®éãã§ãã
- Bacground Modes ã "Fetch" ã¨ã㦠Info.plist ã«è¨è¿°
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
ã§setMinimumBackgroundFetchInterval:
ãè¨å®- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
ã®ä¸èº«ãå®è£
1. Bacground Modes ã "Fetch" ã¨ã㦠Info.plist ã«è¨è¿°
以ä¸ã®ã¹ã¯ãªã¼ã³ã·ã§ããã®ããã« Xcode 5 ä¸ã§è¨å®
2. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
㧠setMinimumBackgroundFetchInterval:
ãè¨å®
application:didFinishLaunchingWithOptions:
å
㧠setMinimumBackgroundFetchInterval:
ãå¼ã³ã¾ããããã§ã¯å®æ°ã® UIApplicationBackgroundFetchIntervalMinimum
ãã»ãããã¦ãã¾ãã
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum]; return YES; }
3. - (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
ã®ä¸èº«ãå®è£
- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { StepCountTweeter *tweeter = [[StepCountTweeter alloc] init]; [tweeter tweetStepCountWithUserName:nil tweetTextBlock:^NSString *(NSInteger numberOfSteps, NSDate *fromDate, NSDate *toDate, NSError *error) { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setLocale:[NSLocale systemLocale]]; [dateFormatter setDateFormat:@"MM/dd HH:mm:ss"]; NSString *text = [NSString stringWithFormat:@"%@ ã %@ ã®æéã« iPhone 5s ãæã£ã¦ %@ æ© æ©ãã¾ããã Tweet By Background Fetch" , [dateFormatter stringFromDate:fromDate] , [dateFormatter stringFromDate:toDate] , [@(numberOfSteps) stringValue]]; return text; } completionBlock:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { if (error) { /// 失ææã«ã¯ UIBackgroundFetchResultFailed ã渡ã㦠completionHandler ãå¼ã¶ completionHandler(UIBackgroundFetchResultFailed); } else { /// æåæã«ã¯ UIBackgroundFetchResultNewData ã渡ã㦠completionHandler ãå¼ã¶ completionHandler(UIBackgroundFetchResultNewData); } }]; }
- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
ã®ä¸ã§å
·ä½çã« Background Fetch ã¨ãã¦å®ç¾ããããã¨ãå®è£
ãã¾ããç´ 30 ç§ã®ç¶äºãä¸ãããã¾ãã
StepCountTweeter ã¯ã©ã¹ã¯ä»åæ©æ°ã Tweet ããããã«åãä½ã£ãã¯ã©ã¹ã§ SLRequest ã使ã£ã¦ Tweet ããã¯ã©ã¹ã§ããBackground Fetch ã§ã®ãã¤ã³ã㯠StepCountTweeter
ã¯ã©ã¹ã® tweetStepCountWithUserName:tweetTextBlock:completionBlock:
ã¡ã½ããã®ç¬¬3å¼æ°ã® completionBlock å
é¨ã§ - (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
ã® completionHandler ãå¼ãã§ããã¨ããã§ãããã® completionHandler ã¯å¿
ãå¼ã¶å¿
è¦ãããã¾ãã
Tips
Background Fetch ã®æå¹/ç¡å¹
ã¢ããªã«ãã£ã¦ã¯ãBackground Fetch èªä½ã¯æå¹ã«ããããã® ã¦ã¼ã¶ã¼è¨å®ã¨ã㦠Background Fetch ã§ã®åä½ã On/Off ããããã¨ããããã Background Fetch ã§å®è¡ãããå¦çã§ãã°ã¤ã³ãå¿
é ãªã©ã®å ´åããããã¨æãã¾ããã¢ããªå´ã®ç¶æ³ã§å®éã« Background Fetch ã®ç´°ãã On/Off ãå¿
è¦ãªå ´åã¯ä»¥ä¸ã®ããã« setMinimumBackgroundFetchInterval:
ã«æ¸¡ãå¤ã§æå¹ OR ç¡å¹ã®åãæ¿ããè¡ãã¾ãã
// ç¡å¹ã«ãã [application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalNever]; // æå¹ã«ãã [application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum]; // ãããã¯ä»»æã® NSTimeInterval
Background Fetch ã®ãããã°æ¹æ³
Background Fetch ã®ãããã°æ¹æ³ã¯ä¸»ã« 2 ã¤ããã¾ãã
1ã¤ç®ã¯ã¹ãã¼ã ãåãã¦ãã«ãã®ãªãã·ã§ã³ã§ã¢ããªçµäºç¶æ ããã® Background Fetch ã試ãæ¹æ³ã§ãã
2ã¤ç®ã¯ Xcode ã® Debug ã¡ãã¥ã¼ãã "Simulate Background Fetch" ãå®è¡ããæ¹æ³ã§ãããã®æ¹æ³ã®å ´åã«ã¯ã¢ããªã®ããã»ã¹ãçãã¦ããéã« Background Fetch ãå¼ã³åºãããã·ãã¥ã¨ã¼ã·ã§ã³ã試ããã¨ãã§ãã¾ãã
æéã®ãããéä¿¡å¦ç
30ç§ã®å¶éãããã¾ããã"Background Transfer" ãä½µç¨ãããã¨ã§æéã®ãããéä¿¡å¦çã§ãå®è£ å¯è½ã ã¨æãã¾ãã
å®é¨
Background Fetch 㯠OS ã«ãã£ã¦ã¹ã±ã¸ã¥ã¼ãªã³ã°ãããã¨ãããã¨ãªã®ã§ã©ã®ãããã®ã¹ãã³ã§å®è¡ãããã®ããæ°ã«ãªã£ãã®ã§ç°¡åã«èª¿ã¹ã¦ã¿ã¾ãããæ°ã«ãªã£ãã®ã¯
- completionHandler ã«æ¸¡ãçµæã«ãã£ã¦ Background Fetch ã®å®è¡ã«å½±é¿ãã§ãã®ãï¼
setMinimumBackgroundFetchInterval:
ã«æ¸¡ãã¤ã³ã¿ã¼ãã«ã®éãã§èµ·ãã Background Fetch ã®å®è¡ã¸ã®å½±é¿
ã§ãã
ããã§ä»¥ä¸ã®ããã«å¤ã調æ´ãã¤ã¤è©¦ãã¦ã¿ã¾ããã
- åå¾å¾ã®çµæå¤å®ã Fail ã«ãã¦ãã ã¤ã³ã¿ã¼ãã«ã¯ UIApplicationBackgroundFetchIntervalMinimum
- åå¾å¾ã®çµæå¤å®ã NewData ã«å¤æ´ãããã¤ã³ã¿ã¼ãã«ã¯ UIApplicationBackgroundFetchIntervalMinimum
- ã¤ã³ã¿ã¼ãã«ã 2æéã«åºãã
å®è·µãã¦ã¿ã¦ã®æ°ä»ã
åå¾çµæã UIBackgroundFetchResultFailed ã«ãã¦ããUIBackgroundFetchResultNewData ãã¦ãç¹ã« Background Fetch ã®å®è¡ã¹ãã³ãåºãã£ãã縮ã¾ã£ãããã¦ããããã«æããªãã£ãã
UIApplicationBackgroundFetchIntervalMinimum ãæå®ãã¦ããã¨
- æç 10 åç¨åº¦
- æé· 5 æé
ãããã®ã¤ã³ã¿ã¼ãã«ã§åãã¦ããæãã ã£ãã
ã¤ã³ã¿ã¼ãã«ã 2 æéã«åºããå ´åã«ã¯
- æç 2 æéåå¾ï¼ 2 æéåã£ã¦ããå ´åããã£ãï¼
- æé· 22.5 æé
ãããã®ã¤ã³ã¿ã¼ãã«ã§åãã¦ããæãã ã£ãã
ãªãã¨ãªããæ°ãã«ãã«ãï¼ã¤ã³ã¹ãã¼ã«ãè¡ã£ããã¨ã¯ Background Fetch ãå®è¡ãããã¾ã§æéããããããã«æãã
ææ³
Apple ã®è©±ã«ãã㨠Background Fetch ã¯ã¢ããªã®å©ç¨ç¶æ³ã«å¿ã㦠OS å´ã§å¼ã³åºãã¦ãããããä»å試ãã¦ã¿ããµã³ãã«ã¢ããªã®æ示çãªèµ·åé »åº¦çãå½±é¿ãã¦ãããããããªãã¨æãã¾ãããã¢ã¯ãã£ãã«ä½¿ããããããªã¢ããªã®å ´åã¯æå®ããã¤ã³ã¿ã¼ãã«ã«è¿ãé »åº¦ã§å®è¡ããããããããªãã§ãããã»ã¼èµ·åããªããããªã¢ããªã®å ´åã¯ç¸å½éã空ããææªã»ã¼å®è¡ãããªãã¨ããããã®ããç¥ããªãã¨ãæãã¾ããã
æ¸å¿µ
å®è¡ã¿ã¤ãã³ã°ãæ示çã«ã¯æå®ã§ããªããã®ã®ããç¨åº¦èªç±åº¦ãé«ããã¨ãã§ãããã§ããããæ ã«ããããã«éãéä¿¡å¦çã Wi-Fi 以å¤ã®éä¿¡æ段ã®æã«è¡ããã¦ãã¾ããã¨ããã°çãªæ å ±ãéä¿¡ãããããåºåç³»ã®ä½ããã®ãã¼ã¿å¦çãè¡ããããã§1ã¦ã¼ã¶ã¼ã¨ãã¦ã¯å«ã ãªãã¨æããã¨ãããã¾ããéçºè ã¯ãè¡åã®è¯ãã¦ã¼ã¶ã¼ã«åã°ãããããªå®è£ ãå¿ããã¦ä¾¿å©ã§æç¨ãªãã®ã«ãã¦ãããããã¨æãã¾ãã