How to change int value in block?

删除回忆录丶 提交于 2019-12-24 02:14:26

问题


How change int value in block, I have this :

__block long long size = -1;
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{

    ALAssetRepresentation * rep = [myasset defaultRepresentation];
    size = [rep size];
    //here showed normal value
    NSLog(@"needed size : %lld",size);
};
ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
[assetslibrary assetForURL:self.tmpVideoURL 
               resultBlock:resultblock
              failureBlock:nil];

//but here remaind -1
NSLog(@"out block value : %lld",size);

回答1:


The problem is that you're sending that block off to be executed sometime later, after the assetForURL:... method has done its work, which it's doing asynchronously. It's most likely on a background thread or queue, allowing the method itself to return immediately while the work continues.

So the method assetForURL:... returns before your resultBlock has run, meaning the value hasn't been changed yet, by the time you get to the second NSLog. Everything's working fine; you're just checking the value too early.



来源:https://stackoverflow.com/questions/9997489/how-to-change-int-value-in-block

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!