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
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
97
Dictionary in Python
notorca
0
130
Foundation data structures
notorca
0
170
iOS memory management
notorca
0
120
Python impergections
notorca
0
98
Other Decks in Programming
See All in Programming
Inside Stream API
skrb
1
740
ローカルLLMを使ってB2Bサービスを作っていての学び
yaotti
0
200
TAKTでAI駆動開発の品質を設計する
j5ik2o
7
1.4k
Signal Forms: Details & Live Coding @enterJS 2026 in Mannheim
manfredsteyer
PRO
0
160
生成AI時代にこそ効くGo | Why Go Works in the Age of Generative AI
mom0tomo
8
3.3k
作って学ぶ、 JSX (TSX) ランタイムの基本
syumai
7
1.6k
CSC307 Lecture 17
javiergs
PRO
0
320
脅威をエンジニアリングの糧にして――現場編 / Turning Threats into Engineering Fuel — Field Edition
nrslib
0
290
Composerを使ったサプライチェーン攻撃の様子を眺めてみる #phpstudy
o0h
PRO
2
250
Strategic Design in the Frontend: Moduliths & Micro Frontends @DDDEurope
manfredsteyer
PRO
0
110
「AIで開発し、AIを届ける」をEvalでつなぐ 〜AIネイティブに始めるプロダクト開発の実践〜 / Connecting "Develop with AI, deliver AI" with Eval
rkaga
4
5.3k
Vite+ Unified Toolchain for the Web
naokihaba
0
320
Featured
See All Featured
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k
Writing Fast Ruby
sferik
630
63k
A better future with KSS
kneath
240
18k
How STYLIGHT went responsive
nonsquared
100
6.2k
Game over? The fight for quality and originality in the time of robots
wayneb77
1
200
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
490
We Are The Robots
honzajavorek
0
250
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
320
First, design no harm
axbom
PRO
2
1.2k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.2k
The Cost Of JavaScript in 2023
addyosmani
55
10k
Art, The Web, and Tiny UX
lynnandtonic
304
22k
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