Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.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
Objective c 我的dispatch_async用于重载数据有什么问题_Objective C_Grand Central Dispatch - Fatal编程技术网

Objective c 我的dispatch_async用于重载数据有什么问题

Objective c 我的dispatch_async用于重载数据有什么问题,objective-c,grand-central-dispatch,Objective C,Grand Central Dispatch,使用webservices C#,我想显示有关产品的信息。为了加载数据,我使用dispatch_async作为队列执行,如下所示: dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ //Show loading [ShareAppDelegate showloading]; //LOAD PRODUCT INFO productDatasources =

使用webservices C#,我想显示有关产品的信息。为了加载数据,我使用dispatch_async作为队列执行,如下所示:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    //Show loading
    [ShareAppDelegate showloading];
    //LOAD PRODUCT INFO
    productDatasources = [self loadProductInfo];
    //LOAD COMMENT
    commentDatasources = [self loadComment];
    //LOAD PRODUCT PHOTOS
    photoDatasources = [self loadPhotos];

    dispatch_async(dispatch_get_main_queue(), ^{
        [ShareAppDelegate hideloading];
        //UPDATE PRODUCT INFO TO UI
        [self showProductInfoWithDatasource:productDatasources];
        //UPDATE COMMENT INFO TO UI
        [self showCommentWithDatasource:commentDatasources];
        //UPDATE PHOTO INFO TO UI
        [self showProductPhotoWithDatasource:photoDatasources];
     });
});
但有时它工作正常,我的意思是,我的产品信息,评论和照片可以加载并显示在我的屏幕上。但有时,只能加载ProductInfo,无法加载评论和照片并获取空值。有时,3是空的

我发现,在我测试过的所有情况下,服务器总是正确返回结果,结果不为空。但我的dispatch_get_main_队列似乎在完全接收数据之前执行

有人知道我的情况吗。调度异步是否合适。请提前通知我

编辑:

我找到了解决这个问题的方法。因为在另一个视图中,我声明并启动了NSTimer来调用另一个webservices。此Web服务每5秒运行一次,以通知用户是否有来自服务器的任何通知

timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(loadNotification:) userInfo:nil repeats:YES];

因此,在这种情况下,有时我调用了两个Web服务并同时接收数据,这就是GCD无法正确接收数据的原因。我是否可以更新通知并仍然正确执行上述GCD???

您在后台线程中分配的属性(productDataSources、commentDataSources、photoDataSources),它们是如何声明的?它们在其他地方可以访问吗?在这种情况下,可能是出了什么问题

主线程使用的属性决不能被后台线程更改,我怀疑这里正在发生这种情况。相反,在后台线程上使用临时变量,然后在返回主线程时更改实际变量,如下所示:

//Show loading on main thread (important)
[ShareAppDelegate showloading];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    //LOAD PRODUCT INFO
    NSArray *tempProductDatasources = [self loadProductInfo];
    //LOAD COMMENT
    NSArray *tempCommentDatasources = [self loadComment];
    //LOAD PRODUCT PHOTOS
    NSArray *tempPhotoDatasources = [self loadPhotos];

    dispatch_async(dispatch_get_main_queue(), ^{
        // Set the temp variables when on the main thread
        productDatasources = tempProductDatasources;
        // ... etc

        [ShareAppDelegate hideloading];
        //UPDATE PRODUCT INFO TO UI
        [self showProductInfoWithDatasource:productDatasources];
        //UPDATE COMMENT INFO TO UI
        [self showCommentWithDatasource:commentDatasources];
        //UPDATE PHOTO INFO TO UI
        [self showProductPhotoWithDatasource:photoDatasources];
     });
});

尝试移动[ShareAppDelegate showloading];在调度之前,请异步或将其放入主线程(我假设它与UI一起工作)。我刚刚尝试过这样做,但仍然会遇到相同的问题。您确定在调用主队列时,您拥有产品、评论和照片数据吗?这就是我使用GCD加载大量数据的原因。确保主队列中有我的产品、评论和照片数据。事实上,我所做的一切都与您的代码相同。你能帮我解决我上面编辑过的问题吗?我想既然NSTimer在主线程上启动,你需要在收到这样一个通知后将一个新的后台更新排队。正确,在我的NSTimer中,我使用另一个GCD加载数据,并获取主队列来更新UI。