gpt4 book ai didi

objective-c - NSOperationQueue 在 ios4 的主线程上执行,而不是在 io5 上执行?

转载 作者:行者123 更新时间:2023-11-29 13:44:07 26 4
gpt4 key购买 nike

NSOperationQueue 在 ios 4.2.1 的主线程上执行并在 ios 5.0.1 的单独线程(如预期)上运行是否有任何原因。

我正在使用 NSOperationQueue 在启动我的应用程序时对大量图片 (ALassets) 执行繁忙的操作,但它在 ios4 的主线程上运行,而不是在 ios5 上...

这是一段代码

来自主线程:

    cacheManager = [[PicturesCacheManager alloc] initWithDelegate:self];
//start the generation of the pictures array
NSLOG(@"setUpToDatePicturesArray");
operationQueue = [[NSOperationQueue alloc] init];
[operationQueue addOperation:cacheManager];
[cacheManager release];

来自图片缓存管理器:

- (id)initWithDelegate:(id <CachePicturesProtocol> )delegatePic
{
self = [super init];
if (self) {
// Initialization code here.
[self setDelegate:delegatePic];
}

return self;
}

-(void)main{
[self loadCurrentCameraRoll]; //do lot of stuff
}

-(void)loadCurrentCameraRoll{
//NSLOG(@"loadCurrentCameraRoll");
// photos
void (^assetEnumerator)( ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
//photos only!
if(result != NULL && [[result valueForProperty:@"ALAssetPropertyType"] isEqualToString:@"ALAssetTypePhoto"]) {

//Do stuff
}
};

void (^assetGroupEnumerator)( ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop) {
if(group != nil) {
NSLOG(@"current group = %@", group);
[group enumerateAssetsUsingBlock:assetEnumerator];
}
else{
//we've enumerated all the groups

}
};

assetsList = [[NSMutableArray alloc] init];
ALAssetsLibrary *library = [[[ALAssetsLibrary alloc] init] autorelease];

[library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:assetGroupEnumerator
failureBlock: ^(NSError *error) {
NSLOG(@"Failure : %@", [error description]);
}];

}

更新:这件事似乎来自 enumerateGroupsWithTypes:usingBlock:failureBlock: 方法。在我调用它之前,我实际上在一个单独的线程中,但在它通过后切换到主线程......

最佳答案

来自 here :

In iOS, operation queues do not use Grand Central Dispatch to execute operations. They create separate threads for non-concurrent operations and launch concurrent operations from the current thread. For a discussion of the difference between concurrent and non-concurrent operations and how they are executed, see NSOperation Class Reference.

因此,NSOperationQueue 总是在单独的线程上执行

关于objective-c - NSOperationQueue 在 ios4 的主线程上执行,而不是在 io5 上执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8168977/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com