SDWebImage 读码笔记

Watch Star Fork Open Issues Closed Issue
823 15711 4432 133 1021

从 Star 和 Issue 维度来看SDWebImage可谓是万众瞩目和久经考验。

SDWebImage-README

How is SDWebImage better than X?

下面,我们来细致的阅读一下 SDWebImage(简称 SD)的源码。

一些良好的编码习惯

  • SD的扩展命名风格不错。 带有sd_ 下划线前缀以此来区分是否为 三方库扩展。避免 扩展方法重名 覆盖问题。

    1. 同样的命名风格适用于 类名。前缀可以避免OC语言没有命名空间的尴尬。
    2. 不过需要注意的是,Cocoa应用程序Apple宣称拥有”两字母前缀”的权利。 所以有可能在不远的将来,SD要面临 命名冲突的问题。
  • 对于SD废弃的方法。 SD的做法是 将方法移至 对应的Deprecated中。 并在方法申明的时候给出提示。这种做法,笔者也比较认同。 SDK对外的接口变更是不可避免的。在版本更新中直接删除的话,会让依赖方有改造成本。对废弃方法进行警告声明,实现上调用新方法是比较优雅和无害的。

1
2
3
@interface UIImageView (WebCacheDeprecated)
- (NSURL *)imageURL __deprecated_msg("Use `sd_imageURL`");
@end
  • 使用类型常量。 Effective Objective-C 2.0 中的第四条实践。

    SDWebImageCompat.h

1
extern NSString *const SDWebImageErrorDomain;

SDWebImageCompat.m

1
NSString *const SDWebImageErrorDomain = @"SDWebImageErrorDomain";
  • 使用inline 内联函数关键词。 inline这个关键词会建议编译器内联展开处理。inline这个关键词在C++上出场频率比较高。 关于inline关键词的QA 具体的性能影响可以看[9.3]。 笔者认为 inline仅适用于调用频繁的短函数

    1
    2
    3
    4
    5
    6
    7
     inline UIImage *SDScaledImageForKey(NSString *key, UIImage *image) {
    ...
    }

    FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) {
    return image.size.height * image.size.width * image.scale * image.scale;
    }

关于inline的扩展阅读

  • 使用 dispatch_barrier_sync 来保证一些多线程操作的原子性。 笔者也做过一些 iOS 锁的branchMarking

关于SDWebImage图片下载实现的细节

  • 如何识别 图片格式? 是jpg,png还是gif呢? SD的 NSData扩展方法可以告诉你。根据图片的具体数据内容,可以看出对应的格式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@implementation NSData (ImageContentType)

+ (NSString *)sd_contentTypeForImageData:(NSData *)data {
uint8_t c;
[data getBytes:&c length:1];
switch (c) {
case 0xFF:
return @"image/jpeg";
case 0x89:
return @"image/png";
case 0x47:
return @"image/gif";
case 0x49:
case 0x4D:
return @"image/tiff";
case 0x52:
// R as RIFF for WEBP
if ([data length] < 12) {
return nil;
}

NSString *testString = [[NSString alloc] initWithData:[data subdataWithRange:NSMakeRange(0, 12)] encoding:NSASCIIStringEncoding];
if ([testString hasPrefix:@"RIFF"] && [testString hasSuffix:@"WEBP"]) {
return @"image/webp";
}

return nil;
}
return nil;
}

@end
  • SD 缓存中的图片是 原始的,未解压的。

On the other side, SDWebImage caches the UIImage representation in memory and store the original compressed (but decoded) image file on disk. UIImage are stored as-is in memory using NSCache, so no copy is involved, and memory is freed as soon as your app or the system needs it.

Additionally, image decompression that normally happens in the main thread the first time you use UIImage in an UIImageView is forced in a background thread by SDWebImageDecoder.

关于 图片的解压,可以查看SDWebImageDecoder的实现。 SD有对应shouldDecompressImages功能开关。默认是开启的。

What does SDWebImageDecoder do? #1173

流程分析

step. 3

因为,SD实现了自己的图片缓存机制。所以,在下载流程中,SD会先检查一遍是否命中缓存。
SD的图片缓存key 为url.absoluteString,SD还有一个缓存筛选机制,用来支持删除 图片url动态变更的部分。

1
2
SDWebImageManager.h
@property (nonatomic, copy) SDWebImageCacheKeyFilterBlock cacheKeyFilter;

这个机制会作用于所有cache key

1
2
3
4
5
6
7
8
9
10
11
- (NSString *)cacheKeyForURL:(NSURL *)url {
if (!url) {
return @"";
}

if (self.cacheKeyFilter) {
return self.cacheKeyFilter(url);
} else {
return [url absoluteString];
}
}

Cache实现部分参见SDImageCache.h

  • 内存缓存部分,SD是使用NSCahce
  • 磁盘缓存部分,SD是读写文件操作

    缓存部分,并无新意。IO注意异步线程操作即可。SD监听了应用 DidReceiveMemoryWarningWillTerminateDidEnterBackground等消息来处理自己的缓存,让缓存存储较为得体。应用内部缓存大小不影响宿主环境。

    - (void)storeImage:(UIImage *)image recalculateFromImage:(BOOL)recalculate imageData:(NSData *)imageData forKey:(NSString *)key toDisk:(BOOL)toDisk 中有一段代码挺有意思的。 SD的磁盘缓存是写入imageData,一般下载结束之后,SD是能拿到imageimageData的。 但如果只有image呢? 磁盘缓存该如何写入?

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
     
    if (image && (recalculate || !data)) {
    #if TARGET_OS_IPHONE
    // We need to determine if the image is a PNG or a JPEG
    // PNGs are easier to detect because they have a unique signature (http://www.w3.org/TR/PNG-Structure.html)
    // The first eight bytes of a PNG file always contain the following (decimal) values:
    // 137 80 78 71 13 10 26 10

    // If the imageData is nil (i.e. if trying to save a UIImage directly or the image was transformed on download)
    // and the image has an alpha channel, we will consider it PNG to avoid losing the transparency
    int alphaInfo = CGImageGetAlphaInfo(image.CGImage);
    BOOL hasAlpha = !(alphaInfo == kCGImageAlphaNone ||
    alphaInfo == kCGImageAlphaNoneSkipFirst ||
    alphaInfo == kCGImageAlphaNoneSkipLast);
    BOOL imageIsPng = hasAlpha;

    // But if we have an image data, we will look at the preffix
    if ([imageData length] >= [kPNGSignatureData length]) {
    imageIsPng = ImageDataHasPNGPreffix(imageData);
    }

    if (imageIsPng) {
    data = UIImagePNGRepresentation(image);
    }
    else {
    data = UIImageJPEGRepresentation(image, (CGFloat)1.0);
    }
    #else
    data = [NSBitmapImageRep representationOfImageRepsInArray:image.representations usingType: NSJPEGFileType properties:nil];
    #endif
    }

