NSOperationQueue 在 ios4 上的主线程上执行,而不是在 io5 上执行?
是否有任何原因导致 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自此处:
因此,NSOperationQueue 总是在单独的线程上执行
From here:
So, NSOperationQueue always performs on a separate thread