SlideShare a Scribd company logo
1 of 76
自己紹介
• 
• 
• 
• 

片岡 直之
株式会社シロク
SNS: @katty0324
ブログ: 三度の飯とエレクトロン (
http://blog.katty.in/ )
ある日の社内SNSにて・・・
なんとなく
とは言ったものの
最後にiOSアプリを作っていたの
は半年以上前だったことを思い出
し・・・

←アバターチャットアプリ
半年前にクローズ済み
ネタがないことに気づく。
自分のブログを漁ってみたところ
良さそうなのがあった。
というわけで本日のテーマ
Objective-CのBlocksの循環参
照に関する僕なりのベストプラク
ティス
その前に
Delegateとは?
Objective-C流のコールバック
手法
Blocksとは?
Objective-C流のモダンなコー
ルバック手法
参照カウントとは?
オブジェクトの解放管理のための
数値変数
Blocksの循環参照とは?
オブジェクトがBlocksを参照して
いて、そのBlocks内でそのオブ
ジェクトを参照している状態

強参照

呼び出し元
強参照

Blocksの実行者

Blocks
強参照
BLOCKSの使い方
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
Blocksで処理を保持しておけば、
必要になった時に実行できる。
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>
Blocksが循環参照してしまうと、
オブジェクトは解放されない。
ではどうするか?
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
Blocks内に渡されたオブジェクト
は参照カウントが増えてしまうの
で、弱参照の修飾子を添えて渡す。

強参照

呼び出し元
弱参照

Blocksの実行者

Blocks
強参照
というのは結構スタンダードな手
法ですが
僕はweakSelfパターンと呼んで
勝手にアンチパターン扱いしてい
ます。
WEAKSELFパターンの弱点
__blockとか、__weakという修
飾子が汚い。
Blocksを使うたびに、毎回変数
を確保しなければならない。
オブジェクトを弱参照で渡すので、
オブジェクトの存在を保証できな
い。
Blocks構文の使い手が、中の実
装を意識しなくてはいけない。
そもそも
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;
}
こうやって使ってもちゃんとメモ
リが解放されるのが理想であって
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;
}
使い手が、メモリが解放されるよ
うに仕向けなければならないのは
良くない。
BLOCKSの使い方は3種類
その場限りのBlocks
1度きりのBlocks
使いまわすBlocks
その場限りのBLOCKS
たとえば、jQueryにおける
$.each(func)のような、同期的
に処理されるものなど。
InstantBlocksRunner *instantBlocksRunner = [[[InstantBlocksRunner alloc]
init] autorelease];
[instantBlocksRunner run: ^{
NSLog(@"instantBlocksRunner: %@", instantBlocksRunner);
}];
循環参照している!
これをメモリリークさせないため
には・・・
- (void)run:(void (^)(void))runnable {
if(runnable)
runnable();
}

強参照

呼び出し元
強参照

Blocksの実行者

Blocks
弱参照
その場限りのBlocksは、Blocks
を保持しないことで、メモリリー
クを回避する!
1度きりのBLOCKS
非同期的にHTTP通信しその結果
をBlocksでコールバックするよ
うな場合など。
OneTimeBlocksRunner *oneTimeBlocksRunner = [[[OneTimeBlocksRunner
alloc] init] autorelease];
oneTimeBlocksRunner.runnable = ^{
NSLog(@"oneTimeBlocksRunner: %@", oneTimeBlocksRunner);
};
[oneTimeBlocksRunner run];
循環参照している!
これをメモリリークさせないため
には・・・
- (void)run {
if(runnable)
runnable();
self.runnable = nil;
}

強参照

呼び出し元
強参照
使い終わったら
即解放

Blocksの実行者

Blocks
強参照
1度だけBlocksを使う場合は、
使った直後にちゃんと解放してや
ることでメモリリークを回避す
る!
使いまわすBLOCKSパターン
画面上のボタンが押された時の
コールバック処理をBlocksで実
行する場合など。
MultiTimeBlocksRunner *multiTimeBlocksRunner =
[[[MultiTimeBlocksRunner alloc] init] autorelease];
[multiTimeBlocksRunner setRunnable:^(id target){
NSLog(@"multiTimeBlocksRunner: %@", target);
} target:multiTimeBlocksRunner];
[multiTimeBlocksRunner run];
[multiTimeBlocksRunner run];
[multiTimeBlocksRunner run];
循環参照している!
これをメモリリークさせないため
には・・・
@property (nonatomic, copy) void(^runnable)(id target);
@property (nonatomic, assign) id target;

強参照

呼び出し元
弱参照

Blocksの実行者

Blocks
強参照
この場合ばかりは、オブジェクト
を弱参照で保持できるオプション
を用意してメモリリークを回避す
る。
まとめ
Blocksを利用する際の循環参照
によるメモリリークを回避するた
めに、無差別に弱参照を使わない。
できるだけ使い手側にメモリ管理
を意識させないようにするために、
Blocksを保持する側が解放の責
任をもつ方が良い。
最後に
株式会社シロクでは、スマホアプリの開
発・運用支援ツールを続々提供していま
す!

プッシュ通知配信・解析プラットフォーム
Growth Push

ユーザー獲得・継続のための共通ポイント基盤
Growth Point

アプリのテストを効率化するツール
Growth Debug

ユーザビリティテストのための新しいツール
Growth ??? (近日公開)
 Objective-CのBlocksの循環参照に関する僕なりのベストプラクティス

More Related Content

Recently uploaded

TSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdfTSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdftaisei2219
 
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)Hiroki Ichikura
 
SOPを理解する 2024/04/19 の勉強会で発表されたものです
SOPを理解する       2024/04/19 の勉強会で発表されたものです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論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A surveyToru Tamaki
 
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介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...論文紹介: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] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略Ryo Sasaki
 
論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNet論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNetToru Tamaki
 

Recently uploaded (9)

TSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdfTSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdf
 
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
 
SOPを理解する 2024/04/19 の勉強会で発表されたものです
SOPを理解する       2024/04/19 の勉強会で発表されたものですSOPを理解する       2024/04/19 の勉強会で発表されたものです
SOPを理解する 2024/04/19 の勉強会で発表されたものです
 
論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A survey論文紹介: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」の紹介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...論文紹介: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] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
 
論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNet論文紹介: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 Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)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 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd 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 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 IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...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 Hubspot2024 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 ChatGPTEverything 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 EngineeringsProduct 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 HealthHow 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.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO 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)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 2024How 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 InsightsSocial 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 2024Trends 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 summary5 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 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 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 IntentGoogle'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 How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe 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...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の循環参照に関する僕なりのベストプラクティス