0

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?

tara tandel
  • 538
  • 5
  • 25

2 Answers2

1

First I want to know that self.session is following arc, and as delegate always contains weak reference then no need to set it to nil.

Is it causing any issue? if you are not setting it nil manually ? If yes then you can set it to nil in sessionDidDeactivate as it says:Called after all data from the previous session has been delivered and communication with the Apple Watch has ended. and you can set new session like

func sessionDidDeactivate(session: WCSession) {
    // Begin the activation process for the new Apple Watch.
    WCSession.defaultSession().activateSession()
}
PJR
  • 13,052
  • 13
  • 64
  • 104
  • Yes I'm using ARC. But there is two issues with this delegate: First of all it's causing memory leak. Second, some times my sessions are not managed smoothly. like I have 2 apple watches the second one seems to be connected but it does not receiving any data. – tara tandel Aug 01 '19 at 10:23
  • @taratandel, I think you got your answer :) as I mentioned in my answer, weak reference will work for you like `@property (nonatomic, weak, nullable) id delegate;` – PJR Aug 08 '19 at 04:51
0

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

baguIO
  • 391
  • 2
  • 14