Re: asynchronous nsurlconnection in nsoperation
Re: asynchronous nsurlconnection in nsoperation
- Subject: Re: asynchronous nsurlconnection in nsoperation
- From: Alex Zavatone <email@hidden>
- Date: Wed, 21 Mar 2012 14:17:27 -0400
Is this for iOS or Mac OS?
I've used Grand Central Dispatch and [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
The second example is synchronous.
There is also:
NSData *response_NSData = [NSURLConnection sendSynchronousRequest:my_NSURLRequest returningResponse:&my_NSURLResponse error:&my_NSError];
and
sendAsynchronousRequest:queue:completionHandler:
Loads the data for a URL request and executes a handler block on an operation queue when the request completes or fails.
+ (void)sendAsynchronousRequest:(NSURLRequest *)request queue:(NSOperationQueue *)queue completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))handler
Look up the above in the NSURLConnection Class Reference
And also this, but disregard the older stuff.
http://www.raywenderlich.com/4295/multithreading-and-grand-central-dispatch-on-ios-for-beginners-tutorial
Here is my GDC stuff.
- (void)viewDidLoad {
NSLog(@"In viewDidLoad");
[super viewDidLoad];
//NSString * const kRootURLString = @"http://10.6.2.137/";
NSURL *wordsURL = [NSURL URLWithString:@"http://localhost/~zav/Offices.txt?"];
NSURL *myUrl = [NSURL URLWithString:URLstring];
// dispatch_async(kBgQueue, ^{ NSData* data = [NSData dataWithContentsOfURL: myUrl]; // THIS IS WHAT YOU WANT Ariel **
dispatch_sync(kBgQueue, ^{ NSData* data = [NSData dataWithContentsOfURL: myUrl]; // doing a sync dispatch here - we want to wait for it to finish
[self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
});
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
...
- (void)fetchedData:(NSData *)responseData {
NSLog(@"In fetchedData");
NSLog(@" ");
// log URL data
NSString* newStr = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"%@", newStr);
}
Practice on a few simple cases until you find a method that you like. Note that I haven't checked my code for any leaks. This is old play code from 2 weeks ago.
On Mar 21, 2012, at 11:59 AM, Ariel Feinerman wrote:
> Hi,
>
> I wish to insert an asynchronous NSURLConnection into non-concurrent
> NSOperation
> the reason is to allow necessarily unarchive actions on the streaming date
> so
>
> - (void) connection: (NSURLConnection *) connection didReceiveData:
> (NSData*) data {
>
>
> // Append the new data to receivedData.
>
> [_receivedData appendData: data];
>
> if ([_receivedData length] >= MAX_CHUNK) {
>
> // unarchive
>
> // write to file
>
> // [receivedData setLength: 0];
>
> }
>
> }
>
> Is there a correct way to do ?
>
>
>
> --
> best regards
> Ariel
> _______________________________________________
>
> 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
- Alex Zavatone
_______________________________________________
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