How to wait for methods with result/completion blocks to finish?
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