通过 是否有alpha通道 和 ImageDataHasPNGPreffix 来判断image类型,进而使用API进行UIimage to NSData

step. 4

SD的下载核心逻辑是在SDWebImageDownloaderDownloader维护着一个线程池,控制SDWebImageDownloaderOperation最小单元下载任务。

  • 默认并发下载量为6。可以通过maxConcurrentDownloads修改。
  • 下载超时时限默认为15.0。 可以通过downloadTimeout修改。
  • 下载任务执行顺序策略有 SDWebImageDownloaderFIFOExecutionOrder (队列等待,默认方式)SDWebImageDownloaderLIFOExecutionOrder (栈等待)

Downloader实现NSURLSessionDataDelegate协议,并将回调派发给具体的DownloaderOperation来响应下载各个阶段的数据回调。

DownloaderOperation继承自NSOperation并实现SDWebImageOperation。都是为了保证下载任务能被cancelcancel对应一个任务而言是很重要的。因为继承自NSOperation,所以SD在cancel部分的实现也很简单。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
- (void)cancel {
@synchronized (self) {
if (self.thread) {
[self performSelector:@selector(cancelInternalAndStop) onThread:self.thread withObject:nil waitUntilDone:NO];
}
else {
[self cancelInternal];
}
}
}

- (void)cancelInternalAndStop {
if (self.isFinished) return;
[self cancelInternal];
}

- (void)cancelInternal {
if (self.isFinished) return;
[super cancel];
if (self.cancelBlock) self.cancelBlock();

if (self.dataTask) {
[self.dataTask cancel];
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:self];
});

// As we cancelled the connection, its callback won't be called and thus won't
// maintain the isFinished and isExecuting flags.
if (self.isExecuting) self.executing = NO;
if (!self.isFinished) self.finished = YES;
}

[self reset];
}

保证cancel操作的原子性,调用[super cancel] 和 将内置的[self.dataTask cancel]。重置一些状态位即可。

SD的下载逻辑实现还是写的比较精彩的。清晰和严谨,不愧是“久经考验”

其他

SD还有SDWebImagePrefetcher模块。顾名思义,Prefetcher就是预下载逻辑。提前拉取图片资源,等到用的时候直接取用本地资源。本地IO速度快于网络IO,是一种优化思路。

查看SD源码时,一些查阅的链接。 有些启发,在这里记录一下。

Image I/O Programming Guide

Understanding SDWebImage - Decompression

Resizing High Resolution Images on iOS Without Memory Issues

iOS 包瘦身浅析

为什么要做包瘦身?

Your app’s total uncompressed size must be less than 4 billion bytes. Each Mach-O executable file (for example, app_name.app/app_name) must not exceed these limits:

For apps whose MinimumOSVersion is less than 7.0: maximum of 80 MB for the total of all __TEXT sections in the binary.

For apps whose MinimumOSVersion is 7.x through 8.x: maximum of 60 MB per slice for the __TEXT section of each architecture slice in the binary.

For apps whose MinimumOSVersion is 9.0 or greater: maximum of 400 MB for the size of the Mach-O binary file.

Apple 对上架App 是有包大小限制的。超过这个限制,是无法提交审核的。

However, consider download times when determining your app’s size. Minimize the file’s size as much as possible, keeping in mind that there is a 100 MB limit for over-the-air downloads.

尽可能控制包大小, 超过100MB的App 是不会被自动更新到用户手机上

如何分析可执行文件的大小

AppStore 上传的包大小 = 资源文件大小 + 可执行文件大小

资源文件大小 显而易见。 但可执行文件由 具体的(.h/.m/.swift)文件编译链接而成。这个光是看代码文件大小是衡量不出来的。

