加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – 尝试在单例中更改变量但它保持可为空

发布时间:2020-12-16 10:01:58 所属栏目:百科 来源:网络整理
导读:刚刚开始编写 objective-c编程,现在我遇到了自己无法解决的问题.我正在从异步请求接收数据并尝试将其转发为单例,但它没有改变. 这是我试图存储我的数据的地方 Data.h #import Foundation/Foundation.h@interface Data : NSObject@property (nonatomic,strong
刚刚开始编写 objective-c编程,现在我遇到了自己无法解决的问题.我正在从异步请求接收数据并尝试将其转发为单例,但它没有改变.

这是我试图存储我的数据的地方
Data.h

#import <Foundation/Foundation.h>
@interface Data : NSObject
@property (nonatomic,strong) NSDictionary *products;
-(void)setProducts:(NSDictionary *)value;
@end

Data.m

#import "Data.h"

@implementation Data

+(Data *)sharedInstance
{
    static Data *_sharedInstance = nil;
    static dispatch_once_t oncePredicate;
    dispatch_once(&oncePredicate,^{
        _sharedInstance = [[Data alloc] init];
    });
    return _sharedInstance;
}

- (id)init {
    self = [super init];
    if ( self )
    {
        _products = [[NSDictionary alloc] init];
    }
    return self;
}

@end

这是我从服务器接收数据的类:
ConnectionService.m

- (void)getProductsWithCompletion:(void (^)(NSDictionary *products))completion
{
    NSString *urlString = [NSString stringWithFormat:@"serverurl",[[AppDelegate instance]getUrl]];
    NSURL *url = [NSURL URLWithString:urlString];

    NSURLSessionDataTask *getData = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *data,NSURLResponse *response,NSError *error){
        NSString *rawJson = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSDictionary *value = [rawJson JSONValue];
        completion(value);
    }];
    [getData resume];
}

这是我正在调用请求并尝试将其传递给单例的类:
viewController.m

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:YES];
    [[ConnectionService instance] getProductsWithCompletion:^(NSDictionary *products) {
        [Data sharedInstance].products = products;
        NSLog(@"products: %@",[[Data sharedInstance] products]);//all is working,products contains data
    }];
    // checking received data 
    NSDictionary *tmp = [[Data sharedInstance] products];
    NSLog(@"tmp: %@",tmp); //now it's null
}

解决方法

问题在于请求是异步的,并且事情没有按照您期望的顺序发生:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:YES];
     [[ConnectionService instance] getProductsWithCompletion:^(NSDictionary *products) {
        // (2)
        [Data sharedInstance].products = products;
        NSLog(@"products: %@",[[Data sharedInstance]products]);//all is working,products contains data
    }];


    // (1)
    NSDictionary *tmp = [[Data sharedInstance]products];
    NSLog(@"tmp: %@",tmp); //now it's null
}

在您发布的代码中,(1)将在(2)之前发生.这是因为(2)是完成块的一部分,并且一旦网络请求完成并且所有数据都已被解析并准备好使用,就会设置为运行.虽然该异步请求已准备好并在后台线程中运行,但主线程((1))会在请求发生之前继续并执行.

要解决此问题,请将日志记录移至完成例程,或者只需删除(1).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读