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

发布于 2024-12-16 20:07:22 字数 2005 浏览 1 评论 0原文

是否有任何原因导致 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];

来自 PictureCacheManager:

- (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: 方法。 在我调用它之前,我实际上处于一个单独的线程中,但在它通过之后,它切换到主线程......

Is there any reason why an NSOperationQueue is performing on the main thread on ios 4.2.1 and runing on a seperate thread (as expected) on ios 5.0.1.

I'm using an NSOperationQueue to perfom a busy operation on a big amount of pictures (ALassets) on the launch of my app but it's runing on the main thread on ios4 and not on ios5...

Here is a piece of code

from the main thread :

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

From PictureCacheManager :

- (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]);
                     }];

}

UPDATE : It seems that the matter comes from the enumerateGroupsWithTypes:usingBlock:failureBlock: method.
Before I call it I'm actually in a seperate thread but after it goes thru it switches to the main thread...

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

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

发布评论

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

评论(1

携余温的黄昏 2024-12-23 20:07:22

来自此处

在iOS中,操作队列不使用Grand Central Dispatch来执行
运营。 它们为非并发操作创建单独的线程
并从当前线程启动并发操作。对于一个
讨论并发和非并发的区别
操作及其执行方式,请参阅 NSOperation 类参考。

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

From 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.

So, NSOperationQueue always performs on a separate thread

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