这个时候就要介绍一下LinkMap了。 Xcode的LinkMap文件。LinkMap文件是Xcode产生可执行文件的同时生成的链接信息,用来描述可执行文件的构造成分,包括代码段__TEXT和数据段__DATA的分布情况。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Sections:
# Address Size Segment Section
0x100002680 0x02DAED20 __TEXT __text
0x102DB13A0 0x00002FAC __TEXT __stubs
0x102DB434C 0x00004DC2 __TEXT __stub_helper
0x102DB9110 0x00172C10 __TEXT __gcc_except_tab
0x102F2BD20 0x001DC91A __TEXT __objc_methname
0x10310863A 0x00039CC2 __TEXT __objc_classname
0x1031422FC 0x00057D8E __TEXT __objc_methtype
0x10319A090 0x00229C39 __TEXT __cstring
0x1033C3CD0 0x00025194 __TEXT __ustring
0x1033E8E80 0x000CC108 __TEXT __const
0x1034B4F88 0x000002C0 __TEXT __swift2_proto
0x1034B5248 0x000003E6 __TEXT __entitlements
0x1034B5630 0x00099AC8 __TEXT __unwind_info
0x10354F0F8 0x000AAED0 __TEXT __eh_frame
0x1035FA000 0x00000010 __DATA __nl_symbol_ptr
0x1035FA010 0x00002558 __DATA __got
0x1035FC568 0x00003F90 __DATA __la_symbol_ptr
0x1036004F8 0x000002E0 __DATA __mod_init_func
0x1036007E0 0x00103990 __DATA __const
0x103704170 0x00123460 __DATA __cfstring
0x1038275D0 0x00012130 __DATA __objc_classlist
0x103839700 0x00000328 __DATA __objc_nlclslist
0x103839A28 0x000016A8 __DATA __objc_catlist
0x10383B0D0 0x00000128 __DATA __objc_nlcatlist
0x10383B1F8 0x00002B68 __DATA __objc_protolist
0x10383DD60 0x00000008 __DATA __objc_imageinfo
0x10383DD68 0x009AECA8 __DATA __objc_const
0x1041ECA10 0x00074B98 __DATA __objc_selrefs
0x1042615A8 0x00000A60 __DATA __objc_protorefs
0x104262008 0x0000EFE8 __DATA __objc_classrefs
0x104270FF0 0x0000B490 __DATA __objc_superrefs
0x10427C480 0x00050BD0 __DATA __objc_ivar
0x1042CD050 0x000B6010 __DATA __objc_data
0x104383060 0x000C0CA0 __DATA __data
0x104443D00 0x00143220 __DATA __common
0x104586F40 0x0013DAC0 __DATA __bss

可执行文件大小 = 末位地址(0x104586F40) + size(0x0013DAC0) - 起始地址(0x100002680)。 注意是16进制

详细的 LinkMap 是 这么几部分组成:

  1. Object files 所编译项目中的所有.obj文件及文件编号

  2. Sections 可执行文件的段表,描述各个段在可执行文件中的偏移位置和大小

  3. Symbols 详细描述各个.obj文件在段表中的分布情况。 可以计算出每个obj文件的占用大小,进而算出每个静态库、功能模块代码占用大小。

官方 Q&A

如何将App装载到模拟器

笔者这里使用 nice 这款App 演示。

step 1

首先使用Mac iTunes下载 对应App的ipa包,再解压,在Payload目录下 我们发现nice这款app的真身: KKShopping.app(难道nice也有一颗电商的心吗?)

使用 xcrun simctl list 查看现有的设备列表。

xcrun-simctl-list

step 2

创建新的模拟器,笔者这里创建iPhone 6搭载iOS9.3系统. 命名为MyiPhone6

1
xcrun simctl create "MyiPhone6" "iPhone 6" "com.apple.CoreSimulator.SimRuntime.iOS-9-3"

simctl-create

step 3

唤起MyiPhone6, 使用Xcode Simulator.app 唤起,参数CurrentDeviceUDID选择我们在xcrun simctl list Devices下看到的MyiPhone6的udid。 注意 一定要唤起,模拟器在shutdown state下是装载不了App的。

1
open -n /Applications/Xcode.app/Contents/Developer/Applications/Simulator.app --args -CurrentDeviceUDID '2C630172-AF2B-4798-B854-3FEE5F221411'

xcrun-simctl-list1

step 4

装载 KKShopping.app

1
xcrun simctl install MyiPhone6 ~/Music/iTunes/iTunes\ Media/Mobile\ Applications/nice\ 4.0.0/Payload/KKShopping.app

install-nice-app

如果,你按照步骤做完之后。 点击模拟器中的nice图标,发现它闪退了。

这里有一个原因是 我们通过iTunes下载下来的包要求运行环境是手机。 而手机和电脑模拟器的CPU架构是不一致的,所以运行不了很正常。

如果,你有源码编译出的Release-iphonesimulator目录下的app文件。 可以试一下下面的命令,在模拟器中唤起你的app

step 5

唤起模拟器中的App

1
xcrun simctl launch booted your.app.bundle_Identifier

唤起时,可以额外设置参数进去
xcrun simctl launch booted your.app.bundle_Identifier -port 7001

对应iOS中的读取逻辑代码为:

1
2
3
4
5
6
7
8
[[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
if([key isEqualToString:@"port"]){
port = obj;
*stop = YES;
}
}];

// port = 7001

Word Search II

Word Search

Given a 2D board and a list of words from the dictionary, find all words in the board.

Each word must be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.

1
2
3
4
5
6
7
8
9
10
For example,
Given words = ["oath","pea","eat","rain"] and board =

[
['o','a','a','n'],
['e','t','a','e'],
['i','h','k','r'],
['i','f','l','v']
]
Return ["eat","oath"].

题目大意:给定一个二维面板和一组字符串,找出二维面板上存在的字符串。
必须使用面板上的字母且水平连续或垂直连续拼接单词。

思路1: 深度优先搜索(DFS) Time Limit Exceeded

先遍历每个单词,如果单词首字母面板上有存在则开始深度优先搜索单词剩余的字母。

来分析一下超时的Test Case:

1
2
3
4
["aaaa","aaaa","aaaa","aaaa","bcde","fghi","jklm","nopq","rstu","vwxy","zzzz"]


