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
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
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
A2UI という光を覗いてみる
satohjohn
1
130
Inside Stream API
skrb
1
690
ローカルLLMを使ってB2Bサービスを作っていての学び
yaotti
0
160
dRuby over BLE
makicamel
2
330
ADKを使って簡単にAIエージェントを作ってみよう
k1mu21
0
260
Technical Debt: Understanding it Rightly, Engaging it Rightly #LaravelLiveJP
shogogg
0
220
Language Server 使ってる? 〜VSCode と Zed の場合〜 / Are you using a Language Server? ~For VS Code and Zed~
handlename
0
780
Observability in Practice:Grafana 與 Edge Device SRE 的那些事
blueswen
0
160
jQueryをバージョンアップする前に使いたいjQuery Migrate
matsuo_atsushi
0
210
Vite+ Unified Toolchain for the Web
naokihaba
0
280
ユニットテストの先へ:テスト技法で要求・仕様を整理するJava開発実践 / Beyond_Unit_Testing_Practical_Java_Development_Techniques_for_Organizing_Requirements_and_Specifications
shimashima35
0
390
AI 時代のソフトウェア設計の学び方
masuda220
PRO
29
12k
Featured
See All Featured
A designer walks into a library…
pauljervisheath
211
24k
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
160
Documentation Writing (for coders)
carmenintech
77
5.4k
Google's AI Overviews - The New Search
badams
0
1k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
390
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
Git: the NoSQL Database
bkeepers
PRO
432
67k
Heart Work Chapter 1 - Part 1
lfama
PRO
7
36k
Reality Check: Gamification 10 Years Later
codingconduct
0
2.2k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
3.4k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
52k
Bash Introduction
62gerente
615
220k
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