【问题标题】:What is the proper way of making weak reference of Singleton class对 Singleton 类进行弱引用的正确方法是什么
【发布时间】:2016-07-27 13:00:44
【问题描述】:

我遇到了一种情况,我正在使用sharedClass,并在其中使用块获取数据。有一个约定是在块内使用 __weak 引用来防止循环保留。如何创建一个将在整个应用程序中保持活动状态的东西的弱引用?

这是我所做的代码,

+ (instancetype)sharedPresenter {
    static PostCareListPresenter *presenterInstance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        presenterInstance = [[PostCareListPresenter alloc] init];
    });
        return presenterInstance;
}

- (void)getPostCareList:(NSDictionary*)pParameterDictionary
                success:(void(^)(NSData*responseData))pSuccessCallback
                failure:(void(^)(NSError* error))pFailureCallback {
    [[GSWebAccessManager sharedManager] invokePostCareList:pParameterDictionary success:^(NSData *responseData) {
        // Here I have to make a call to my private method using weak self.  [weakSelf somePrvateMethod]; 

    } failure:^(NSError *error) {

    }];

}

【问题讨论】:

    标签: ios objective-c memory-management singleton reference-counting


    【解决方案1】:

    作为一个单例对象永远不会被破坏,你不应该关心它是否被保留

    【讨论】:

    • 我应该使用self而不是weakSelf吗?
    • 当然。为什么不?你不关心它的保留计数。它永远存在
    • 或者你可以使用weakSelf,如果你愿意的话。它不会改变任何事情
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-26
    • 1970-01-01
    • 2019-06-19
    • 2018-01-24
    • 2018-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多