["aaaaaaaaaaaaaaaa","aaaaaaaaaaaaaaab","aaaaaaaaaaaaaaac","aaaaaaaaaaaaaaad","aaaaaaaaaaaaaaae","aaaaaaaaaaaaaaaf","aaaaaaaaaaaaaaag","aaaaaaaaaaaaaaah","aaaaaaaaaaaaaaai","aaaaaaaaaaaaaaaj","aaaaaaaaaaaaaaak","aaaaaaaaaaaaaaal","aaaaaaaaaaaaaaam","aaaaaaaaaaaaaaan","aaaaaaaaaaaaaaao","aaaaaaaaaaaaaaap","aaaaaaaaaaaaaaaq","aaaaaaaaaaaaaaar","aaaaaaaaaaaaaaas","aaaaaaaaaaaaaaat","aaaaaaaaaaaaaaau","aaaaaaaaaaaaaaav","aaaaaaaaaaaaaaaw","aaaaaaaaaaaaaaax","aaaaaaaaaaaaaaay","aaaaaaaaaaaaaaaz","aaaaaaaaaaaaaaaa","aaaaaaaaaaaaaaab","aaaaaaaaaaaaaaac","aaaaaaaaaaaaaaad","aaaaaaaaaaaaaaae","aaaaaaaaaaaaaaaf","aaaaaaaaaaaaaaag","aaaaaaaaaaaaaaah","aaaaaaaaaaaaaaai","aaaaaaaaaaaaaaaj","aaaaaaaaaaaaaaak","aaaaaaaaaaaaaaal","aaaaaaaaaaaaaaam","aaaaaaaaaaaaaaan","aaaaaaaaaaaaaaao","aaaaaaaaaaaaaaap","aaaaaaaaaaaaaaaq","aaaaaaaaaaaaaaar","aaaaaaaaaaaaaaas","aaaaaaaaaaaaaaat","aaaaaaaaaaaaaaau","aaaaaaaaaaaaaaav","aaaaaaaaaaaaaaaw","aaaaaaaaaaaaaaax","aaaaaaaaaaaaaaay","aaaaaaaaaaaaaaaz","aaaaaaaaaaaaaaba","aaaaaaaaaaaaaabb","aaaaaaaaaaaaaabc"]

发现这个Test Case很有意思,有大量重复相同的前缀aaaaaaaaaaaaaaaa。使用DFS遍历图的话,如果没有高效的剪枝策略。光是这个前缀的深度优先搜索效率就很低下了。

思路2 : 字典树(Trie) + 深度优先搜索(DFS) Accepted

建立Trie字典树,能有效的避免大量重复前缀的搜索。

LeetCodeOJ 上有关于Trie建树的题目

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144

const int TrieChildNodeMax(26);
class Trie{
public:
class TrieNode{
public:
TrieNode(){
isWord = false;
word = "";
memset(childNode, NULL, sizeof(TrieNode *) * TrieChildNodeMax);
}
TrieNode *childNode[TrieChildNodeMax];
string word;
bool isWord;
};
TrieNode *root;

Trie(){
root = new TrieNode();
}


void insert(const string word,TrieNode* node,int idx = 0){
if(idx == word.length()) return ;
int k = word[idx] - 'a';
if(node->childNode[k] == NULL){
node->childNode[k] = new TrieNode();
}
if(idx == word.length() - 1){
node->childNode[k]->isWord = true;
node->childNode[k]->word = word;
}
else{
insert(word, node->childNode[k],idx+1);
}
}

};

class Solution {
public:
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
vector<string> ans;


for_each(words.begin(), words.end(), [&](const string word){
trie.insert(word, trie.root);
});


n = (int)board.size();
m = 0;
if(n > 0) m = (int)board[0].size();

for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
memset(vis, false, sizeof(bool)*1024*1024);
if(boardHasWord(board,i,j,trie.root,ans)){
cout<<"Yes,find word in borad"<<endl;
}
}
}

sort(ans.begin(), ans.end());

return ans;
}
private:
bool judge(const int x,const int y){
return (x>-1&&x<n&&y>-1&&y<m&&!vis[x][y]);
}
bool boardHasWord(const vector<vector<char>> board,int x,int y,Trie::TrieNode* root,vector<string>& ans){
int k = board[x][y] - 'a';
vis[x][y] = true;
if(root->childNode[k]){
if(root->childNode[k]->isWord){
root->childNode[k]->isWord = false;
ans.push_back(root->childNode[k]->word);
}


for(int i=0;i<4;i++){
int xx = x + dir[i][0];
int yy = y + dir[i][1];

if(judge(xx, yy)){
vis[xx][yy] = true;
if(boardHasWord(board, xx, yy, root->childNode[k], ans)) return true;
vis[xx][yy] = false;
}
}
}

return false;
}
int n,m;
const int dir[4][2] = { {1,0},{0,1},{-1,0},{0,-1} };
bool vis[1024][1024];
Trie trie;
};



int main(){

freopen(INPUT,"r",stdin);

string buf;
vector<vector<char>> board;
while(cin>>buf){
vector<char> chars;
chars.assign(buf.begin(), buf.end());
board.push_back(chars);
}

Solution solve;
vector<string> words({"aaaaaaaaaaaaaaaa","aaaaaaaaaaaaaaab","aaaaaaaaaaaaaaac","aaaaaaaaaaaaaaad"});
vector<string> ans = solve.findWords(board,words);


for_each(ans.begin(), ans.end(), [](const string str){
cout<<str<<endl;
});


return 0;
}



/* Wrong Answer */
/*
["ab","aa"]
["aba","baa","bab","aaab","aaa","aaaa","aaba"]
*/



/* Time Limit Exceeded */
/*

["aaaa","aaaa","aaaa","aaaa","bcde","fghi","jklm","nopq","rstu","vwxy","zzzz"]
["aaaaaaaaaaaaaaaa","aaaaaaaaaaaaaaab","aaaaaaaaaaaaaaac","aaaaaaaaaaaaaaad"]
*/

Accepted

House Robber

House Robber

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.

题目大意:你是一个职业强盗,计划去洗劫一条街。这条街上每个店铺都有一定量的现金。但是如果两家相邻的店铺被同时打劫的话会惊动警察。给定一个非负数数组表示各个店铺的现金数,请制定一个合理的打劫顺序使今晚的洗劫行动收获最大。

