• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
How to wait for methods with result/completion blocks to finish?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How to wait for methods with result/completion blocks to finish?


  • Subject: How to wait for methods with result/completion blocks to finish?
  • From: Chris Markle <email@hidden>
  • Date: Mon, 07 Mar 2011 21:00:33 -0800

This is on iOS... Say I have use a method that has some kind of
result/completion block like ALAssetsLibrary assetForURL In the
example below, in the assetForURL result block, the JPEG
representation of the asset is read into a buffer and the NSData form
of that buffer is assigned to a property. I need the property to be
set before my code continues past his point. How do I go about
accomplishing that? Am I forced to show a "waiting for xxx" view until
this finishes?

I tried surrounding the assetForURL call with a semaphore but this
does not work on the main app thread (the dispatch_semaphore_wait()
blocks the main thread and thus the dispatch_semaphore_signal() in the
resultBlock never gets to run to signal that the block is done).

I guess in general I an wondering how I correctly wait for things to
happen that I absolutely positively need to be done before I
continue...

Thanks in advance...

Chris

* * *

-(void)getJPEGFromAssetForURL:(NSURL *)url {
	ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
	[assetslibrary assetForURL:url
            resultBlock: ^(ALAsset *myasset) {
                ALAssetRepresentation *rep = [myasset defaultRepresentation];
                Byte *buf = malloc([rep size]);
                NSError *err = nil;
                NSUInteger bytes = [rep getBytes:buf fromOffset:0LL
length:[rep size] error:&err];
                if (err || bytes == 0) {
                    [...]
                }
                self.imageJPEG = [NSData dataWithBytesNoCopy:buf
length:[rep size] freeWhenDone:YES];
            }
            failureBlock: ^(NSError *err) {
                NSLog(@"can't get asset %@: %@", url, err);
            }];
	[assetslibrary release];
}
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: How to wait for methods with result/completion blocks to finish?
      • From: Kyle Sluder <email@hidden>
  • Prev by Date: NSXMLParserDelegateAbortedParseError
  • Next by Date: Re: Crash with extra [CFString release] after changing a bound value in NSTextField
  • Previous by thread: Fwd: NSXMLParserDelegateAbortedParseError
  • Next by thread: Re: How to wait for methods with result/completion blocks to finish?
  • Index(es):
    • Date
    • Thread