Search code examples
iosobjective-capple-watchwatchoswcsession

setting delegate of WCSession to nil


I'm using 'WCSession' for the connection between my app and apple watch. I preferred singleton approach. So, I made a shared session:

static Shared_WCSession *sharedInstance = nil;
+(Shared_WCSession*)getSharedInstance {
    @synchronized(self) {
        // If the class variable holding the reference to the single ContentManager object is empty create it.
        if(sharedInstance == nil) {
             sharedInstance = [[Shared_WCSession alloc] init];
        }
    }
     return sharedInstance;
}

Then in start session I set up the delegate for the session:

-(void)startSession {
     if ([WCSession isSupported]) {
        self.session = [WCSession defaultSession];
        self.session.delegate = self;
        [self.session activateSession];
        LOG(@"WCSession is supported");
    }
}

What is the proper way to deallocate the delegate?

According to apple's docs I can do it in the following methods:

sessionDidBecomeInactive(_:) 
sessionDidDeactivate(_:)

If I set the delegate to nil there will this interfere with the performance of my applications?


Solution

  • WCSession.delegate won't leak: it is a weak reference

    NS_CLASS_AVAILABLE_IOS(9.0)
    @interface WCSession : NSObject
    // ...
    /** A delegate must exist before the session will allow sends. */
    @property (nonatomic, weak, nullable) id <WCSessionDelegate> delegate;
    // ...
    

    If you're using ARC and your delegate is still being held on memory, it is not because of WCSession.delegate