问题
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