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
Ios 在块中使用异步调度_Ios_Objective C_Iphone_Parse Platform - Fatal编程技术网

Ios 在块中使用异步调度

Ios 在块中使用异步调度,ios,objective-c,iphone,parse-platform,Ios,Objective C,Iphone,Parse Platform,当我使用块时,使用dispatch\u async在主线程上调用UI更新是更好的做法吗?例如: PFFile *image = (PFFile *)[currentUser objectForKey:@"image"]; [image getDataInBackgroundWithBlock:^(NSData *data, NSError *error){ if (error) { self.profileImage.ima

当我使用块时,使用dispatch\u async在主线程上调用UI更新是更好的做法吗?例如:

    PFFile *image = (PFFile *)[currentUser objectForKey:@"image"];
    [image getDataInBackgroundWithBlock:^(NSData *data, NSError *error){
        if (error)
        {
            self.profileImage.image = [UIImage imageNamed:@"image"];

        }
        else
        {
            UIImage *userImage = [UIImage imageWithData:data];
            self.profileImage.image = userImage;
        }
    }];

如果我将“self.profileImage.image=userImage;”放在else条件中的dispatch async中会更好吗?还是因为它是一个块,按原样被称为async

如果设置
self.profileImage.image
会立即更改用户希望看到的UI,则应在主队列上设置该属性。如果只是设置与UI不直接相关的内部数据,则可以在后台队列上继续执行。因此,如果要更新主队列上的配置文件映像,可以使用

dispatch_async(dispatch_get_main_queue(), ^(void){
    self.profileImage.image = [UIImage imageWithData:data];
}

您需要在主线程中执行图像加载操作

self.profileImage.image=userImage

上面这一行是UI操作,因为它位于一个块中,所以加载并使UI交互停止需要一些时间

您只需在主线程中调用此行:

PFFile *image = (PFFile *)[currentUser objectForKey:@"image"];
[image getDataInBackgroundWithBlock:^(NSData *data, NSError *error){
    if (error)
    {
        self.profileImage.image = [UIImage imageNamed:@"image"];

    }
    else
    {
        [self performSelectorOnMainThread:@selector(loadImage:) withObject:data waitUntilDone:YES];

    }
}];


- (void) loadImage:(NSData *)data {
            UIImage *userImage = [UIImage imageWithData:data];
            self.profileImage.image = userImage;
}

UIImage操作不是UI操作。UIImageView操作是,但是,应该在主线程上执行。@jshier:同意,我忘了提到这一部分。编辑回答:我为什么会得到这样的回答:“一个长时间运行的操作正在主线程上执行。在WarnBlockingOperationMainThread()上中断以进行调试。”从现在开始?现在似乎正在发生。这意味着您已经锁定了应用程序的主线程,可能是通过某种形式的。