gpt4 book ai didi

ios - 为什么 NSOperationQueue 不执行使用 addOperationWithBlock 提交的 block ?

转载 作者:行者123 更新时间:2023-11-29 02:42:20 24 4
gpt4 key购买 nike

事情是这样的,我得到了一些不执行的代码(编译器运行代码但不做任何事情)...这是代码...使用 NSURLSessionDelegate[NSOperationQueue mainQueue] addOperationWithBlock

@interface tablaPosViewController : UIViewController  <NSURLSessionDelegate>

@end

@implementation tablaPosViewController ()

- (void)viewDidLoad
{
//some code to set the view, labels and stuff

[self downloadTheHTMLdata] // this code download some data from WWW

}

- (void)downloadTheHMTLdata
{
//some code to set the session using an object

[object.downloadTask resume]; //Begins the download

}

- (void)toFixTablaPosiciones
{
//some code to do with the downloaded data from WWW (and HTML sheet)
//here, parse the HTML Sheet, and put some data into an Arrays, and another public vars

//call another method
[self PutTheDataInLabel];

}

- (void)PutTheDataInLabel
{
//some code to put all the data in every label
//take the public vars that was set in the previous method, and do some code with it
//and put the info into the labels

//call another method
[self MoreStuff];

}

- (void)MoreStuff
{

//some code..

}


-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{
//When the download finish this method fire up!

//this is to copy file from tmp folder to Docs folder
if ([fileManager fileExistsAtPath:[destinationURL path]])
{
[fileManager removeItemAtURL:destinationURL error:nil];
}
BOOL success = [fileManager copyItemAtURL:location //TMP download folder
toURL:destinationURL //Documents folder
error:&error];
//HERES COMES THE TROUBLE!!!
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self toFixTablaPosiciones]; //Call this method, that has other method calls!
}];

}
@end

更新

这另一个代码将方法放入队列中...

-(void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session{
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
[self.session getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) {
if ([downloadTasks count] == 0) {
if (appDelegate.backgroundTransferCompletionHandler != nil) {
void(^completionHandler)() = appDelegate.backgroundTransferCompletionHandler;
appDelegate.backgroundTransferCompletionHandler = nil;
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
completionHandler();
}];
}
}
}];
}

问题是当下载文件结束时,调用 -(void)URLSession:(NSURLSession *)session downloadTask:... 方法,我等待 [[NSOperationQueue mainQueue] addOperationWithBlock :^{... 运行所有内容...但它不执行 [self toFixTablaPosiciones] 的任何内容!!。

我一步一步地运行代码,我看到编译器如何运行所有代码,一个方法一个方法...但是 View 从不更新,运行但不执行,只是做任何事情,我有一个事件指示器,并想要停止它,但标签仍然带有虚拟数据,并且事件指示器永远不会停止,也永远不会消失。在之前的 View 中,我使用类似的类下载另一个文件,并且下载速度非常快。进入这个 View /类尝试执行下载,这就是事情......

希望任何人都可以帮助我并向我发送任何建议。谢谢!

最佳答案

与大多数有经验的开发人员一样,我使用的一种技术是使用断言和 NSLog(您可以注释掉它)来验证您对代码所做的假设实际上是正确的。尝试剪切并粘贴以下代码,看看会发生什么 - 它应该有所帮助。将来,不要用头撞墙——开始添加断言和日志。在某些时候,您会发现某些假设是不正确的。

-(void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session {
NSLog(@"Received URLSessionDidFinishEventsForBackgroundURLSession: application state is %d", [UIApplication sharedApplication] applicationState];
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;

[self.session getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) {
NSLog(@"Download task count %d", [downloadTasks count]);
#warning "This looks like incorrect logic, but I don't know this background code. Wouldn't you see "1", or more? Won't you get an array of the tasks you submitted???
if ([downloadTasks count] == 0) {
assert(appDelegate.backgroundTransferCompletionHandler); // if this is nil, your app will be stuck in some odd state forever, so treat this like a fatal error
void(^completionHandler)() = appDelegate.backgroundTransferCompletionHandler;
appDelegate.backgroundTransferCompletionHandler = nil;

assert([NSOperationQueue mainQueue]); // why not?
assert(![NSOperationQueue mainQueue].isSuspended); // I looked at the class description, the queue **could** be suspended
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"Completion Operation called!");
assert(completionHandler); // perhaps an ARC bug, unlikely, but then you cannot get this code to work, right?
completionHandler();
}];
}
}];
}

关于ios - 为什么 NSOperationQueue 不执行使用 addOperationWithBlock 提交的 block ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25609198/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com