alternative to NSURLConnection sendSynchronousRequest
Hi all, I'm building a client for a server with (apparently) weird redirect issues, I cannot get a proper HTTP response using NSURLConnection sendSynchronousRequest However, I can print out the response code using the asynchronous method: [[NSURLConnection alloc] initWithRequest:request delegate:self]; When I swap this method in, I can print out the status code just fine. However, because this approach is asynchronous my code calls this method and moves on without waiting for a return value. I need the request to complete before moving on. How can I force my code to wait for the asynchronous request to finish, without using sendSynchronousRequest? example: - (void)post{ NSLog(@"building batch request"); NSMutableURLRequest* request = [self requestWithMethod:POST]; NSLog(@"posting batch request"); // this method wraps NSURLConnection initWithRequest:request delegate:self [self sendAsynchronousRequest:request]; // should not be evaluated until sendAsynchronousRequest is complete NSLog(@"post finished"); } Many thanks! JB ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: alternative to NSURLConnection sendSynchronousRequest
Great suggestion! However, the post method itself sits inside a try-catch-finally statement: @implementation Controller - (void)sync{ @try { NSLog(@"sync has begun"); ... lots of setup, with potential to throw custom errors ... [batch post]; // can throw custom errors too } @catch (NSException* exception) { NSString* reason = [NSString stringWithFormat:@"%@", [exception reason]]; [self setSyncStatus:[NSString stringWithFormat:@"Sync failed: %@", reason]]; [self setSyncing:FALSE]; } @finally { if ([self isSyncing] == TRUE) { // if still syncing, sync wasn't interrupted by error; set sync status to success [self setSyncStatus:[NSString stringWithFormat:@"Last sync: %@", [self timestamp]]]; [self setSyncing:FALSE]; } [self postSyncFinishedNotification]; } } @end @implementation Batch ... - (void)post{ NSMutableURLRequest* request = [self requestWithMethod:POST]; // this method wraps NSURLConnection initWithRequest:request delegate:self [self sendAsynchronousRequest:request]; // should not be evaluated until sendAsynchronousRequest is complete NSLog(@"post finished"); } - (NSData*)sendAsynchronousRequest:(NSMutableURLRequest*)request{ NSURLConnection* connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; if (connection) { // do nothing, handled by delegate methods } else { @throw [self connectionException]; } return receivedData; } @end I did this to have one place to catch and process errors for the entire sync process, which includes errors that I throw upon receipt of responses containing non-2xx status codes (not shown here, but appears in the delegate method connection:didReceiveResponse:) This works with the synchronous approach, but not with the async approach because the rest of the try-catch-finally statement evaluates before the request completes. Hence the request for suggestions on forcing the sendAsynchronousRequest method to finish before moving on. I've looked into locking the thread with NSConditionLock, but that feels like overkill here. Might there be a more direct solution that I'm not seeing? Many thanks! JB On Tue, Jan 13, 2009 at 2:24 PM, Kevin Gessner wrote: > On Jan 13, 2009, at 4:27 PM, JB wrote: > > Hi all, >> >> I'm building a client for a server with (apparently) weird redirect >> issues, >> I cannot get a proper HTTP response using NSURLConnection >> sendSynchronousRequest >> >> However, I can print out the response code using the asynchronous method: >> [[NSURLConnection alloc] initWithRequest:request delegate:self]; >> >> When I swap this method in, I can print out the status code just fine. >> However, because this approach is asynchronous my code calls this method >> and >> moves on without waiting for a return value. I need the request to >> complete >> before moving on. >> >> How can I force my code to wait for the asynchronous request to finish, >> without using sendSynchronousRequest? >> > > You can split your post-connection stuff into another method, and call it > from the delegate's implementation of connectionDidFinishLoading:. Then it > will be called only when the connection is complete. > > HTH > -- Kevin > > Kevin Gessner > http://kevingessner.com > ke...@kevingessner.com > > > ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
newbie question: tangential compiler errors
Hi all, I've added Apple's sample LoginItemsAE class to autolaunch an app I've built for Tiger and Leopard. However, the app fails to compile when I add "LoginItemsAE.c" to the app target, throwing over 3000 "syntax" and "conflicting types" errors here: AppKit.h > Foundation.h > NSObjCRuntime.h I didn't get these errors beforehand, and they don't appear when LoginItemsAE is introduced to a fresh app. I'm hoping there's an easier way to isolate my infringing code than rebuilding my entire app with LoginItemsAE.c introduced from the get-go. Any pointers and suggestions would be much appreciated. Thanks, JB ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: newbie question: tangential compiler errors
I unchecked "Precompile Prefix Header" in the new target's build settings (per Chris's comments) and it compiles without error now. Thanks! JB On Mon, Oct 13, 2008 at 8:07 PM, Chris Hanson <[EMAIL PROTECTED]> wrote: > On Oct 13, 2008, at 12:42 PM, JB wrote: > > I've added Apple's sample LoginItemsAE class to autolaunch an app I've >> built >> for Tiger and Leopard. However, the app fails to compile when I add >> "LoginItemsAE.c" to the app target, throwing over 3000 "syntax" and >> "conflicting types" errors here: >> >> AppKit.h > Foundation.h > NSObjCRuntime.h >> > > You're probably doing this in a new target, which (due to limitations of > how Xcode creates new targets from templates) has AppKit.h set as its prefix > header. > > Using AppKit.h (which is Objective-C) as a prefix header doesn't guard > against inclusion in plain C or C++ source files, unfortunately. You'll > have to switch the target's prefix header to a local prefix that includes an > #ifdef __OBJC__/#endif around the #import of , one of which > was probably created for you when you created the project. > > -- Chris > > ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
variable sensitive to Active Build Configuration
Hi all, I'm building a small app that talks to a web service, so I would like use different URLs for the DEBUG vs. RELEASE targets (staging and production URLs, respectively). What is the best way to conditionally set a variable based on the Active Build Configuration? Many thanks! JB ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
newbie question: NSURLConnection delegation and "method instance"
Hi all, I'm struggling with "receivedData is declared as a method instance elsewhere" in this explanation of NSURLConnection delegation: http://developer.apple.com/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html When I declare the receivedData as NSMutableData in the delegate's header file, I get this warning from the compiler: "warning: local declaration of 'receivedData' hides instance variable". Where should I declare the receivedData variable so that it is available to all the delegation methods (connection:didReceiveResponse:, etc.), but won't be hidden by the local declaration above? Won't local declarations in each delegate method refer to different NSMutableData objects? Many thanks, JB ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]