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
270
Fun with blocks in ObjC
notorca
1
110
CocoaHeads in Grodno, Lighting
notorca
0
95
Dictionary in Python
notorca
0
130
Foundation data structures
notorca
0
170
iOS memory management
notorca
0
120
Python impergections
notorca
0
95
Other Decks in Programming
See All in Programming
LLM本来の能力を解き放つサンドボックス技術とAI民主化への適用
yukukotani
3
3.6k
ユニットテストの先へ:テスト技法で要求・仕様を整理するJava開発実践 / Beyond_Unit_Testing_Practical_Java_Development_Techniques_for_Organizing_Requirements_and_Specifications
shimashima35
0
390
AI 時代のソフトウェア設計の学び方
masuda220
PRO
29
12k
気づいたらRubyで100作品 ー クリエイティブコーディングが生活の一部になるまで / 100 Ruby Sketches Later: How Creative Coding Became Part of My Life
chobishiba
3
570
The NotImplementedError Problem in Ruby
koic
1
710
Modding RubyKaigi for Myself
yui_knk
0
920
ローカルLLMを使ってB2Bサービスを作っていての学び
yaotti
0
160
作って学ぶ、 JSX (TSX) ランタイムの基本
syumai
7
1.6k
過去最大のMCPアップデート! 2026-07-28 RC版の謎に迫る
licux
6
250
net-httpのHTTP/2対応について
naruse
0
470
ふつうのFeature Flag実践入門
irof
7
3.7k
Vite+ Unified Toolchain for the Web
naokihaba
0
280
Featured
See All Featured
Code Review Best Practice
trishagee
74
20k
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
480
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
390
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.3k
Writing Fast Ruby
sferik
630
63k
Paper Plane (Part 1)
katiecoart
PRO
0
8.8k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.3k
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
170
Art, The Web, and Tiny UX
lynnandtonic
304
22k
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
5
1.1k
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
320
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
420
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