Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
NSProxy, multithreading, messaging
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Cyril Lashkevich
March 13, 2014
Programming
110
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
NSProxy, multithreading, messaging
Cyril Lashkevich
March 13, 2014
More Decks by Cyril Lashkevich
See All by Cyril Lashkevich
Go Scheduler
notorca
2
630
Bitcode in Swift
notorca
0
75
Mobile Optimized 2014
notorca
1
280
Fun with blocks in ObjC
notorca
1
110
CocoaHeads in Grodno, Lighting
notorca
0
99
Dictionary in Python
notorca
0
140
Foundation data structures
notorca
0
170
iOS memory management
notorca
0
120
Python impergections
notorca
0
99
Other Decks in Programming
See All in Programming
ECSアプリログをFireLensでコスト削減しようとしたけど諦めた話 in Fargate×Node.js
akihisaikeda
2
4.3k
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
100
なぜ関数型プログラミングで「型」と「証明」が語られるのか #fp_matsuri
kajitack
3
800
吝嗇家のためのAI活用 / AI development for miser - ChatGPT + Issue Driven Development
tooppoo
0
180
1年で人数1.5倍、PR数5.5倍増。 品質とアウトカムはどうなったか、 何が効いたか
ike002jp
0
120
「なぜそう決めたのか」を残し続ける仕組み ― Notion AI カスタムエージェント × Slack連携による設計判断の自動記録 - NIKKEI Tech Talk #47
niftycorp
PRO
0
260
act2-costs.pdf
sumedhbala
0
110
エンジニアにデザインハーネスを 〜デザインプロセスを規定するためのハーネス〜 / Design harness from an engineer's perspective
rkaga
2
1.4k
関数型プログラミングのメリットって何だろう?
wanko_it
0
170
Skillsは効率化、Agentsは"自分の拡張"——Builder時代のエージェント編成(CC Night 2026)
wemra
1
210
【やさしく解説 設計編・中級 #6】良いアーキテクチャとは ~ 一本の登り道の、行き先 ~
panda728
PRO
0
150
OS アップデート対応の取り組み方がもっと共有されてほしい
andpad
0
110
Featured
See All Featured
Claude Code のすすめ
schroneko
67
230k
Six Lessons from altMBA
skipperchong
29
4.3k
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
500
How Software Deployment tools have changed in the past 20 years
geshan
0
34k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
201
75k
Darren the Foodie - Storyboard
khoart
PRO
3
3.4k
Code Review Best Practice
trishagee
74
20k
Documentation Writing (for coders)
carmenintech
77
5.4k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.5k
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
2.1k
Principles of Awesome APIs and How to Build Them.
keavy
128
18k
GitHub's CSS Performance
jonrohan
1033
470k
Transcript
NSProxy, messaging, многопоточность и асинхронность в ObjC Cyril @notorca Lashkevich
wtorek, 10 września 13
"All problems in computer science can be solved by another
level of indirection..." David Wheeler wtorek, 10 września 13
"All problems in computer science can be solved by another
level of indirection..." David Wheeler "...except for the problem of too many layers of indirection." Kevlin Henney wtorek, 10 września 13
Задача Прозрачно пересылать вызовы методов обекта (например делегата) в другой
поток wtorek, 10 września 13
Задача Прозрачно пересылать вызовы методов обекта (например делегата) в другой
поток [obj performSelector:@selector(doThing:) onThread:thread withObject:param waitUntilDone:NO]; wtorek, 10 września 13
Задача Прозрачно пересылать вызовы методов обекта (например делегата) в другой
поток [obj performSelector:@selector(doThing:) onThread:thread withObject:param waitUntilDone:NO]; wtorek, 10 września 13
Задача Прозрачно пересылать вызовы методов обекта (например делегата) в другой
поток [obj performSelector:@selector(doThing:) onThread:thread withObject:param waitUntilDone:NO]; [obj doThing:param]; wtorek, 10 września 13
NSProxy Абстрактный класс Реализует протокол NSObject Минимальная реализация требует переопределения
2х методов - (void)forwardInvocation:(NSInvocation *) invocation - (NSMethodSignature *) methodSignatureForSelector:(SEL)aSelector // Всегда возвращает результат wtorek, 10 września 13
Интерфейс @interface ThreadProxy : NSProxy @property (weak) id target; -
(id)initWithTarget:(id)target thread:(NSThread *)thread @end wtorek, 10 września 13
Приватная часть интерфейса @interface ThreadProxy () { @protected NSThread *_thread;
id _target; Class _targetClass; } - (void)_invokeInvocation:(NSInvocation *)invocation; @end wtorek, 10 września 13
init - (id)initWithTarget:(id)target thread:(NSThread *)thread { if (!thread) self =
nil; if (self) { _thread = thread; _target = target; _targetClass = [target class]; } return self; } wtorek, 10 września 13
- (NSMethodSignature *) methodSignatureForSelector: (SEL)aSelector { return [_targetClass instanceMethodSignatureForSelector: aSelector];
} - (BOOL)respondsToSelector:(SEL)aSelector { return [self.target respondsToSelector:aSelector]; } wtorek, 10 września 13
- (void)setTarget:(id)target { @synchronized(self) { _target = target; if (target)
{ _targetClass = [target class]; } } } - (id)target { @synchronized(self) { return _target; } } wtorek, 10 września 13
NSInvocation Вызов метода "замороженный" в виде объекта target, selector и
параметры могут быть изменены Вызов можно делать сколько угодно раз Не для vararg и union параметров wtorek, 10 września 13
- (void)forwardInvocation:(NSInvocation *)invocation { [invocation retainArguments]; } wtorek, 10 września
13
@implementation NSThread (ProxyAdditions) - (void)_invokeWithThreadProxy:(NSInvocation *)invocation { ThreadProxy *p =
invocation.target; [p _invokeInvocation:invocation]; } @end - (void)forwardInvocation:(NSInvocation *)invocation { [invocation retainArguments]; } wtorek, 10 września 13
@implementation NSThread (ProxyAdditions) - (void)_invokeWithThreadProxy:(NSInvocation *)invocation { ThreadProxy *p =
invocation.target; [p _invokeInvocation:invocation]; } @end - (void)forwardInvocation:(NSInvocation *)invocation { [invocation retainArguments]; [_thread performSelector: @selector(_invokeWithTreadProxy:) onThread:_thread withObject:invocation waitUntilDone:NO]; } wtorek, 10 września 13
- (void)_invokeInvocation:(NSInvocation *)invocation { if (_target) { [invocation invokeWithTarget:_target]; }
} wtorek, 10 września 13
- (void)_invokeInvocation:(NSInvocation *)invocation { if (_target) { [invocation invokeWithTarget:_target]; }
} - (void)_invokeInvocation:(NSInvocation *)invocation { if ([_target respondsToSelector: invocation.selector]) { [invocation invokeWithTarget:_target]; } } wtorek, 10 września 13
- (void)forwardInvocation:(NSInvocation *)invocation { NSMethodSignature *s = invocation.methodSignature; NSInteger lastArg
= s.numberOfArguments - 1; const void *block = nil; [invocation getArgument:&block atIndex:lastArg]; block = Block_copy(block); [invocation setArgument:&block atIndex:lastArg]; [invocation retainArguments]; Block_release(block); [_thread performSelector:@selector(_invokeWithTreadProxy:) onThread:_thread withObject:invocation waitUntilDone:NO]; } wtorek, 10 września 13
Usefull links http://clang.llvm.org/docs/Block-ABI-Apple.html https://github.com/ebf/CTObjectiveCRuntimeAdditions https://github.com/pandamonia/BlocksKit http://sourceware.org/libffi/ wtorek, 10 września 13