Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 这是处理背景thred的正确方法吗?_Iphone_Objective C_Ios_Xcode_Nsoperationqueue - Fatal编程技术网

Iphone 这是处理背景thred的正确方法吗?

Iphone 这是处理背景thred的正确方法吗?,iphone,objective-c,ios,xcode,nsoperationqueue,Iphone,Objective C,Ios,Xcode,Nsoperationqueue,我向服务器发出了很多请求: NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; [theRequest setValue:@"gzip" forHTTPHeaderField:@"Accept-

我向服务器发出了很多请求:

    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    [theRequest setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"]; 

     urlData = [[NSMutableData data] retain];
    urlConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];

    if (urlConnection) 
    {
        finishedLoading = FALSE;
        while(!finishedLoading) {
            [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
        }
    }
    else 
    {
        [delegate swHttpConnection:self withRequest:urlString failedWithError:nil];
    }
…请求完成后,我会收到一个回调。但是,如果使用下面的代码,将不会调用选择器:

- (void)request:(MBRequest *)request finished:(NSMutableArray *)resultArray
{
    //Handle resultArray data 

    [self performSelector:@selector(someRandomFunction) withObject: nil afterDelay:1.0f]; 
}
…但是如果我使用下面的代码;它工作得很好:

- (void)request:(MBRequest *)request finished:(NSMutableArray *)resultArray
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0), ^(void) 
    {
        dispatch_async(dispatch_get_main_queue(), ^(void) 
        {
            //Handle resultArray data 

            [self performSelector:@selector(someRandomFunction) withObject: nil afterDelay:1.0f]; 
        }); 
    }); 
}
所以我的问题是双重的

  • 这样做对吗?我是否需要强制回调在mainthred上运行,或者代码的逻辑是错误的

  • 如果我必须强制在mainthred上运行回调,那么上面的代码正确吗

  • 此外,服务器请求代码有时会在以下时间崩溃:

    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    
    我在某些方面得到了这样的警告:

    Class _NSZombie_NSRunLoop is implemented in both ?? and ?? 
    

    提前感谢

    我不认为
    nsurlconnectionelegate
    有一个
    请求:finished:
    方法?通过查看您的代码,您可以使用它。如果是这样的话,我强烈认为您应该看看他们的网页,例如如何使用块执行此操作。

    我认为
    nsurlconnectionelegate
    没有
    请求:完成:
    方法?通过查看您的代码,您可以使用它。如果是这样的话,我强烈认为您应该看看他们的网页,例如如何使用块来完成此操作。

    当您更新此部分中的UI时

      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0), ^(void) 
        {
            dispatch_async(dispatch_get_main_queue(), ^(void) 
            {
                //Handle resultArray data 
    
                [self performSelector:@selector(someRandomFunction) withObject: nil afterDelay:1.0f]; 
            }); 
        }); 
    

    然后必须在UI线程中调用它,然后是,您必须在更新此段中的UI时执行此操作

      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0), ^(void) 
        {
            dispatch_async(dispatch_get_main_queue(), ^(void) 
            {
                //Handle resultArray data 
    
                [self performSelector:@selector(someRandomFunction) withObject: nil afterDelay:1.0f]; 
            }); 
        }); 
    

    然后必须在UI线程中调用它,然后是的,您必须这样做

    Pelase发布函数“update”的代码嗨,谢谢您的回复。update函数可以是任何东西。现在它只是一个空函数。我在里面放了一个NSLog,它从来没有被调用过“//Handle resultArray data”它是处理UI还是一些共享存储?是的,它更新了UI,包括一个tableView,并更新了一些本地存储。请发布函数“update”的代码。您好,感谢您的回复。update函数可以是任何东西。现在它只是一个空函数。我在其中放了一个NSLog,但它从未被调用“//Handle resultArray data”它是否处理UI或某个共享存储?是的,它更新了UI,包括一个tableView,并更新了一些本地存储