我使用'WCSession‘来连接我的应用程序和苹果手表。我更喜欢单例方法。所以,我做了一个共享会话:
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;
}然后,在启动会话中,我为会话设置了代理:
-(void)startSession {
if ([WCSession isSupported]) {
self.session = [WCSession defaultSession];
self.session.delegate = self;
[self.session activateSession];
LOG(@"WCSession is supported");
}
}释放委托的正确方法是什么?
根据apple's docs的说法,我可以通过以下方法做到这一点:
sessionDidBecomeInactive(_:)
sessionDidDeactivate(_:)如果我将委托设置为nil,会不会影响我的应用程序的性能?
发布于 2019-08-02 01:18:06
it is a weak reference:WCSession.delegate不会泄露
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;
// ...如果您使用的是ARC,而您的委托仍然保存在内存中,这并不是因为WCSession.delegate
发布于 2019-08-01 17:10:32
首先,我想知道self.session是跟在弧线后面的,因为委托总是包含弱引用,所以不需要将它设置为零。
这会引起什么问题吗?如果您不手动设置为空?如果是,那么您可以在sessionDidDeactivate中将其设置为nil,如下所示:Called after all data from the previous session has been delivered and communication with the Apple Watch has ended.,您可以设置新会话,如下所示
func sessionDidDeactivate(session: WCSession) {
// Begin the activation process for the new Apple Watch.
WCSession.defaultSession().activateSession()
}https://stackoverflow.com/questions/57256668
复制相似问题