NSOperationQueue 操作优先级

发布于 2024-11-19 18:14:36 字数 473 浏览 3 评论 0原文

我的操作有问题。当我向 NSOperationQueue 添加操作并且它已执行时,我有一个添加到数组结果的方法。这是我的方法:

- (void)loadPostImageWithDictionary:(NSDictionary*)dict
{
NSData *tmpImageData = [[NSData alloc] initWithContentsOfURL:
                        [NSURL URLWithString: [dict   
    objectForKey:@"picture"]]];
UIImage *image=[[UIImage alloc] initWithData:tmpImageData]; 
[userPostImage addObject:image];
[image release];
}

如果某些操作已提前执行 - 将结果添加到数组中。但我需要按照我添加操作的顺序执行操作。请帮忙..

I have a problem with operations. When I add operations to NSOperationQueue and it's had executed I have a method that add to array result. This is my method:

- (void)loadPostImageWithDictionary:(NSDictionary*)dict
{
NSData *tmpImageData = [[NSData alloc] initWithContentsOfURL:
                        [NSURL URLWithString: [dict   
    objectForKey:@"picture"]]];
UIImage *image=[[UIImage alloc] initWithData:tmpImageData]; 
[userPostImage addObject:image];
[image release];
}

And if some operation have executed early - result adding to array. But I need that the operations were performed in the order of which I added them. Please help..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

韶华倾负 2024-11-26 18:14:36

使用 [NSOperationQueue setMaxConcurrentOperationCount:1] 方法一次只允许运行一个操作,从而有效地在另一个线程上创建一个串行队列。

如果您的代码依赖于以特定顺序运行的操作(在多线程中,这首先并不是一个很好的设计),那么为什么不将一组字典传递给单个操作,该操作会下载所有图像并在委托方法中返回图像数组?

use [NSOperationQueue setMaxConcurrentOperationCount:1] method to only allow one operation to run at a time, effectively making a serial queue on another thread.

if your code depends on operations running in a specific order (which, in multithreading, isn't really good design in the first place), when why don't you just pass an array of dictionaries to a single operation, that downloads all the images and returns the array of images in a delegate method?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文