斯坦福iOS7 2013-2014秋Assignment 6的一种答案 #3
来源:互联网 发布:淘宝网秋装新款 编辑:程序博客网 时间:2024/06/03 14:02
这篇文章是针对斯坦福iOS7 2013-1014的公开课Assignment 6 Top Regions所进行的解答的第三部分。
7.Fetch the URLforRecentGeoreferencedPhotos from Flickr periodically (a few times an hour when your application is in the foreground and whenever the system will allow when it is in the background using the background fetching API in iOS). You must load this data into a Core Data database with whatever schema you feel you need to do the rest of this assignment.
本节接着实现#2未完成的要求。
首先使用白胡子老师课上介绍的NSTimer来实现periodically fetch
- (void)startFlickrFetch:(NSTimer *)timer{ [self startFlickrFetch];}#define FOREGROUND_FLICKR_FETCH_INTERVAL (15 * 60) - (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ [self startFlickrFetch]; [NSTimer scheduledTimerWithTimeInterval:FOREGROUND_FLICKR_FETCH_INTERVAL target:self selector:@selector(startFlickrFetch:) userInfo:nil repeats:YES]; return YES;}
接着实现后台获取,也很简单,只需要使用一个appDelegate 方法,然后把completion handler传递给#2中的“handleEventsForBackgroundURLSession” appDelegate 方法就可以自动实现了。
- (void)application:(UIApplication *)applicationperformFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{ [FlickrHelper loadRecentPhotosOnCompletion:^(NSArray *photos, NSError *error) { if (error) { NSLog(@"Flickr background fetch failed: %@", error.localizedDescription); completionHandler(UIBackgroundFetchResultFailed); } else { NSLog(@"%d photos fetched", [photos count]); completionHandler(UIBackgroundFetchResultNewData); } }];}
然而,由于我们希望成为“good background citizens”,我们要在FlickrHelper中禁用蜂窝数据网络,并限制超时的时限,在config和session之间加入下面的代码。
#define BACKGROUND_FLICKR_FETCH_TIMEOUT 10+ (void)loadRecentPhotosOnCompletion:(void (^)(NSArray *places, NSError *error))completionHandler{ ... config.allowsCellularAccess = NO; config.timeoutIntervalForRequest = BACKGROUND_FLICKR_FETCH_TIMEOUT; ...}
但是我们希望让app尽可能经常地调用后台来fetch(不然有可能iOS设备可能永远不会调用。。。),在NSTimer之前接入下面的代码
- (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ ... [[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum]; ...}测试一下!
0 0
- 斯坦福iOS7 2013-2014秋Assignment 6的一种答案 #3
- 斯坦福iOS7 2013-2014秋Assignment 6的一种答案 #6
- 斯坦福iOS7 2013-2014秋Assignment 6的一种答案 #1
- 斯坦福iOS7 2013-2014秋Assignment 6的一种答案 #2
- 斯坦福iOS7 2013-2014秋Assignment 6的一种答案 #4
- 斯坦福iOS7 2013-2014秋Assignment 6的一种答案 #5
- 斯坦福iOS7 2013-2014秋Assignment 6的一种答案 #7
- 斯坦福iOS7 2013-2014秋Assignment 6的一种答案 #8
- 斯坦福iOS7 2013-2014秋Assignment 6的一种答案 #9
- 斯坦福iOS7 2013-2014秋Assignment 6的一种答案 #10
- coursera斯坦福Andrew Ng的机器学习编程作业答案
- 悲剧的斯坦福算法入门Assignment 1……fin.eof 函数详解
- 一种iOS7 设置隐藏状态栏的方法
- iOS7的一些总结:使用UIView的一种有效方法
- iOS7的一些总结:使用UIView的一种有效方法
- iOS7的一些总结:使用UIView的一种有效方法
- Assignment 6
- Assignment 3
- Java 中非公平锁与公平锁的区别
- 测试的总结
- C++Primer第五版 练习11.13(解答)
- 顺序表实现两个集合的合并
- Struts2使用过滤器验证登陆状态
- 斯坦福iOS7 2013-2014秋Assignment 6的一种答案 #3
- UIViewController的基本概念与生命周期
- iOS中需要注意的小细节
- R中的列表与数据框
- 关于ViewDragHelper
- CocoaPods 的又一个小坑 required by `Podfile`
- Tools ------ Download ------Installing the SDK
- 移动web如何显示出滚动条
- ArcGIS相关资源整理