OGeek|极客世界-中国程序员成长平台

标题: ios - 模型、 block 和完成处理程序 Oh MY [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 11:51
标题: ios - 模型、 block 和完成处理程序 Oh MY

我从零开始学习 iOS 编程。

我希望我的应用从网站中提取 XML。我在想,为了符合 MVC 模式,我应该有一个模型类,它只提供一种方法来完成它(也许让它也解析 XML 并返回一个数组)。

问题是我找到的所有教程都在 View 和 Controller 的上下文中教授 NSURLSession - 所以编辑 appdelegate,或创建 View Controller 等。

我从 Apples 文档中获得了以下方法,我目前在按下按钮时将其作为 IBAction 运行(因此我可以轻松地运行它并对其进行测试)。我想让它工作,然后把它放在它自己的类中:

__block NSMutableData *webData;

NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];

NSURLSession *delegateFreeSession = [NSURLSession sessionWithConfiguration:defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];
[[delegateFreeSession dataTaskWithURL: [NSURL URLWithString:url] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
    NSLog(@"Got response %@ with error %@.\n", response, error);
    NSLog(@"DATA:\n%@\nEND DATA\n", [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]);
        webData = [[NSMutableData alloc] initWithData:data];
}
]resume];

我的直接问题是:

有人可以解释完成处理程序是如何工作的以及如何从那里获取 data 吗?它正在工作,数据正在从网站上获取 xml 并将其记录在控制台上,但是将其复制到 webData 不起作用,它会编译但不会复制。 (我还在弄清楚为什么 __block 声明首先允许 webData 潜入其中!)

我更大的问题是,如果每个人都认为为这个过程创建一个单独的模型类的想法是个好主意。有没有更好的设计方法?

谢谢!



Best Answer-推荐答案


这可能只是对异步 block 如何工作的一些混淆。如果你这样做:

__block NSMutableData *webData;
// ...

[[delegateFreeSession dataTaskWithURL: [NSURL URLWithString:url] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
    NSLog(@"within the block, did I get data: %@", data);
    webData = [[NSMutableData alloc] initWithData:data];
}]resume];

NSLog(@"after the block, did I get data: %@", webData);

您可能会看到如下所示的输出:

after the block, did I get data: (null)
within the block, did I get data: <NSData ...

什么给了?为什么 block 后的代码先运行?数据在哪里?问题在于我们对“之后”的定义。 block 之后出现的 NSLog 实际在 block 运行之前运行。它会在 dataRequest 启动 后立即运行。 block 内的代码在请求完成后运行

将数据结果保存在该方法的本地 block 变量中没有好处。当您到达方法的末尾时,该值未初始化。 block 在运行时初始化它,但在 block 完成后立即丢弃该值。

修复:处理 block 内的数据。不要指望它在 block 运行之后才有效(在方法运行之后):

编辑 - 在这个 block 中使用 self 来调用方法、设置属性等是 100% 没问题的。只有当 block 本身是 self 的属性时,您才需要注意保留周期 (或 self 保留的东西的属性),它不是......

// don't do this
//__block NSMutableData *webData;
// ...

[[delegateFreeSession dataTaskWithURL: [NSURL URLWithString:url] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
    NSLog(@"within the block, did I get data: %@", data);
    NSMutableData *webData = [[NSMutableData alloc] initWithData:data];
    // do whatever you plan to do with web data
    // write it to disk, or save it in a property of this class
    // update the UI to say the request is done

    [self callAMethod:data];     // fine
    [self callAnotherMethod];    // fine
    self.property = data;        // fine
}]resume];

// don't do this, there's no data yet
//NSLog(@"after the block, did I get data: %@", webData);

关于ios - 模型、 block 和完成处理程序 Oh MY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25937578/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4