1

I have 2 classes. Son inherits from Dad. (Note code has been simplified)

@interface Dad : NSObject
@interface Son : Dad

Dad Class

- (void)setupSession
{
    dadSession = [NSURLSession config:config delegate:self delegateQueue:mainQueue];    
    // PROBLEM: self == Son, not Dad
}

Son class

- (void)startDadSession
{
    [super setupSession];
}

then someone somewhere calls...

[son startDadSession];

I want the Dad class to be its own delegate for its session, but when I call [super setupSession] from the Son class, when the execution goes into the Dad class self == Son.

Is there a way to have self not be the instance of the Child class here?

Anand Kore
  • 1,300
  • 1
  • 15
  • 33
BigHeadCreations
  • 1,583
  • 3
  • 16
  • 31

2 Answers2

1

Sadly, inheritance and delegates may sometimes interact in bad ways. Generally I find it hard to write code when a class inherits from another class that is a delegate. But it is necessary from time to time.

One solution I try is to override the delegate methods in the subclass and add a check for the correct instance ofNSURLSession, or whichever is the correct object. If it is the wrong session, pass it on to the super class implementation. E.g.

- (void)URLSession:(NSURLSession *)session didBecomeInvalidWithError:(NSError *)error {
    if (session == self.sonSession) {
        // do stuff
    } else {
        // Unknown session, should be the dadSession so pass it on
        [super URLSession:session didBecomeInvalidWithError:error];
    }
}
Mats
  • 8,528
  • 1
  • 29
  • 35
  • This works. So the `dadSession` delegate actually gets set to the `Son` class but in the `Son` delegate methods I can like you said either test the `session` or the `task` and if they belong to `Dad` I can pass them up. – BigHeadCreations Mar 22 '18 at 03:45
0

modify Dad Class's

- (void)setupSession

with

- (void)setupSessionWithDelegate:(id)delegate

and

dadSession = [NSURLSession config:config delegate:self delegateQueue:mainQueue];

with

dadSession = [NSURLSession config:config delegate:delegate delegateQueue:mainQueue];

Then implement delegate methods.

Atanu Mondal
  • 1,714
  • 1
  • 24
  • 30
Anand Kore
  • 1,300
  • 1
  • 15
  • 33
  • But in the `dadSession = [NSURLSession config:config delegate:delegate delegateQueue:mainQueue];` how do I specify the delegate as the `Dad` class if `self == Son` when that function is called? – BigHeadCreations Mar 21 '18 at 13:28
  • When you are in DadClass just pass delegate as self and same for SonClass. – Anand Kore Mar 21 '18 at 13:33
  • That still doesn't work b/c when `Son` calls `[super someDadMethod]` and execution moves into the `Dad` class the value of `self` == `Son`. This made more sense when I found this definition of `super` in another [post](https://stackoverflow.com/questions/3095360/what-exactly-is-super-in-objective-c): It's a keyword that's equivalent to self, but starts its message dispatch searching with the superclass's method table. – BigHeadCreations Mar 22 '18 at 02:42