delegates

Storing an async function as a variable

强颜欢笑 提交于 2020-01-03 05:11:13
问题 I have a question that is similar to this question but that differs in that in my case I am dealing with Asynchronous functions. So as the question says I want to store a method in a variable (to call it later) In the case of syncrhonous functions private delegate void eventmethod(); //(for a function without arguments and return void) private eventmethod MySavedEvent; void D() { } MySavedEvent = D; MySavedEvent(); But what happens if the function is actually Task<returnType> D(); How can I

iOS - Setting delegate of input stream to another class

陌路散爱 提交于 2020-01-03 03:06:07
问题 i am wondering that if it is possible to set the delegate of input stream to another class. So far all examples i have encountered are with self: [inputStream setDelegate:self] . I want to set delegate to another class like a ViewController not self. Thanks in advance. 回答1: if your ViewController is responding to NSStreamDelegate , you can initiate an instance of the controller and set the delegate as usual. @interface ViewController : NSOperation<NSStreamDelegate> ... - ViewController *vc =

.NET delegate equality?

↘锁芯ラ 提交于 2020-01-03 02:57:07
问题 I think this is the question, anyway. I am using a RelayCommand, which decorates an ICommand with two delegates. One is Predicate for the _canExecute and the other is Action for the _execute method. ---Background motivation -- The motivation has to do with unit testing ViewModels for a WPF presentation. A frequent pattern is that I have one ViewModel that has an ObservableCollection, and I want a unit test to prove the data in that collection is what I expect given some source data (which

Implementing delegation

白昼怎懂夜的黑 提交于 2020-01-02 09:59:19
问题 I think I'm following how delegation works, here's the tutorial I followed, but I'm messing up somewhere. I'm expecting my delegate to NSLog but it's not. Can anyone find out what am I missing or doing wrong? My MainViewController.h : @interface MainViewController : UITableViewController < AddClassDelegate > MainViewController.m : - (void)cancelAddingClass { NSLog(@"Canceled Yo"); } - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { /* When a row is selected, the segue

Should I use List<IObserver> or simply Action<T> to keep track of an IObservable's subscribers?

故事扮演 提交于 2020-01-02 08:30:14
问题 I'm implementing the IObservable<T> interface on some classes. I used Reflector to figure out how this is typically done in Rx. Concerning how an observable keeps track of its subscribers and notifies them via their OnNext method, I stumbled upon code similar to this: private List<Observer<T>> observers; // subscribe a new observer: public IDisposable Subscribe(IObserver<T> observer) { observers.Add(observer); ... } // trigger all observers' OnNext method: ... foreach (IObserver<T> observer

Python Delegate Pattern - How to avoid circular reference?

為{幸葍}努か 提交于 2020-01-02 06:11:30
问题 I would to ask if using the Delegate Pattern in Python would lead to circular references and if so, what would be the best way to implement it to ensure the object and its delegate will be garbage collected? In Objective C, the above problem is avoided by using a weak reference to the delegate. In C++, we don't call delete on the delegate. I've found a link to Python's weak reference module here: http://docs.python.org/library/weakref.html. It seems like a plausible approach might be to

Abort a Thread started with Delegate.BeginInvoke

放肆的年华 提交于 2020-01-02 05:51:10
问题 Disclaimer : I know that Thread.Abort is evil. I'm using it as a last resort since some I/O functions (e.g., File.Exists when used on a non-existing network share) block for a large amout of time and do not allow you to specify a timeout. Question : Is it possible to Abort (as in Thread.Abort ) a worker thread started using Delegate.BeginInvoke or do I have to do the Thread handling myself? 回答1: The only way to do that would be to have the delegate pass its Thread.CurrentThread to the main

Abort a Thread started with Delegate.BeginInvoke

僤鯓⒐⒋嵵緔 提交于 2020-01-02 05:51:05
问题 Disclaimer : I know that Thread.Abort is evil. I'm using it as a last resort since some I/O functions (e.g., File.Exists when used on a non-existing network share) block for a large amout of time and do not allow you to specify a timeout. Question : Is it possible to Abort (as in Thread.Abort ) a worker thread started using Delegate.BeginInvoke or do I have to do the Thread handling myself? 回答1: The only way to do that would be to have the delegate pass its Thread.CurrentThread to the main

How do I declare a Func Delegate which returns a Func Delegate of the same type?

隐身守侯 提交于 2020-01-02 01:00:10
问题 I'd like to write a method which does some work and finally returns another method with the same signature as the original method. The idea is to handle a stream of bytes depending on the previous byte value sequentially without going into a recursion. By calling it like this: MyDelegate executeMethod = handleFirstByte //What form should be MyDelegate? foreach (Byte myByte in Bytes) { executeMethod = executeMethod(myByte); //does stuff on byte and returns the method to handle the following

iOS - viewDidLoad is being called BEFORE the didFinishLaunchingWithOptions delegate?

会有一股神秘感。 提交于 2020-01-01 18:18:11
问题 I have an application that is working as it was supposed to. However, I put one breakpoint at the first line of my rootViewController's viewDidLoad method and another breakpoint in the first line of my delegate's didFinishLaunchingWithOptions, Surprisingly for me, the application entered in the viewDidLoad method, then went to the didFinishLaunchingWithOptions, and then executed one more time the viewDidLoad method. What is going on? I think that that behavior is totally wrong. Thank you in