Re: please help with this handler issue
Re: please help with this handler issue
- Subject: Re: please help with this handler issue
- From: Uli Kusterer <email@hidden>
- Date: Thu, 29 Aug 2013 13:40:52 +0200
On Aug 29, 2013, at 5:18 AM, Rick C. <email@hidden> wrote:
> [request2 performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
>
> if (responseData) {
>
> NSDictionary *user =
> [NSJSONSerialization JSONObjectWithData:responseData
> options:NSJSONReadingAllowFragments
> error:NULL];
>
> NSString *profileImageUrl = [[user objectForKey:@"profile_image_url"] stringByReplacingOccurrencesOfString:@"_normal" withString:@""];
>
> NSData *imageData =
> [NSData dataWithContentsOfURL:
> [NSURL URLWithString:profileImageUrl]];
>
> image = [UIImage imageWithData:imageData];
>
> UIGraphicsBeginImageContext(CGSizeMake(128 ,128));
> [image drawInRect:CGRectMake(0, 0, 128, 128)];
>
> result = UIGraphicsGetImageFromCurrentImageContext();
> UIGraphicsEndImageContext();
>
> }
> }];
>
> // new way:
> while (result == nil)
> {
> [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];
> }
>
> UIImage *OrigImage = result;
> UIImage *mask = [UIImage imageNamed:@"email@hidden"];
> UIImage *maskedImage = [UIImage maskImage:OrigImage withMask:mask];
>
> return maskedImage;
>
> }
>
> But this is an unreliable way to do this I know because I have problems. Can someone please help me with the right way? Because I need for it to wait before advancing or else it will cause problems in following code. Really appreciate the time thanks!
You're not very clear about what and why it is unreliable, but I guess you're talking about the performRequestWithHandler call, which is asynchronous. Running the run loop is a bad idea. What you should do instead is make an async design like Graham suggested. My favorite solution is to use blocks:
1) Put a placeholder image into your prefs (like the "silhouette of a head" Apple typically uses for users)
2) Kick of the request with the handler block.
3) Move the code that generates maskedImage into the handler block for the request.
4) Change the method to -(void)userImage:(NSString*)name withAccount:(id)account completionHandler: (void (^)(UIImage* maskedImage))doneHandler and give it a block (remember, your code has to copy the block so it's still there when the request returns!) which does the prefs setObject: call. Call that block from the request's handler block, passing out the image that way.
Cheers,
-- Uli Kusterer
"The Witnesses of TeachText are everywhere..."
http://www.zathras.de
_______________________________________________
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