思考:

  1. 贪心策略:只洗劫奇数店铺 或 偶数店铺。 但细想一下,该策略只适合店铺数少于4的情况。比如有4家店铺的话: Plans = {[1,3],[2,4],[1,4]};贪心策略的一个反例。
  2. 暴力搜索:深度搜索,枚举每个店铺的两种状态: 抢 或 不抢。抢依赖上一家不抢。类似01背包,背包没有容量限制。可能会超时
  3. 在想 思路2的时候,其实已经想到这是一个 动态规划的题了:)动规的时间复杂度比搜索要来的线性,稳定。动态方程式是 dp[i] = max{dp[i-2]+nums[i],dp[i-1]} 2<=i<numsSize

动态规划 Accepted

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
int max(const int a,const int b){
return a > b ? a : b;
}

int easyRob(int* nums, int numsSize){
int odd = 0, eve = 0;

for(int i=0;i<numsSize;i++){
if(i%2) eve += nums[i];
else odd += nums[i];
}

return max(odd,eve);
}

int rob(int* nums, int numsSize) {
if(numsSize <= 3) return easyRob(nums,numsSize);

int *dp = (int *)malloc(sizeof(int)*(numsSize));
memset(dp, 0, sizeof(int)*(numsSize));
dp[0] = nums[0];
dp[1] = max(nums[1],nums[0]);
dp[2] = max(dp[0]+nums[2],dp[1]);

for(int i=3;i<numsSize;i++){
dp[i] = max(dp[i-2]+nums[i],dp[i-1]);
}

int ans = dp[numsSize-1];
free(dp);
return ans;
}

Minimum Size Subarray Sum

Minimum Size Subarray Sum

Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn’t one, return 0 instead.

For example, given the array [2,3,1,2,4,3] and s = 7,
the subarray [4,3] has the minimal length under the problem constraint.

题目大意: 给定一个正数数组 nums和一个正数 s,找出最短长度的连续子数组 使其和 大于等于 s。如果没有的话,返回 0.

Solution

暴力枚举 Accepted

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
int minSubArrayLen(int s, vector<int>& nums) {
size_t n = nums.size();
int sum = 0;
int ans = INT_MAX;
for(int i=0;i<n;i++){
sum = nums[i];
if(sum >= s) return 1;
for(int j=i+1;j<n;j++){
sum += nums[j];
if(sum >= s){
ans = min(ans,j-i+1);
break;
}
}
}

return ans==INT_MAX ? 0 : ans;
}
};

暴力枚举 + flag优化 Accepted

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution {
public:
int minSubArrayLen(int s, vector<int>& nums) {
size_t n = nums.size();
int sum = 0;
int ans = INT_MAX;

bool flag;
for(int i=0;i<n;i++){
sum = nums[i];
if(sum >= s) return 1;
flag = false;
for(int j=i+1;j<n;j++){
sum += nums[j];
if(sum >= s){
ans = min(ans,j-i+1);
flag = true;
break;
}
}

if(!flag) break;
}

return ans==INT_MAX ? 0 : ans;
}
};

枚举小窗口 Accepted

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
int minSubArrayLen(int s, vector<int>& nums) {
size_t n = nums.size();
int sum = 0;
int ans = INT_MAX;
int p = 0,q = 0;

while(q < n){
sum += nums[q++];

while(sum >= s){
ans = min(ans,q-p);
sum -= nums[p++];
}

}

return ans==INT_MAX ? 0 : ans;
}
};

Cocoa Auto Update

Cocoa auto update

我们在进行Mac 桌面应用开发的时候,需要维护应用的更新。我们希望说每一次的新版本发布,用户都能够自动更新下载版本并且替换掉老版本。

首先需要一个文件服务器来存放 对外发布的软件压缩包。这里还维护一份releaseInfo.json对外描述已有的版本信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"app": "MyApp",
"version": "1.0.0",
"channels": ["release"],
"entries": [
{
"os": "osx",
"architectures": ["x86-64"],
"osversion": " >= 10.6 ",
"appversion": "1.0.0",
"path": "MyApp-1.0.0-osx.tar.gz",
"format": "gz"
}
]
}

客户端定时check版本信息,发现服务端有版本号大于自身版本号的时候 触发更新下载逻辑。 这里使用NSURLSession即可,下载过程不在这里描述了。

当我们自行下载完安装包之后,如何帮助用户自动安装呢?

我们下载完成之后,可用使用NSTask来调用一个 shell 脚本,让它来帮助我们结束老版本的进程,同时将下载好的新版本解压、拷贝内容到老版本的路径下、重新唤起App。 这个时候,app运行就是新版本的样式和逻辑

附上具体的shell脚本:

注意需要传入的参数:tarball指的下载的安装包路径,destination指的当前app的bundlePath

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
function abort() {
echo "Update script aborted."
echo "Removing temporary directory..."
rm -rf $tempdir
echo "Removing tarball..."
rm -rf "$tarball"
echo "Relaunching bundle..."
open "$destination"
rm -f ~/.auto-update.lock
exit 1
}

tarball=$1
destination=$2

lockfile ~/.auto-update.lock

# Step 1. Wait until all processes from within the bundle are closed

processes=$(echo "$(ps ax)" | grep -v "$0")
# Escape the destination into a regexp that matches it
regexp=$(echo "$destination" | sed 's/[^[:alnum:]_-]/\\&/g')
# Filters entries matching the regexp, and do some magic to preserve the trailing newline
matches=$(echo "$processes" | awk "/$regexp/ { print \$1 }"; echo .)
matches=${matches%.}
# Count matches
count=`printf "%s" "$matches" | wc -l`
if [[ $count -gt 0 ]]; then
for pid in $matches
do
echo "$pid"
kill -9 "$pid"
done
fi


# Step 2. Check if the downloaded tar is empty, if so abort
if [ ! -s "$tarball" ]
then
abort
fi

# Step 3. Extract the new contents
echo "Creating temporary directory..."
tempdir=`mktemp -d /tmp/auto-update.XXXXX`
echo "Extracting new content from tarball..."
tar -xf "$tarball" -C "$tempdir"

