ios - block 的执行总是延迟
<p><p>我对 IOS 很陌生。我想在 <code>viewDidLoad</code> 中获取设备上的所有图片。但是该 block 总是在我在代码中调用 <code>NSLog(@"%d", photos.count)</code> 之后执行,如下所示。这种情况如何处理?</p>
<pre><code>__block NSMutableArray* photos = [[ init] retain];
ALAssetsLibrary *library = [ init];
void (^assertsEnumerator)(ALAsset *, NSUInteger , BOOL *) =^(ALAsset *result, NSUInteger index, BOOL *stop)
{
if(result)
{
NSLog(@"Assert name : %@", );
]];
}
};
void (^groupEnumerator)(ALAssetsGroup*, BOOL*) = ^(ALAssetsGroup *group, BOOL *stop) {
if(group != nil) {
NSLog(@"Adding group %@", );
;
}
};
[library enumerateGroupsWithTypes: ALAssetsGroupSavedPhotos
usingBlock:groupEnumerator
failureBlock:^(NSError * err) {NSLog(@"Erorr: %@", );}];
;
NSLog(@"%d", photos.count);
;
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>作为 <a href="https://developer.apple.com/library/ios/#documentation/AssetsLibrary/Reference/ALAssetsLibrary_Class/Reference/Reference.html" rel="noreferrer noopener nofollow">ALAssetsLibrary documentation</a>状态,<code>enumerateGroupsWithTypes:usingBlock:failureBlock:</code> 是异步的,所以不是在原地调用,而是计划在下一个运行循环周期内调用。文档清楚地说明了这是什么原因以及您应该如何进行:</p>
<blockquote>
<p>This method is asynchronous. When groups are enumerated, the user may be asked to confirm the application's access to the data; the method, though, returns immediately. You should perform whatever work you want with the assets in <em>enumerationBlock</em>.</p>
</blockquote></p>
<p style="font-size: 20px;">关于ios -block 的执行总是延迟,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/9478456/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/9478456/
</a>
</p>
页:
[1]