试图理解调度
Trying to understand dispatching
我正在努力了解 Objective-C 的调度机制。但是不要成功。
我有这两个方法:
- (void)downloadImage
{
NSString * URLString= @"http://www.something.com";
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^{
NSURL *imageURL = [NSURL URLWithString:fullURL];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
theImportant.image = [UIImage imageWithData:imageData];
dispatch_sync(dispatch_get_main_queue(), ^{
stopAnimate=true;
[self MoveToPosition:myView.center];
});
});
}
和:
-(void)MoveToPosition:(CGPoint)position{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[UIView animateWithDuration:1.5
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut |UIViewAnimationOptionBeginFromCurrentState
animations:^{
myView.center=position;
}
completion:^(BOOL finished){
if (stopAnimate){
//do Nothing
} else if (someFlag){
[self downloadImage];
}
}
];
}
所以,第一个方法被第二个方法调用,第二个方法又调用第一个方法。
一切正常,只是 theImportant.image 不显示。
当我移动
theImportant.image = [UIImage imageWithData:imageData];
在
内
dispatch_sync
downloadImage 块,动画开始以意外方式运行。
我确实知道我对调度不是很了解,但我希望你的见解能增长我的智慧。
重要的定义为
@属性(强,原子)IBOutlet UIImageView * theImportant;
简而言之,我的问题是:为什么我的 theImportant.image 没有显示。
我最后添加了
@property (strong, atomic) UIImage * theImportantImage;
到我的 .h 文件,将图像设置为该文件,然后我就这样做了
theImportantFace.image=theImportantImage.
不确定为什么会这样,但是,嘿,确实如此。看来我的问题根本与异步内容无关。可怜。不满意不查是什么问题
dispatch_xxx...方法不调用任何东西。他们获取一段代码并将其添加到队列中。这就像您在一张纸上写了一些说明,然后将那张纸放在同事的办公桌上一样。你的同事会完成他一直在做的任何事情,当他完成后,他会按照纸上写的去做。
有一种特殊队列,称为"global queue"。如果你在 "global" 办公桌上放一张纸,有五打同事,任何人都可以随时拿起那张纸,所以工作会做得更快(因为很多人工作在不同的任务上),但你永远不知道它们是按什么顺序完成的。
我正在努力了解 Objective-C 的调度机制。但是不要成功。
我有这两个方法:
- (void)downloadImage
{
NSString * URLString= @"http://www.something.com";
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^{
NSURL *imageURL = [NSURL URLWithString:fullURL];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
theImportant.image = [UIImage imageWithData:imageData];
dispatch_sync(dispatch_get_main_queue(), ^{
stopAnimate=true;
[self MoveToPosition:myView.center];
});
});
}
和:
-(void)MoveToPosition:(CGPoint)position{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[UIView animateWithDuration:1.5
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut |UIViewAnimationOptionBeginFromCurrentState
animations:^{
myView.center=position;
}
completion:^(BOOL finished){
if (stopAnimate){
//do Nothing
} else if (someFlag){
[self downloadImage];
}
}
];
}
所以,第一个方法被第二个方法调用,第二个方法又调用第一个方法。
一切正常,只是 theImportant.image 不显示。
当我移动
theImportant.image = [UIImage imageWithData:imageData];
在
内dispatch_sync
downloadImage 块,动画开始以意外方式运行。
我确实知道我对调度不是很了解,但我希望你的见解能增长我的智慧。
重要的定义为
@属性(强,原子)IBOutlet UIImageView * theImportant;
简而言之,我的问题是:为什么我的 theImportant.image 没有显示。
我最后添加了
@property (strong, atomic) UIImage * theImportantImage;
到我的 .h 文件,将图像设置为该文件,然后我就这样做了
theImportantFace.image=theImportantImage.
不确定为什么会这样,但是,嘿,确实如此。看来我的问题根本与异步内容无关。可怜。不满意不查是什么问题
dispatch_xxx...方法不调用任何东西。他们获取一段代码并将其添加到队列中。这就像您在一张纸上写了一些说明,然后将那张纸放在同事的办公桌上一样。你的同事会完成他一直在做的任何事情,当他完成后,他会按照纸上写的去做。
有一种特殊队列,称为"global queue"。如果你在 "global" 办公桌上放一张纸,有五打同事,任何人都可以随时拿起那张纸,所以工作会做得更快(因为很多人工作在不同的任务上),但你永远不知道它们是按什么顺序完成的。