The Analysis of ios video project mainly focuses on block details. If you understand the definition of block, but are not very familiar with the application, refer to this article, iosblock

Source: Internet
Author: User

The Analysis of ios video project mainly focuses on block details. If you understand the definition of block, but are not very familiar with the application, refer to this article, iosblock

Analysis of film and TV projects on github

This project can be downloaded from both 4app and github.

 

Project:

This is the just-run interface of the program.

The program should have a discover controller.

Find the discover controller and familiarize yourself with the principles of the project based on the project process to learn their architecture ideas.


1-(void) setupTableView
2 {
3 if (! _RefreshControl)
4 {
5 _refreshControl = [[UIRefreshControl alloc] initWithFrame: CGRectMake (0, -44, 320, 44)];
6 [self.refreshControl addTarget: self action: @selector (refreshFeed) forControlEvents: UIControlEventValueChanged];
7 [self.tableView addSubview: _refreshControl];
8     }
9 }
 

It can be seen here that it is a refresh space addition using lazy loading

-(void) refreshFeed
{
    [self requestMovies];
}
He calls requestMovies when the viewdidload and refresh controls change

Find this method

-(void) requestMovies
{
    KMDiscoverListCompletionBlock completionBlock = ^ (NSArray * data, NSString * errorString)
    {
        [self.refreshControl endRefreshing];
        if (data! = nil)
            [self processData: data];
        else
            [self.networkLoadingViewController showErrorView];
    };
    KMDiscoverSource * source = [KMDiscoverSource discoverSource];
    [source getDiscoverList: @ "1" completion: completionBlock];
}
After reading it for a long time, I realized that the previous block is used for the subsequent calls.

You can also write it like this

 KMDiscoverSource * source = [KMDiscoverSource discoverSource];
    [source getDiscoverList: @ "1" completion: ^ (NSArray * data, NSString * errorString) {
        [self.refreshControl endRefreshing];
        if (data! = nil)
            [self processData: data];
        else
            [self.networkLoadingViewController showErrorView];
    }
    
     ];
    
 

What it means

Here he encapsulates the method of downloading data into a block. This is the method I mainly want to look at. I am not always familiar with blocks.

Click in to see

#import "KMBaseSource.h"

typedef void (^ KMDiscoverListCompletionBlock) (NSArray * data, NSString * errorString);

@interface KMDiscoverSource: KMBaseSource

+ (KMDiscoverSource *) discoverSource;

-(void) getDiscoverList: (NSString *) pageLimit completion: (KMDiscoverListCompletionBlock) completionBlock;

@end
This should be a tool class used to return discover data

 1-(void) getDiscoverList: (NSString *) pageLimit completion: (KMDiscoverListCompletionBlock) completionBlock;
 2 {
 3 if (completionBlock)
 4 {
 5 NSDictionary * parameters = [[NSDictionary alloc] initWithObjects: [NSArray arrayWithObjects: pageLimit, nil] forKeys: [NSArray arrayWithObjects: @ "page", nil]];
 6
 7 [UIApplication sharedApplication] .networkActivityIndicatorVisible = YES;
 8         
 9 AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
10 [manager.requestSerializer setValue: @ "application / json" forHTTPHeaderField: @ "Accept"];
11
12 [manager GET: [self prepareUrl] parameters: parameters success: ^ (AFHTTPRequestOperation * operation, id responseObject)
13 {
14 NSLog (@ "JSON:% @", responseObject);
15 NSDictionary * infosDictionary = [self dictionaryFromResponseData: operation.responseData jsonPatternFile: @ "KMDiscoverSourceJsonPattern.json"];
16 dispatch_async (dispatch_get_main_queue (), ^ {
17 [UIApplication sharedApplication] .networkActivityIndicatorVisible = NO;
18 completionBlock ([self processResponseObject: infosDictionary], nil);
19});
20}
21 failure: ^ (AFHTTPRequestOperation * operation, NSError * error)
twenty two          {
23 NSLog (@ "Error:% @", error);
24 dispatch_async (dispatch_get_main_queue (), ^ {
25 [UIApplication sharedApplication] .networkActivityIndicatorVisible = NO;
26 NSString * errorString = error.localizedDescription;
27 if ([errorString length] == 0)
28 errorString = nil;
29 completionBlock (nil, errorString);
30});
31}];
32}
33}
A third-party framework AFN is used here

The first sentence is to pass in a dictionary parameter

Line 7 shows the network transmission icon in the status bar

Line 12 Send a get request

Line 15 is the encapsulation of the json analysis. Because the AFN is executed asynchronously, you need to get the main thread to update the ui. Give the data in the block's parameters to the block, and then call the block when other controllers will have parameters. You can see this method below for your own operations, and you can also monitor this data download.

Because the ner passed in the parameter of err is judged when running.

-(NSArray *) processResponseObject: (NSDictionary *) data
{
    if (data == nil)
        return nil;
    NSArray * itemsList = [NSArray arrayWithArray: [data objectForKey: @ "results"]];
    NSMutableArray * sortedArray = [[NSMutableArray alloc] init];
    for (NSDictionary * item in itemsList)
    {
        KMMovie * movie = [[KMMovie alloc] initWithDictionary: item];
        [sortedArray addObject: movie];
    }
    return sortedArray;
}
You can clearly see that this function has sent a dictionary

                                                                                  ------------------- To be continued

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.