Submit Search
Upload
Objective-CのBlocksの循環参照に関する僕なりのベストプラクティス
•
18 likes
•
8,594 views
Naoyuki Kataoka
Follow
http://blog.katty.in/2612
Read less
Read more
Technology
Report
Share
Report
Share
1 of 76
Recommended
objective-c propertyの属性について
objective-c propertyの属性について
Hiroki Uemoto
Ca shape layerの塗りつぶしルールでちょっとリッチな描画をしよう
Ca shape layerの塗りつぶしルールでちょっとリッチな描画をしよう
Kenji Tanaka
The monad fear
The monad fear
LINE Corporation
Swift 3 を書くときに知っておきたい API デザインガイドライン #love_swift #akibaswift
Swift 3 を書くときに知っておきたい API デザインガイドライン #love_swift #akibaswift
Tomohiro Kumagai
シロク流事業の立ち上げ方の成功と失敗
シロク流事業の立ち上げ方の成功と失敗
Naoyuki Kataoka
成長する組織を支えるシロクの自動化
成長する組織を支えるシロクの自動化
Naoyuki Kataoka
SIROK技術勉強会 #1 「Reactってなんだ?」
SIROK技術勉強会 #1 「Reactってなんだ?」
Naoyuki Kataoka
Lightning-fastなライトニングトーク
Lightning-fastなライトニングトーク
Naoyuki Kataoka
Recommended
objective-c propertyの属性について
objective-c propertyの属性について
Hiroki Uemoto
Ca shape layerの塗りつぶしルールでちょっとリッチな描画をしよう
Ca shape layerの塗りつぶしルールでちょっとリッチな描画をしよう
Kenji Tanaka
The monad fear
The monad fear
LINE Corporation
Swift 3 を書くときに知っておきたい API デザインガイドライン #love_swift #akibaswift
Swift 3 を書くときに知っておきたい API デザインガイドライン #love_swift #akibaswift
Tomohiro Kumagai
シロク流事業の立ち上げ方の成功と失敗
シロク流事業の立ち上げ方の成功と失敗
Naoyuki Kataoka
成長する組織を支えるシロクの自動化
成長する組織を支えるシロクの自動化
Naoyuki Kataoka
SIROK技術勉強会 #1 「Reactってなんだ?」
SIROK技術勉強会 #1 「Reactってなんだ?」
Naoyuki Kataoka
Lightning-fastなライトニングトーク
Lightning-fastなライトニングトーク
Naoyuki Kataoka
TSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdf
taisei2219
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
Hiroki Ichikura
SOPを理解する 2024/04/19 の勉強会で発表されたものです
SOPを理解する 2024/04/19 の勉強会で発表されたものです
iPride Co., Ltd.
論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A survey
Toru Tamaki
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Yuma Ohgami
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
Toru Tamaki
スマートフォンを用いた新生児あやし動作の教示システム
スマートフォンを用いた新生児あやし動作の教示システム
sugiuralab
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
Ryo Sasaki
論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNet
Toru Tamaki
2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
Marius Sescu
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
Skeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
More Related Content
Recently uploaded
TSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdf
taisei2219
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
Hiroki Ichikura
SOPを理解する 2024/04/19 の勉強会で発表されたものです
SOPを理解する 2024/04/19 の勉強会で発表されたものです
iPride Co., Ltd.
論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A survey
Toru Tamaki
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Yuma Ohgami
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
Toru Tamaki
スマートフォンを用いた新生児あやし動作の教示システム
スマートフォンを用いた新生児あやし動作の教示システム
sugiuralab
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
Ryo Sasaki
論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNet
Toru Tamaki
Recently uploaded
(9)
TSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdf
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
SOPを理解する 2024/04/19 の勉強会で発表されたものです
SOPを理解する 2024/04/19 の勉強会で発表されたものです
論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A survey
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
スマートフォンを用いた新生児あやし動作の教示システム
スマートフォンを用いた新生児あやし動作の教示システム
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNet
Featured
2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
Marius Sescu
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
Skeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
Introduction to Data Science
Introduction to Data Science
Christy Abraham Joy
Time Management & Productivity - Best Practices
Time Management & Productivity - Best Practices
Vit Horky
The six step guide to practical project management
The six step guide to practical project management
MindGenius
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson36
Featured
(20)
2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
Skeleton Culture Code
Skeleton Culture Code
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Getting into the tech field. what next
Getting into the tech field. what next
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
How to have difficult conversations
How to have difficult conversations
Introduction to Data Science
Introduction to Data Science
Time Management & Productivity - Best Practices
Time Management & Productivity - Best Practices
The six step guide to practical project management
The six step guide to practical project management
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Objective-CのBlocksの循環参照に関する僕なりのベストプラクティス
1.
2.
自己紹介 • • • • 片岡 直之 株式会社シロク SNS: @katty0324 ブログ:
三度の飯とエレクトロン ( http://blog.katty.in/ )
3.
ある日の社内SNSにて・・・
4.
5.
なんとなく
6.
7.
とは言ったものの
8.
最後にiOSアプリを作っていたの は半年以上前だったことを思い出 し・・・ ←アバターチャットアプリ 半年前にクローズ済み
9.
ネタがないことに気づく。
10.
11.
自分のブログを漁ってみたところ
12.
13.
良さそうなのがあった。
14.
15.
というわけで本日のテーマ
16.
Objective-CのBlocksの循環参 照に関する僕なりのベストプラク ティス
17.
その前に
18.
Delegateとは?
19.
Objective-C流のコールバック 手法
20.
Blocksとは?
21.
Objective-C流のモダンなコー ルバック手法
22.
参照カウントとは?
23.
オブジェクトの解放管理のための 数値変数
24.
Blocksの循環参照とは?
25.
オブジェクトがBlocksを参照して いて、そのBlocks内でそのオブ ジェクトを参照している状態 強参照 呼び出し元 強参照 Blocksの実行者 Blocks 強参照
26.
BLOCKSの使い方
27.
int main(int argc,
const char * argv[]) { @autoreleasepool { BlocksRunner *blocksRunner = [[[BlocksRunner alloc] init] autorelease]; NSLog(@"blockRunner retainCount: %ld", [blocksRunner retainCount]); blocksRunner.runnable = ^{ NSLog(@"blocksRunner"); }; NSLog(@"blockRunner retainCount: %ld", [blocksRunner retainCount]); [blocksRunner run]; } return 0; } BlocksRunner init blockRunner retainCount: 1 blockRunner retainCount: 1 blocksRunner BlocksRunner dealloc
28.
Blocksで処理を保持しておけば、 必要になった時に実行できる。
29.
int main(int argc,
const char * argv[]) { @autoreleasepool { BlocksRunner *blocksRunner = [[[BlocksRunner alloc] init] autorelease]; NSLog(@"blockRunner retainCount: %ld", [blocksRunner retainCount]); blocksRunner.runnable = ^{ NSLog(@"blocksRunner: %@", blocksRunner); }; NSLog(@"blockRunner retainCount: %ld", [blocksRunner retainCount]); [blocksRunner run]; } return 0; } BlocksRunner init blockRunner retainCount: 1 blockRunner retainCount: 2 blocksRunner: <BlocksRunner: 0x1001144b0>
30.
Blocksが循環参照してしまうと、 オブジェクトは解放されない。
31.
ではどうするか?
32.
int main(int argc,
const char * argv[]) { @autoreleasepool { BlocksRunner *blocksRunner = [[[BlocksRunner alloc] init] autorelease]; NSLog(@"blockRunner retainCount: %ld", [blocksRunner retainCount]); __block BlocksRunner *weakBlocksRunner = blocksRunner; blocksRunner.runnable = ^{ NSLog(@"blocksRunner: %@", weakBlocksRunner); }; NSLog(@"blockRunner retainCount: %ld", [blocksRunner retainCount]); [blocksRunner run]; } return 0; } BlocksRunner init blockRunner retainCount: 1 blockRunner retainCount: 1 blocksRunner: <BlocksRunner: 0x1001143b0> BlocksRunner dealloc
33.
Blocks内に渡されたオブジェクト は参照カウントが増えてしまうの で、弱参照の修飾子を添えて渡す。 強参照 呼び出し元 弱参照 Blocksの実行者 Blocks 強参照
34.
というのは結構スタンダードな手 法ですが
35.
僕はweakSelfパターンと呼んで 勝手にアンチパターン扱いしてい ます。
36.
WEAKSELFパターンの弱点
37.
__blockとか、__weakという修 飾子が汚い。
38.
Blocksを使うたびに、毎回変数 を確保しなければならない。
39.
オブジェクトを弱参照で渡すので、 オブジェクトの存在を保証できな い。
40.
Blocks構文の使い手が、中の実 装を意識しなくてはいけない。
41.
そもそも
42.
int main(int argc,
const char * argv[]) { @autoreleasepool { BlocksRunner *blocksRunner = [[[BlocksRunner alloc] init] autorelease]; NSLog(@"blockRunner retainCount: %ld", [blocksRunner retainCount]); blocksRunner.runnable = ^{ NSLog(@"blocksRunner: %@", blocksRunner); }; NSLog(@"blockRunner retainCount: %ld", [blocksRunner retainCount]); [blocksRunner run]; } return 0; }
43.
こうやって使ってもちゃんとメモ リが解放されるのが理想であって
44.
int main(int argc,
const char * argv[]) { @autoreleasepool { BlocksRunner *blocksRunner = [[[BlocksRunner alloc] init] autorelease]; NSLog(@"blockRunner retainCount: %ld", [blocksRunner retainCount]); __block BlocksRunner *weakBlocksRunner = blocksRunner; blocksRunner.runnable = ^{ NSLog(@"blocksRunner: %@", weakBlocksRunner); }; NSLog(@"blockRunner retainCount: %ld", [blocksRunner retainCount]); [blocksRunner run]; } return 0; }
45.
使い手が、メモリが解放されるよ うに仕向けなければならないのは 良くない。
46.
BLOCKSの使い方は3種類
47.
その場限りのBlocks
48.
1度きりのBlocks
49.
使いまわすBlocks
50.
その場限りのBLOCKS
51.
たとえば、jQueryにおける $.each(func)のような、同期的 に処理されるものなど。
52.
InstantBlocksRunner *instantBlocksRunner =
[[[InstantBlocksRunner alloc] init] autorelease]; [instantBlocksRunner run: ^{ NSLog(@"instantBlocksRunner: %@", instantBlocksRunner); }];
53.
循環参照している!
54.
これをメモリリークさせないため には・・・
55.
- (void)run:(void (^)(void))runnable
{ if(runnable) runnable(); } 強参照 呼び出し元 強参照 Blocksの実行者 Blocks 弱参照
56.
その場限りのBlocksは、Blocks を保持しないことで、メモリリー クを回避する!
57.
1度きりのBLOCKS
58.
非同期的にHTTP通信しその結果 をBlocksでコールバックするよ うな場合など。
59.
OneTimeBlocksRunner *oneTimeBlocksRunner =
[[[OneTimeBlocksRunner alloc] init] autorelease]; oneTimeBlocksRunner.runnable = ^{ NSLog(@"oneTimeBlocksRunner: %@", oneTimeBlocksRunner); }; [oneTimeBlocksRunner run];
60.
循環参照している!
61.
これをメモリリークさせないため には・・・
62.
- (void)run { if(runnable) runnable(); self.runnable
= nil; } 強参照 呼び出し元 強参照 使い終わったら 即解放 Blocksの実行者 Blocks 強参照
63.
1度だけBlocksを使う場合は、 使った直後にちゃんと解放してや ることでメモリリークを回避す る!
64.
使いまわすBLOCKSパターン
65.
画面上のボタンが押された時の コールバック処理をBlocksで実 行する場合など。
66.
MultiTimeBlocksRunner *multiTimeBlocksRunner = [[[MultiTimeBlocksRunner
alloc] init] autorelease]; [multiTimeBlocksRunner setRunnable:^(id target){ NSLog(@"multiTimeBlocksRunner: %@", target); } target:multiTimeBlocksRunner]; [multiTimeBlocksRunner run]; [multiTimeBlocksRunner run]; [multiTimeBlocksRunner run];
67.
循環参照している!
68.
これをメモリリークさせないため には・・・
69.
@property (nonatomic, copy)
void(^runnable)(id target); @property (nonatomic, assign) id target; 強参照 呼び出し元 弱参照 Blocksの実行者 Blocks 強参照
70.
この場合ばかりは、オブジェクト を弱参照で保持できるオプション を用意してメモリリークを回避す る。
71.
まとめ
72.
Blocksを利用する際の循環参照 によるメモリリークを回避するた めに、無差別に弱参照を使わない。
73.
できるだけ使い手側にメモリ管理 を意識させないようにするために、 Blocksを保持する側が解放の責 任をもつ方が良い。
74.
最後に
75.
株式会社シロクでは、スマホアプリの開 発・運用支援ツールを続々提供していま す! プッシュ通知配信・解析プラットフォーム Growth Push ユーザー獲得・継続のための共通ポイント基盤 Growth Point アプリのテストを効率化するツール Growth
Debug ユーザビリティテストのための新しいツール Growth ??? (近日公開)