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
Vue × Nuxt × Oxc どこまで使える?実運用の現在地
andpad
0
270
RTSPクライアントを自作してみた話
simotin13
0
620
Mujeres en SEO Summit 2026 - Greatest Disaster Hits en Web Performance
guaca
0
190
コンテキストの使い捨てをやめる — ビジネスルール駆動開発と miko —
ioki
0
210
TSKaigi Night Talks 2026_TypeScriptでサプライチェーンの整合性を型に閉じ込める
geekplus_tech
0
400
A2UI という光を覗いてみる
satohjohn
1
140
dRuby over BLE
makicamel
2
380
C# and C++ Interoperability - cho-dotnetnew
harukasao
0
280
AI 輔助遺留系統現代化的經驗分享
jame2408
1
810
The ROI of Quarkus for Spring Boot Applications
hollycummins
0
120
「なぜそう決めたのか」を残し続ける仕組み ― Notion AI カスタムエージェント × Slack連携による設計判断の自動記録 - NIKKEI Tech Talk #47
niftycorp
PRO
0
210
Lemonade + Foundry Toolkit でお手軽アプリ開発
seosoft
1
360
Featured
See All Featured
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
2
220
Getting science done with accelerated Python computing platforms
jacobtomlinson
2
240
How to Think Like a Performance Engineer
csswizardry
28
2.7k
How STYLIGHT went responsive
nonsquared
100
6.2k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.8k
New Earth Scene 8
popppiees
3
2.3k
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
320
Site-Speed That Sticks
csswizardry
13
1.2k
[SF Ruby Conf 2025] Rails X
palkan
2
1.1k
WCS-LA-2024
lcolladotor
0
650
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
250
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
160
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