框架没有按”在一个单独的线程上运行这些块。它只是在同一个运行循环中将它们作为附加事件运行。为了证明这一点,试试这个
[library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:[^(ALAssetsGroup * group, BOOL * stop)
{
if([NSThread isMainThread])
{
NSLog(@"main");
}
else
{
NSLog(@"non-main");
}
} copy]
failureBlock:^(NSError * err)
{NSLog(@"Erorr: %@", [err localizedDescription]);}];
[library release];
if([NSThread isMainThread])
{
NSLog(@"main");
}
else
{
NSLog(@"non-main");
}
我从这个输出是
main
main
main
含义,该块被称为主线程。这只是一个单独的事件。 为了解决您的问题,您只需在到达最后一步时从块内以某种方式返回您的值。你可以告诉它是最后一步,因为你的块将被调用nil作为组对象。
编辑:例如使用该块
^(ALAssetsGroup * group, BOOL * stop)
{
if(group == nil)
{
// we've enumerated all the groups
// do something to return a value somehow (maybe send a selector to a delegate)
}
}
你能告诉我更多关于该解决方案? – MatterGoal 2011-04-28 12:07:09