斯坦福iOS7 2013-2014秋Assignment 6的一种答案 #1

这篇文章是针对斯坦福iOS7 2013-1014的公开课Assignment 6 Top Regions所进行的解答,由于这个作业设计的内容很多包括

1.CoreData

2.UIManagedDocument

3.Database Schema Design 

4.Multithreaded Application Development 

5.The Application Delegate method :application:didFinishLaunchingWithOptions: 

6.Background Fetching 

7.Background URL Sessions 

8.Listening to “radio stations” via NSNotificationCenter

并且难度很大,涉及的方面也很多,就拿出来和大家分享一下。当然这并不是唯一的答案,只是一些见解。这个作业的UI是基于Assignment 5(不需要其中的类和model),并且需要下载好FlickrFetcher的API,这些就不再说了,csdn上有人分享源码的。

拿到这个作业一个直观的感受可能就是如何设计它的数据结构,当然我先卖个关子,在后面的部分会提出一种思路,先一点点地完成。

1.Our application must work identically to last week except that where you are displaying the “top places” (as reported by Flickr), you are going to display the “top regions” in which photos have been taken. You will calculate the most popular regions from the data you gather periodically from the URLforRecentGeoreferencedPhotos

首先,从appDelegate开始

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self startFlickrFetch];
    return YES;
}

简单地建立一个foreground fetch

- (void)startFlickrFetch
{
    [FlickrHelper loadRecentPhotosOnCompletion:^(NSArray *photos, NSError *error) {
        if (error) {
            NSLog(@"Flickr background fetch failed: %@", error.localizedDescription);
        } else {
            NSLog(@"%d photos fetched", [photos count]);
        }
    }];
}

建立一个FlickrHelper的类,它从json中读取出photos,然后在completionHandler 上运行AppDelegate中的block。它的包含以下方法:

//  FlickrHelper.h
+ (void)loadRecentPhotosOnCompletion:(void (^)(NSArray *places, NSError *error))completionHandler;
 
//  FlickrHelper.m
#import "FlickrFetcher.h"
+ (void)loadRecentPhotosOnCompletion:(void (^)(NSArray *places, NSError *error))completionHandler
{
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration ephemeralSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
    NSURLSessionDownloadTask *task = [session downloadTaskWithURL:[FlickrFetcher URLforRecentGeoreferencedPhotos]
                                                completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
                                                    NSArray *photos;
                                                    if (!error) {
                                                        photos = [[NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfURL:location]
                                                                                                  options:0
                                                                                                    error:&error] valueForKeyPath:FLICKR_RESULTS_PHOTOS];
                                                    }
                                                    dispatch_async(dispatch_get_main_queue(), ^{
                                                        completionHandler(photos, error);
                                                    });
                                                }];
    [task resume];
}



好了,在output里面应该能“250 photos fetched”这条信息。





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值