Igor Bidiniuc
Igor Bidiniuc

Reputation: 1560

How to change int value in block?

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);

Upvotes: 3

Views: 839

Answers (1)

jscs
jscs

Reputation: 64022

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.

Upvotes: 7

Related Questions