我有一个方法,有时可以在我的代码中调用.下面是一个非常基本的示例,因为代码处理iphone照片库中的图像和文件,并在完成该方法时标记它们已经处理过.
@property (nonatomic, assign) dispatch_queue_t serialQueue;
....
-(void)processImages
{
dispatch_async(self.serialQueue, ^{
//block to process images
NSLog(@"In processImages");
....
NSLog(@"Done with processImages");
});
}
Run Code Online (Sandbox Code Playgroud)
我认为每次调用此方法时我都会得到以下输出..."在processImages""使用processImages完成""在processImages""完成了processImages"等...
但我总是得到
"在processImages""在processImages""完成与processImages""完成与processImages"等...
我以为串口队列会等到第一个块完成,然后开始.对我来说,似乎它正在启动方法,然后它再次被调用并在第一个调用完成之前启动,创建通常不会被处理的图像的重复,因为如果它真的按顺序执行,则该方法将知道它们已经处理完毕.也许我对串行队列的理解并不具体.有什么输入?谢谢.
编辑:更多上下文,这是块中发生的事情...这可能导致问题???
@property (nonatomic, assign) dispatch_queue_t serialQueue;
....
-(void)processImages
{
dispatch_async(self.serialQueue, ^{
//library is a reference to ALAssetsLibrary object
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
{
....
//Process the photos here
}];
failureBlock:^(NSError *error) { NSLog(@"Error loading images from library");
}];
});
}
-(id)init
{
self …Run Code Online (Sandbox Code Playgroud)