{"id":317,"date":"2014-11-08T17:15:49","date_gmt":"2014-11-08T09:15:49","guid":{"rendered":"http:\/\/www.yusian.com\/blog\/?p=317"},"modified":"2016-07-15T17:21:54","modified_gmt":"2016-07-15T09:21:54","slug":"%e5%a4%9a%e7%ba%bf%e7%a8%8b%e6%8a%80%e6%9c%afnsoperationqueue%e7%9a%842%e7%a7%8d%e9%98%9f%e5%88%97%e6%93%8d%e4%bd%9c%e7%a4%ba%e4%be%8b","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/project\/2014\/11\/08\/171549317.html","title":{"rendered":"\u591a\u7ebf\u7a0b\u6280\u672fNSOperationQueue\u76842\u79cd\u961f\u5217\u64cd\u4f5c\u793a\u4f8b"},"content":{"rendered":"<p>1\u3001<strong>NSOperation<\/strong>\u7684\u4e24\u4e2a\u5b50\u7c7b<span style=\"color: #ff0000;\"><strong>NSBlockOperation<\/strong><\/span>\u4e0e<span style=\"color: #ff0000;\"><strong>NSInvocationOperation<\/strong><\/span>\uff0c\u524d\u8005\u901a\u8fc7Block\u5b9e\u73b0\u961f\u5217\u6267\u884c\u4f53\uff0c\u540e\u8005\u901a\u8fc7\u8c03\u7528\u65b9\u6cd5\u5b9e\u73b0\u3002<\/p>\n<p>2\u3001\u521b\u5efa\u597dNSOperation\u4efb\u52a1\u540e\uff0c\u52a0\u5165\u5230\u7cfb\u7edf\u961f\u5217\uff0c\u5206\u4e3a\u81ea\u5b9a\u4e49\u961f\u5217\u5373\u901a\u8fc7<strong>NSOperationQueue<\/strong>\u521b\u5efa\uff0c\u6216\u52a0\u5165\u5230\u4e3b\u961f\u5217<strong>[NSOperationQueue mainQueue]<\/strong><\/p>\n<p>3\u3001\u4ee5\u4e0a\u4e24\u6b65\u5373\u53ef\u641e\u5b9a\uff0cNSOperation\/NSOperationQueue\u4e2d\u7a0b\u5e8f\u4e0d\u9700\u8981\u5173\u5fc3\u540c\u6b65\/\u5f02\u6b65\u3001\u4e32\u884c\/\u5e76\u884c\u95ee\u9898\uff0c\u7531\u6846\u67b6\u81ea\u52a8\u5904\u7406\uff0c\u56e0\u6b64\u4f7f\u7528\u8d77\u6765\u66f4\u65b9\u4fbf\u66f4\u5b89\u5168\u3002<\/p>\n<p>4\u3001\u64cd\u4f5c\u4efb\u52a1\u4e00\u6dfb\u52a0\u5230\u961f\u5217\u4e2d\u5373\u4f1a\u6267\u884c\uff0c\u8fd9\u4e00\u70b9\u4e0eGCD\u4e0d\u540c\u3002<\/p>\n<p>5\u3001\u5982\u679c\u9700\u8981\u6307\u5b9a\u4efb\u52a1\u7684\u5148\u540e\u6267\u884c\u4efb\u52a1\uff0c\u53ef\u4f7f\u7528NSOperation\u7684 <span style=\"color: #ff0000;\">addDependency: <\/span>\u65b9\u6cd5\uff0c\u5982 <strong>[op2 addDependency:op1]<\/strong>\u8868\u793aop2\u4f9d\u8d56\u4e8eop1\uff0c\u56e0\u6b64\u4f1a\u5728op1\u540e\u88ab\u6267\u884c\u3002\uff08\u6ce8\u610f\u4e0d\u8981\u51fa\u73b0\u5faa\u73af\u4f9d\u8d56\uff09<\/p>\n<p>6\u3001\u5982\u679c\u9700\u8981\u9650\u5236\u7ebf\u7a0b\u6570\uff0c\u53ef\u901a\u8fc7NSOperationQueue\u7684 <span style=\"color: #ff0000;\">setMaxConcurrentOperationCount:<\/span> \u65b9\u6cd5\u5b9e\u73b0\uff0c\u5982\u56fe\u7247\u4e0b\u8f7d\u4efb\u52a1\u5728WiFi\u73af\u5883\u4e2d\u7ebf\u7a0b\u6570\u4e3a6\uff0c3G\u73af\u5883\u4e2d\u4e3a3<!--more--><\/p>\n<p><strong>\u5173\u952e\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre lang=\"objc\" line=\"1\">\/\/ \u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u961f\u5217\n@property (nonatomic, strong) NSOperationQueue  *myQueue;\n\n    \/\/ \u521d\u59cb\u5316\u81ea\u5b9a\u4e49\u961f\u5217\n    self.myQueue = [[NSOperationQueue alloc] init];\n\n#pragma mrak 1\u3001Block\u961f\u5217\u64cd\u4f5c\n- (void)blockOperation\n{\n    \/\/ \u5faa\u73af10\u6b21\u6dfb\u52a010\u4e2a\u4efb\u52a1\u5230\u81ea\u5b9a\u4e49\u961f\u5217\n    for (int i = 0; i < 10; i++) {\n        \n        \/\/ \u521b\u5efa\u4e00\u4e2a\u64cd\u4f5c\u4efb\u52a1\uff0c\u4e0d\u9700\u8981\u5173\u5fc3\u4e32\u884c\u5e76\u884c\u7b49\u95ee\u9898\n        NSBlockOperation *blockOp = [NSBlockOperation blockOperationWithBlock:^{\n            \n            \/\/ \u6bcf\u6267\u884c\u4e00\u6b21\u4efb\u52a1\uff0c\u8f93\u51fa\u8be5\u4efb\u52a1\u7684\u6dfb\u52a0\u5e8f\u53f7\u53ca\u7ebf\u7a0b\u5bf9\u8c61\n            NSLog(@\"\u7b2c %d \u6b21 - %@\", i, [NSThread currentThread]);\n        }];\n        \n        \/\/ \u5c06\u4efb\u52a1\u6dfb\u52a0\u5230\u81ea\u5b9a\u4e49\u961f\u5217\u4e2d\uff0c\u9ed8\u8ba4\u5c06\u5f02\u6b65\u5e76\u884c\u6267\u884c\n        [self.myQueue addOperation:blockOp];\n        \n        \/\/ \u5c06\u4efb\u52a1\u6dfb\u52a0\u5230\u4e3b\u961f\u5217\u4e2d\uff0c\u5373\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u6267\u884c\n        \/\/ [[NSOperationQueue mainQueue] addOperation:blockOp];\n    }\n}\n\n#pragma mark 2\u3001invocation\u961f\u5217\u64cd\u4f5c\n- (void)invocationOperation\n{\n    for (int i = 0; i < 10; i++) {\n        \n        \/\/ \u4e0eNSBlockOperation\u7c7b\u4f3c\uff0c\u552f\u4e00\u4e0d\u540c\u7684\u662f\u8fd9\u91cc\u901a\u8fc7\u8c03\u7528\u65b9\u6cd5\u5b9e\u73b0\u961f\u5217\u6267\u884c\u4f53\n        NSInvocationOperation *invocationOp = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(operationAction:) object:@(i)];\n        \n        [self.myQueue addOperation:invocationOp];\n        \n        \/\/ [[NSOperationQueue mainQueue] addOperation:invocationOp];\n        \n    }\n}\n\n#pragma mark \u8c03\u7528\u65b9\u6cd5\n- (void)operationAction:(id)obj\n{\n    NSLog(@\"\u7b2c %d \u6b21 - %@\", [obj intValue], [NSThread currentThread]);\n}<\/pre>\n<p><strong>Demo\u4e0b\u8f7d\uff1a<\/strong><a href=\"http:\/\/www.yusian.com\/blog\/wp-content\/uploads\/2016\/07\/Test-2.zip\">Test<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001NSOperation\u7684\u4e24\u4e2a\u5b50\u7c7bNSBlockOperation\u4e0eNSInvocationOperation\uff0c\u524d\u8005\u901a\u8fc7Block\u5b9e\u73b0\u961f\u5217\u6267\u884c\u4f53\uff0c\u540e\u8005\u901a\u8fc7\u8c03\u7528\u65b9\u6cd5\u5b9e\u73b0\u3002 2\u3001\u521b\u5efa\u597dNSOperation\u4efb\u52a1\u540e\uff0c\u52a0\u5165\u5230\u7cfb\u7edf\u961f\u5217\uff0c\u5206\u4e3a\u81ea\u5b9a\u4e49\u961f\u5217\u5373\u901a\u8fc7NSOperationQueue\u521b\u5efa\uff0c\u6216\u52a0\u5165\u5230\u4e3b\u961f\u5217[NSOperationQueue mainQueue] 3\u3001\u4ee5\u4e0a\u4e24\u6b65\u5373\u53ef\u641e\u5b9a\uff0cNSOperation\/NSOperationQueue\u4e2d\u7a0b\u5e8f\u4e0d\u9700\u8981\u5173\u5fc3\u540c\u6b65\/\u5f02\u6b65\u3001\u4e32\u884c\/\u5e76\u884c\u95ee\u9898\uff0c\u7531\u6846\u67b6\u81ea\u52a8\u5904\u7406\uff0c\u56e0\u6b64\u4f7f\u7528\u8d77\u6765\u66f4\u65b9\u4fbf\u66f4\u5b89\u5168\u3002 4\u3001\u64cd\u4f5c\u4efb\u52a1\u4e00\u6dfb\u52a0\u5230\u961f\u5217\u4e2d\u5373\u4f1a\u6267\u884c\uff0c\u8fd9\u4e00\u70b9\u4e0eGCD\u4e0d\u540c\u3002 5\u3001\u5982\u679c\u9700\u8981\u6307\u5b9a\u4efb\u52a1\u7684\u5148\u540e\u6267\u884c\u4efb\u52a1\uff0c\u53ef\u4f7f\u7528NSOperation\u7684 addDependency: \u65b9\u6cd5\uff0c\u5982 [op2 addDependency:op1]\u8868\u793aop2\u4f9d\u8d56\u4e8eop1\uff0c\u56e0\u6b64\u4f1a\u5728op1\u540e\u88ab\u6267\u884c\u3002\uff08\u6ce8\u610f\u4e0d\u8981\u51fa\u73b0\u5faa\u73af\u4f9d\u8d56\uff09 6\u3001\u5982\u679c\u9700\u8981\u9650\u5236\u7ebf\u7a0b\u6570\uff0c\u53ef\u901a\u8fc7NSOperationQueue\u7684 setMaxConcurrentOperationCount: \u65b9\u6cd5\u5b9e\u73b0\uff0c\u5982\u56fe\u7247\u4e0b\u8f7d\u4efb\u52a1\u5728WiFi\u73af\u5883\u4e2d\u7ebf\u7a0b\u6570\u4e3a6\uff0c3G\u73af\u5883\u4e2d\u4e3a3<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[20],"class_list":["post-317","post","type-post","status-publish","format-standard","hentry","category-project","tag-basics"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/317","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/comments?post=317"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/317\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=317"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}