Reputation:
I have to use Asynchrnous NSURLConnection inside NSOPeration in background mode,because its response is having large dataI have to avoid Apple's finite length coding to use in didEnterBackground.instead of it I use following code through NSOperation with NSInvocation as, but it is not working.connectToServer is having NSURLConnection operation.any help please?didReceiveData,didReceiveResponse delegate methods are not called?
-(void)viewDidLoad
{
NSOperationQueue *queue = [NSOperationQueue new];
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(connectServer)
object:nil];
[queue addOperation:operation];
[operation release];
[queue autorelease];
}
-(void)connectServer
{
NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSURLConnection *theConnection = [[[NSURLConnection alloc] initWithRequest:theRequest delegate:self] autorelease];
if( theConnection )
{
webData = [[NSMutableData data] retain];
}
else
{
NSLog(@"theConnection is NULL");
}
}
}
Upvotes: 0
Views: 1407
Reputation: 2862
mmmm maybe you could do the connection inside a block for the main queue with this:
dispatch_async(dispatch_get_main_queue(), ^{
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:10.0];
_connection = [[NSURLConnection alloc] initWithRequest:request startImmediately:YES];
[request release];
});
and then the delegate methods should get called.
Upvotes: 2
Reputation: 2317
Whenever you want run NSURLConnection on secondary thread , you need to add that connection to runloops
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url
cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:10.0];
_connection = [[NSURLConnection alloc] initWithRequest:request delegate:self
startImmediately:YES];
[request release];
[_connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[_connection start];
[pool release];
Upvotes: 1
Reputation: 12405
I might be wrong about this, but still wil try...
See the docs say... for start
method
start Causes the connection to begin loading data, if it has not already.
- (void)start Discussion Calling this method is necessary only if you create a connection with the initWithRequest:delegate:startImmediately: method and provide NO for the startImmediately parameter. If you don’t schedule the connection in a run loop or an operation queue before calling this method, the connection is scheduled in the current run loop in the default mode.
so it seems to me you will have to the start the connection manually as it is inside an operation queue. correct me if I am wrong.
Upvotes: 0