# Step 4. Check if the extraction worked, if not abort
if [ $? -ne 0 ]
then
abort
fi

# Step 5. Remove the old bundle directory
echo "Removing bundle..."
rm -rf "$destination"/*
echo "Moving new content into place..."
mv -f $tempdir'/'$(ls $tempdir | head -n 1)'/'* "$destination"'/'
echo "Make sure destination is not quarantined..."
xattr -d com.apple.quarantine "$destination"
echo "Removing temporary directory..."
rm -rf $tempdir
echo "Removing tarball..."
rm -rf "$tarball"

# Step 6. (Re)launch the destination bundle
echo "Relaunching bundle..."
open "$destination"

echo "Done."
rm -f ~/.auto-update.lock

参考资料

Cocoa Launch at Login

Cocoa Launch at Login

Mac Developer Library中查看文档:

有两种方式可以添加登录启动项:使用Service Management framework 或 使用 shared file list

这两种方式有差别:

  • 使用Service Management framework 在系统的登录项中是不可见的。只有卸载App才能移除登录项

  • 使用 shared file list 在系统的登录项中是可见的。用户可以直接在面板上控制他们。(If you use this API, your login item can be disabled by the user, so any other application that communicates with it it should have reasonable fallback behavior in case the login item is disabled.) 原文还有一句大意是指这个API有隐患,所以在OS X 10.10系统上 API被大量Deprecated

下面具体介绍一下 这两种的实现

Adding Login Item Using the Service Management Framework

应用要包含一个 Helper Target,类型也是应用。设置路径为Contents/Library/LoginItems。设置这个Helper Target info.plist LSBackgroundOnly 为 YES。

使用 SMLoginItemSetEnabled方法授权Helper,需要传入两个参数:CFStringRef指的是Helper Target的bundle identifier. Boolean 表示期望状态。 传入 true Helper 会每次在用户登录的时候启动。 传入false 来注销这个登录项,不会在用户登录的时候启动。 这个方法会返回一个bool值,如果是ture的话,说明期望状态设置成功。如果是false的话,可能存在多个helper

比如 一个公司发布过很多App,但其中的helper bundle identifier都是相同的。 这样会导致在同一个电脑上只有一个App(greatest bundle version number)能成功注册登录项。

这个方式,我未实践过。贴几个相关的博文吧:

The Launch At Login Sandbox Project

Adding a preference to launch sandboxed app on login

在SandBox沙盒下实现程序的开机启动

Adding Login Items Using a Shared File List

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
- (BOOL)isLaunchAtStartup {
// See if the app is currently in LoginItems.
LSSharedFileListItemRef itemRef = [self itemRefInLoginItems];
// Store away that boolean.
BOOL isInList = itemRef != nil;
// Release the reference if it exists.
if (itemRef != nil) CFRelease(itemRef);

return isInList;
}

- (IBAction)toggleLaunchAtStartup:(id)sender {
// Toggle the state.
BOOL shouldBeToggled = ![self isLaunchAtStartup];
// Get the LoginItems list.
LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
if (loginItemsRef == nil) return;
if (shouldBeToggled) {
// Add the app to the LoginItems list.
CFURLRef appUrl = (__bridge CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
LSSharedFileListItemRef itemRef = LSSharedFileListInsertItemURL(loginItemsRef, kLSSharedFileListItemLast, NULL, NULL, appUrl, NULL, NULL);
if (itemRef) CFRelease(itemRef);
[[self.statusMenu itemAtIndex:0] setTitle:@"取消开机自启动"];
}
else {
// Remove the app from the LoginItems list.
LSSharedFileListItemRef itemRef = [self itemRefInLoginItems];
LSSharedFileListItemRemove(loginItemsRef,itemRef);
if (itemRef != nil) CFRelease(itemRef);
[[self.statusMenu itemAtIndex:0] setTitle:@"设置开机自启动"];
}
CFRelease(loginItemsRef);
}

- (LSSharedFileListItemRef)itemRefInLoginItems {
LSSharedFileListItemRef res = nil;

// Get the app's URL.
NSURL *bundleURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
// Get the LoginItems list.
LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
if (loginItemsRef == nil) return nil;
// Iterate over the LoginItems.
NSArray *loginItems = (__bridge NSArray *)LSSharedFileListCopySnapshot(loginItemsRef, nil);
for (id item in loginItems) {
LSSharedFileListItemRef itemRef = (__bridge LSSharedFileListItemRef)(item);
CFURLRef itemURLRef;
if (LSSharedFileListItemResolve(itemRef, 0, &itemURLRef, NULL) == noErr) {
// Again, use toll-free bridging.
NSURL *itemURL = (__bridge NSURL *)itemURLRef;
if ([itemURL isEqual:bundleURL]) {
res = itemRef;
break;
}
}
}
// Retain the LoginItem reference.
if (res != nil) CFRetain(res);
CFRelease(loginItemsRef);
CFRelease((__bridge CFTypeRef)(loginItems));

return res;
}

launch on startup

主要特别注意的是以上方法大都是 Deprecated in OS X V10.10 所以如果你要开发Deployment Target 10.10 以上的App的话不推荐使用这个方法。显然这些API在之后的OS X更新版本中,将会无法调用。导致你的App出现编译问题。

当然介于这个方式比较简单,你仍然想使用的话。你可以设置Deployment Target为 10.9 来使用这些在 10.10被废弃的API。是没有问题的。我就是这么干的,😊

Deprecated APIs

In previous versions of OS X, it is possible to add login items by sending an Apple event, by using the CFPreferences API, and by manually editing a property list file. These approaches are deprecated.

If you need to maintain compatibility with versions of OS X prior to v10.5, the preferred approach is to use Apple events; for details, see LoginItemsAE. Using the CFPreferences API is an acceptable alternative. You should not directly edit the property list file on any version of OS X.

早些 OS X版本的API 和实现方式,现在也许行不通了。我就不翻译了。

Range Sum Query Mutable

Range Sum Query - Mutable

给定一个整形数组。会有两个操作

操作1:查询区间(i,j)的总和

操作2:更新元素(i,val) 下标为i的元素 值更新为val

提示:

  • 数组元素的修改只有通过 update 方法
  • update 和 sumRange 方法调用是不确定次数和顺序的(distributed evenly 分布式)

这个是一个经典的问题。类似 RMQ(Range Minimum/Maximum Query),区间最值查询。不过我们注意到有操作2,就表示说数据是可变的,离线算法就不考虑了。

这类题目有一个现成的数据结构可以解决:SegmentTree 线段树

由于线段树是平衡二叉树,所以深度是logn。就是算查询的区间和的时间复杂度是 O(logn)的。

segment-tree1

由上图可知,叶子节点均为数组元素。我们在做ACM题的常常会提到一个线段树常数,其实是指线段树的空间复杂度。按照数组n来推算线段树的空间复杂度。

时间复杂度大体分为:

  • 初始化 建树O(n)
  • 区间查询和 O(logn)
  • 更新单值O(logn)

时间复杂度理想是线性的,不因为n的变化产生剧烈抖动。空间复杂度是O(nlogn)

A segment tree T on a set I of n intervals uses O(nlogn) storage.

Proof:
Lemma: Any interval [x, x′] of I is stored in the canonical set for at most two nodes at the same depth.

Proof: Let v1, v2, v3 be the three nodes at the same depth, numbered from left to right; and let p(v) be the parent node of any given node v. Suppose [x, x′] is stored at v1 and v3. This means that [x, x′] spans the whole interval from the left endpoint of Int(v1) to the right endpoint of Int(v3). Note that all segments at a particular level are non-overlapping and ordered from left to right: this is true by construction for the level containing the leaves, and the property is not lost when moving from any level to the one above it by combining pairs of adjacent segments. Now either p(v2) = p(v1), or the former is to the right of the latter (edges in the tree do not cross). In the first case, Int(p(v2))’s leftmost point is the same as Int(v1)’s leftmost point; in the second case, Int(p(v2))’s leftmost point is to the right of Int(p(v1))’s rightmost point, and therefore also to the right of Int(v1)’s rightmost point. In both cases, Int(p(v2)) begins at or to the right of Int(v1)’s leftmost point. Similar reasoning shows that Int(p(v2)) ends at or to the left of Int(v3)’s rightmost point. Int(p(v2)) must therefore be contained in [x, x′]; hence, [x, x′] will not be stored at v2.
The set I has at most 4n + 1 elementary intervals. Because T is a binary balanced tree with at most 4n + 1 leaves, its height is O(logn). Since any interval is stored at most twice at a given depth of the tree, that the total amount of storage is O(nlogn).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
typedef unsigned long lusigned;

struct SegmenTreeNode{
int val;
lusigned star,end;
SegmenTreeNode *left,*right;
SegmenTreeNode(lusigned _star,lusigned _end,int _val){
val = _val;
star = _star, end = _end;
left = right = NULL;
}
};

class NumArray {
public:
NumArray(vector<int> &nums) {
numbers = nums;
if(nums.size() >= 1){
rootNode = buildSegmentTree(0, 0, nums.size()-1, nums);
}
}

void update(int i, int val){
updateNodeVal(rootNode, i, val);
}

int sumRange(int i, int j) {
if(j < i) return 0;
return querySumValInSegmentTree(rootNode, 0, numbers.size()-1, i, j);
}

private:
vector<int> numbers;
SegmenTreeNode *rootNode;
SegmenTreeNode *buildSegmentTree(int root,lusigned star,lusigned end,const vector<int> nums){

SegmenTreeNode *rootNode = new SegmenTreeNode(star, end, 0);

if(star == end){
rootNode->val = nums[star];
}
else{
lusigned mid = (star + end)/2;
rootNode->left = buildSegmentTree(root*2+1, star, mid, nums);
rootNode->right = buildSegmentTree(root*2+2, mid+1, end, nums);

rootNode->val = rootNode->left->val + rootNode->right->val;
}

return rootNode;
}

int updateNodeVal(SegmenTreeNode *root, int idx,int value){
if(root->star == root->end && root->star == idx) return (root->val = value);
else if(root->star == root->end) return root->val;

lusigned mid = (root->star + root->end)>>1;
if(idx <= mid){
root->left->val = updateNodeVal(root->left, idx, value);
}
else{
root->right->val = updateNodeVal(root->right, idx, value);
}
return (root->val = root->left->val + root->right->val);
}

int querySumValInSegmentTree(SegmenTreeNode *root, lusigned star,lusigned end,lusigned qSidx,lusigned qEidx){

if(qSidx <= star && qEidx >= end) return root->val;

if(qEidx < star || qSidx > end) return 0;

lusigned mid = (star + end) / 2;

int leftSumVal = querySumValInSegmentTree(root->left, star, mid, qSidx, qEidx);
int rightSumVal = querySumValInSegmentTree(root->right, mid+1, end, qSidx, qEidx);

return leftSumVal + rightSumVal;
}
};

accepted

不太明白为什么 leetCode Judge 中 C/C++ 的效率最低,Java最高。这个明显是不合理的。。

最后 关于 线段树的更新是有优化空间的。看一看这组操作

  1. 操作1
  2. 操作2
  3. 操作2
  4. 操作2
  5. 操作2
  6. 操作2
  7. 操作2

其实 后面的操作2 如果没有接着操作1的话,其实我们没有必要实时更新单点值。这里就有一个延迟更新的优化点,也是线段树的精髓。 我之后有空补充:)

Lighter View Controller

Lighter-View-Controller 轻量级视图控制器

视图控制器们(View Controllers)通常是最大的文件在我们的iOS工程中。而且 他们常常有很多不必要的代码。导致视图控制器往往是无法在其他项目中复用的。我们下面讲述的技巧可以让控制器们 更加轻量,有复用性,让不合理的代码出现在合理的位置

example project on GitHub

分离出数据协议和其他接口协议

  • 一个很有用的技巧就是将视图控制器中的 UITableViewDataSource 这部分代码分离出来。独立写一个类来维护,如果你都是这么处理的话,那么你这些类是可以以后在其他项目中复用的。
  • 这样做还有一个好处就是,我们可以独立测试这个类。

举个例子:在示例项目中,PhotosViewController 有这样几个方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# pragma mark Pragma 

- (Photo*)photoAtIndexPath:(NSIndexPath*)indexPath {
return photos[(NSUInteger)indexPath.row];
}

- (NSInteger)tableView:(UITableView*)tableView
numberOfRowsInSection:(NSInteger)section {
return photos.count;
}

- (UITableViewCell*)tableView:(UITableView*)tableView
cellForRowAtIndexPath:(NSIndexPath*)indexPath {
PhotoCell* cell = [tableView dequeueReusableCellWithIdentifier:PhotoCellIdentifier
forIndexPath:indexPath];
Photo* photo = [self photoAtIndexPath:indexPath];
cell.label.text = photo.name;
return cell;
}

让我们试着把这些代码放到 我们自己的类上。我们用闭包来填充数据(或许协议更合适,这个取决于你)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@implementation ArrayDataSource

- (id)itemAtIndexPath:(NSIndexPath*)indexPath {
return items[(NSUInteger)indexPath.row];
}

- (NSInteger)tableView:(UITableView*)tableView
numberOfRowsInSection:(NSInteger)section {
return items.count;
}

- (UITableViewCell*)tableView:(UITableView*)tableView
cellForRowAtIndexPath:(NSIndexPath*)indexPath {
id cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier
forIndexPath:indexPath];
id item = [self itemAtIndexPath:indexPath];
configureCellBlock(cell,item);
return cell;
}

@end

这三个方法不再存在我们的视图控制器了,我们可以创建实例来承接table view的数据协议委托

1
2
3
4
5
6
7
void (^configureCell)(PhotoCell*, Photo*) = ^(PhotoCell* cell, Photo* photo) {
cell.label.text = photo.name;
};
photosArrayDataSource = [[ArrayDataSource alloc] initWithItems:photos
cellIdentifier:PhotoCellIdentifier
configureCellBlock:configureCell];
self.tableView.dataSource = photosArrayDataSource;

而且,这种办法扩展其他接口协议也很方便。比如 另外的一个list数据协议是 UICollectionViewDataSource 由于一些需求实现,你们决定将 UITableView 替换为 UICollectionView。实际上你的视图控制器不需要任何改动,你可以让你的类都支持这两个数据协议。

逻辑处理放在Model

这里还是一个例子,这些代码在view controller里,功能是返回一个列表,上面的数据是用户的活跃度

1
2
3
4
5
6
7
- (void)loadPriorities {
NSDate* now = [NSDate date];
NSString* formatString = @"startDate <= %@ AND endDate >= %@";
NSPredicate* predicate = [NSPredicate predicateWithFormat:formatString, now, now];
NSSet* priorities = [self.user.priorities filteredSetUsingPredicate:predicate];
self.priorities = [priorities allObjects];
}

当这些代码被放置到 User类的扩展中时,view controller就看起来比较清爽了。

1
2
3
- (void)loadPriorities {
self.priorities = [self.user currentPriorities];
}

User+Extensions.m:

1
2
3
4
5
6
- (NSArray*)currentPriorities {
NSDate* now = [NSDate date];
NSString* formatString = @"startDate <= %@ AND endDate >= %@";
NSPredicate* predicate = [NSPredicate predicateWithFormat:formatString, now, now];
return [[self.priorities filteredSetUsingPredicate:predicate] allObjects];
}

当然在实际项目中,有些代码不是那么轻松可以转移到Model当中的。所以我们要创建 “数据仓库”(Store Class)

创建仓库类

我们有些代码是从文件中加载数据并处理数据的,这些代码大概是这样的:

1
2
3
4
5
6
7
8
9
10
11
12
13
- (void)readArchive {
NSBundle* bundle = [NSBundle bundleForClass:[self class]];
NSURL *archiveURL = [bundle URLForResource:@"photodata"
withExtension:@"bin"];
NSAssert(archiveURL != nil, @"Unable to find archive in bundle.");
NSData *data = [NSData dataWithContentsOfURL:archiveURL
options:0
error:NULL];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
_users = [unarchiver decodeObjectOfClass:[NSArray class] forKey:@"users"];
_photos = [unarchiver decodeObjectOfClass:[NSArray class] forKey:@"photos"];
[unarchiver finishDecoding];
}

其实 View Controller 没有必要关心数据是如何处理的,我们创建 数据仓库来处理这些。使这些代码分离出来,这样我们就可以复用这个仓库类了。
独立测试并且View Controller的代码量又减少了一些。这些数据仓库负责数据处理,持久化和数据库交互。我们也可以叫这些”仓库”为服务层或资料库

将网络服务逻辑转移到Model

这是一个类似的优化逻辑:不要把网络交互放在View Controller。把这些逻辑独立放置在其他类中,可以调用方法并设置回调处理。
这样做的好处是你可以格外处理所有你的数据和错误在这个类上。而不会让View Controller变的臃肿

结论

我们看到有很多技巧可以让View Controller更加精简。我们努力让这些技巧在实际开发中更加实用。
我们只有一个目的:写出可维护的代码。知道这些模式之后,我们有更加深刻的认识来讨论如何将笨重的View Controller精简优化。

扩展阅读