Re: NSURLConnection unhappiness

2009-05-04 Thread Nick Hristov
Thank you all for your suggestions. I will use the runloop to wait on
response.

One more comment on self-ivar... I used this approach because using [self
connection] or self.connection (they are both the same thing) amount to an
extra message call.

Nick

On Thu, Apr 30, 2009 at 11:09 AM, Jeff Johnson 
publicpost...@lapcatsoftware.com wrote:

 On Apr 30, 2009, at 12:53 AM, Kyle Sluder wrote:

  On Thu, Apr 30, 2009 at 1:44 AM, Jeff Johnson
 publicpost...@lapcatsoftware.com wrote:

 On an unrelated note, your use of self-connection, etc., is
 non-standard
 and not advised. You should be using direct ivar access connection,
 properties self.connection, or accessor methods [self connection].


 Sure, `self-connection` is redundant with just plain old
 `connection`, but they amount to the same thing.  Unless there's some
 new-runtime trickery going on that I'm not aware of.

 --Kyle Sluder


 It amounts to the same thing, but it's a bad habit to get into. It's
 redundant, as you say, for an object's own instance variables. And you
 shouldn't be trying to directly access the instance variables of other
 objects. According to the documentation, Marking instance variables @public
 defeats the ability of an object to hide its data. It runs counter to a
 fundamental principle of object-oriented programming—the encapsulation of
 data within objects where it’s protected from view and inadvertent error.
 Public instance variables should therefore be avoided except in
 extraordinary cases. Thus, except in extraordinary cases, there's no reason
 to use the object-ivar syntax.

 -Jeff


___

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: NSURLConnection unhappiness

2009-05-04 Thread Jeff Johnson
Yes, those result in a message call, though it's a very fast message  
call. Within your instance methods, however, you can just access the  
ivar directly, e.g, connection without a message call and without  
self-connection.


-Jeff


On May 1, 2009, at 11:00 AM, Nick Hristov wrote:


Thank you all for your suggestions. I will use the runloop to wait on
response.

One more comment on self-ivar... I used this approach because using  
[self
connection] or self.connection (they are both the same thing) amount  
to an

extra message call.

Nick

On Thu, Apr 30, 2009 at 11:09 AM, Jeff Johnson 
publicpost...@lapcatsoftware.com wrote:


On Apr 30, 2009, at 12:53 AM, Kyle Sluder wrote:

On Thu, Apr 30, 2009 at 1:44 AM, Jeff Johnson

publicpost...@lapcatsoftware.com wrote:


On an unrelated note, your use of self-connection, etc., is
non-standard
and not advised. You should be using direct ivar access  
connection,
properties self.connection, or accessor methods [self  
connection].




Sure, `self-connection` is redundant with just plain old
`connection`, but they amount to the same thing.  Unless there's  
some

new-runtime trickery going on that I'm not aware of.

--Kyle Sluder



It amounts to the same thing, but it's a bad habit to get into. It's
redundant, as you say, for an object's own instance variables. And  
you
shouldn't be trying to directly access the instance variables of  
other
objects. According to the documentation, Marking instance  
variables @public
defeats the ability of an object to hide its data. It runs counter  
to a
fundamental principle of object-oriented programming—the  
encapsulation of
data within objects where it’s protected from view and inadvertent  
error.

Public instance variables should therefore be avoided except in
extraordinary cases. Thus, except in extraordinary cases, there's  
no reason

to use the object-ivar syntax.

-Jeff


___

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: NSURLConnection unhappiness

2009-05-04 Thread Alexander Spohr


Am 01.05.2009 um 18:00 schrieb Nick Hristov:

One more comment on self-ivar... I used this approach because using  
[self
connection] or self.connection (they are both the same thing) amount  
to an

extra message call.


Then just use ivar, without self-
It amounts to the same (no call) but looks right

atze

___

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: NSURLConnection unhappiness

2009-05-04 Thread Kyle Sluder
On Fri, May 1, 2009 at 12:00 PM, Nick Hristov n...@freshlybakedapps.com wrote:
 One more comment on self-ivar... I used this approach because using [self
 connection] or self.connection (they are both the same thing) amount to an
 extra message call.

This is true, and sometimes you want to do this, while other times you
don't.  But anywhere you do self-foo you can simply do foo, assuming
foo isn't shadowed by another variable identifier in the same scope.
Properties, methods, and variables (including ivars) all exist in
separate namespaces.

--Kyle Sluder
___

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: NSURLConnection unhappiness

2009-04-30 Thread Richard Frith-Macdonald


On 30 Apr 2009, at 05:31, Nick Hristov wrote:


Hello,

I am currently working on a small iphone app, and I am facing some
difficulty with getting NSURLConnection to ... connect. Basically my  
unit

tests don't pass: connection is never made.

Here is a snippet of the non-working code:

code

- (id) initWithURL: (NSURL*) someurl
{
   self = [super init];
   if (self != nil) {
   self-url = [someurl copy];
   NSURLRequest * request = [NSURLRequest requestWithURL:self-url
cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 6.0];
   NSLog([NSString stringByAppendingStrings:@Request:  ,  
[request

description], nil]);
   self-finished = NO;
   self-connection = [[NSURLConnection alloc]
initWithRequest:requestdelegate:self startImmediately:NO];
   }
   return self;
}

- (void) downloadData
{
   NSLog([self-connection description]);
   NSLog(@run, b*$#4rd, run...);
   [self-connection scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
   [self-connection start];
   int timeout = 30;
   int i = 0;
   while(self-finished == NO  [self _deletegateTerminateCheck] ==  
NO) {

   // suspend thread
   [NSThread sleepForTimeInterval: 1.0];


I expect the above line is the problem ... if you suspend the thread  
then it presumably can't connect to the remote system.
You would probably need to repeatedly run the current runloop using  
the -runMode:BeforeDate: method instead of suspending the thread.


___

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: NSURLConnection unhappiness

2009-04-30 Thread Stephen J. Butler
On Wed, Apr 29, 2009 at 11:31 PM, Nick Hristov
n...@freshlybakedapps.com wrote:
 - (id) initWithURL: (NSURL*) someurl
 {
self = [super init];
if (self != nil) {
self-url = [someurl copy];
NSURLRequest * request = [NSURLRequest requestWithURL:self-url
 cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 6.0];
NSLog([NSString stringByAppendingStrings:@Request:  , [request
 description], nil]);

Ugg. Don't do this, please. NSLog accepts as its first argument a
printf style format string. Also, '%@' gets replaced with [aObject
description]. So all you really need is:

NSLog( @Request: %@, request );

self-finished = NO;
self-connection = [[NSURLConnection alloc]
 initWithRequest:requestdelegate:self startImmediately:NO];
}
return self;
 }

 - (void) downloadData
 {
NSLog([self-connection description]);

Please don't do this either. If [connection description] happens to
contain a '%' then your program will crash. The proper way is to
ALWAYS specify a format string:

NSLog( @%@, connection );

NSLog(@run, b*$#4rd, run...);
[self-connection scheduleInRunLoop:[NSRunLoop currentRunLoop]
 forMode:NSDefaultRunLoopMode];
[self-connection start];
int timeout = 30;
int i = 0;
while(self-finished == NO  [self _deletegateTerminateCheck] == NO) {

You probably need to mark 'finished' as volatile in your interface
definition for this to work. Otherwise the assembly generated could
cause your loop to never exit.

// suspend thread
[NSThread sleepForTimeInterval: 1.0];

Yeah, as others have said, you need to run the runloop. Otherwise the
connection can't do any work.

i++;

/// stupid, but effective safety measure:
if(i = timeout) {
break;
}
}
 }

 - (NSData *) data {
return self-data;
 }

 #pragma mark NSConnectionDelegate implementation
 - (NSURLRequest *)connection:(NSURLConnection *)connection
 willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse
 *)response
 {
NSLog(@Will send request);
return request;
 }


 - (void)connection:(NSURLConnection *)connection
 didReceiveResponse:(NSURLResponse *)response
 {
NSLog(@Did get response);
NSString * encoding = [response textEncodingName];
self-dataEncoding = [self getEncodingForEncodingName: encoding];
self-mimeType = [response MIMEType];

You need to retain the values here.

 }

 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData
 *)receiveddata
 {
NSLog(@Did get data);
if([self _deletegateTerminateCheck]) {
if(self-data != nil) {
[self-data release];
self-data = nil;
}
[self-connection cancel];
[self-connection release];
self-connection = nil;
return;
}
if(self-data == nil) {
self-data = [[NSMutableData alloc]init];
}
[self-data appendData:receiveddata];
 }

 - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@did finish loading page);
self-finished = YES;
 }


 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError
 *)error
 {
NSLog([NSString stringByAppendingStrings:@Connection failed with error:
  , [error description], nil]);

Again with the format strings.

NSLog( @Connection failed with error: %@, error );

self-finished = YES;
 }

 - (NSCachedURLResponse *)connection:(NSURLConnection *)connection
 willCacheResponse:(NSCachedURLResponse *)cachedResponse
 {
NSLog(@Will cache response);
return cachedResponse;
 }

 - (void) dealloc {
if(self-url !=nil) {
[self-url release];
self-url = nil;
}
if(self-connection != nil) {
[self-connection release];
self-connection = nil;
}
[super dealloc];
 }

What about releasing the other instance variables you assign?
___

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: NSURLConnection unhappiness

2009-04-30 Thread Jeff Johnson

On Apr 30, 2009, at 12:53 AM, Kyle Sluder wrote:


On Thu, Apr 30, 2009 at 1:44 AM, Jeff Johnson
publicpost...@lapcatsoftware.com wrote:
On an unrelated note, your use of self-connection, etc., is non- 
standard

and not advised. You should be using direct ivar access connection,
properties self.connection, or accessor methods [self  
connection].


Sure, `self-connection` is redundant with just plain old
`connection`, but they amount to the same thing.  Unless there's some
new-runtime trickery going on that I'm not aware of.

--Kyle Sluder


It amounts to the same thing, but it's a bad habit to get into. It's  
redundant, as you say, for an object's own instance variables. And you  
shouldn't be trying to directly access the instance variables of other  
objects. According to the documentation, Marking instance variables  
@public defeats the ability of an object to hide its data. It runs  
counter to a fundamental principle of object-oriented programming—the  
encapsulation of data within objects where it’s protected from view  
and inadvertent error. Public instance variables should therefore be  
avoided except in extraordinary cases. Thus, except in extraordinary  
cases, there's no reason to use the object-ivar syntax.


-Jeff

___

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


NSURLConnection unhappiness

2009-04-29 Thread Nick Hristov
Hello,

I am currently working on a small iphone app, and I am facing some
difficulty with getting NSURLConnection to ... connect. Basically my unit
tests don't pass: connection is never made.

Here is a snippet of the non-working code:

code

- (id) initWithURL: (NSURL*) someurl
{
self = [super init];
if (self != nil) {
self-url = [someurl copy];
NSURLRequest * request = [NSURLRequest requestWithURL:self-url
cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 6.0];
NSLog([NSString stringByAppendingStrings:@Request:  , [request
description], nil]);
self-finished = NO;
self-connection = [[NSURLConnection alloc]
initWithRequest:requestdelegate:self startImmediately:NO];
}
return self;
}

- (void) downloadData
{
NSLog([self-connection description]);
NSLog(@run, b*$#4rd, run...);
[self-connection scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
[self-connection start];
int timeout = 30;
int i = 0;
while(self-finished == NO  [self _deletegateTerminateCheck] == NO) {
// suspend thread
[NSThread sleepForTimeInterval: 1.0];
i++;

/// stupid, but effective safety measure:
if(i = timeout) {
break;
}
}
}

- (NSData *) data {
return self-data;
}

#pragma mark NSConnectionDelegate implementation
- (NSURLRequest *)connection:(NSURLConnection *)connection
willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse
*)response
{
NSLog(@Will send request);
return request;
}


- (void)connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse *)response
{
NSLog(@Did get response);
NSString * encoding = [response textEncodingName];
self-dataEncoding = [self getEncodingForEncodingName: encoding];
self-mimeType = [response MIMEType];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData
*)receiveddata
{
NSLog(@Did get data);
if([self _deletegateTerminateCheck]) {
if(self-data != nil) {
[self-data release];
self-data = nil;
}
[self-connection cancel];
[self-connection release];
self-connection = nil;
return;
}
if(self-data == nil) {
self-data = [[NSMutableData alloc]init];
}
[self-data appendData:receiveddata];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@did finish loading page);
self-finished = YES;
}


- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError
*)error
{
NSLog([NSString stringByAppendingStrings:@Connection failed with error:
 , [error description], nil]);
self-finished = YES;
}

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
willCacheResponse:(NSCachedURLResponse *)cachedResponse
{
NSLog(@Will cache response);
return cachedResponse;
}

- (void) dealloc {
if(self-url !=nil) {
[self-url release];
self-url = nil;
}
if(self-connection != nil) {
[self-connection release];
self-connection = nil;
}
[super dealloc];
}
/code

Here is the output that I get from the build:

2009-04-25 14:14:22.699 otest[6135:80f] Request: NSURLRequest
http://arstechnica.com
2009-04-25 14:14:22.704 otest[6135:80f] NSURLConnection: 0x31d2a0,
http://arstechnica.com
2009-04-25 14:14:22.705 otest[6135:80f] run, b*$#4rd, run...
2009-04-25 14:14:22.706 otest[6135:80f] hello?
2009-04-25 14:14:52.709 otest[6135:80f] Data is nil!

Data is nil! is dumped by the

[NSString stringByAppendingStrings:  ] is my own extension and it works.

I also found
http://www.cocoabuilder.com/archive/message/cocoa/2009/3/31/233409 which
helped, without scheduling in the run loop things fail miserably.

Once again, this is all run inside a unit test. So I am not sure what else
may be missing from the run loop, looks like the URL connection never really
even starts the loading mechanism.

Thanks a bunch,

Nick
___

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: NSURLConnection unhappiness

2009-04-29 Thread Jeff Johnson

Hi Nick.

You can't sleep the thread. According to the documentation for - 
[NSThread sleepForTimeInterval:], No run loop processing occurs while  
the thread is blocked.. You need to run the run loop for the  
connection to process.


On an unrelated note, your use of self-connection, etc., is non- 
standard and not advised. You should be using direct ivar access  
connection, properties self.connection, or accessor methods [self  
connection].


-Jeff


On Apr 29, 2009, at 11:31 PM, Nick Hristov wrote:


- (void) downloadData
{
   NSLog([self-connection description]);
   NSLog(@run, b*$#4rd, run...);
   [self-connection scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
   [self-connection start];
   int timeout = 30;
   int i = 0;
   while(self-finished == NO  [self _deletegateTerminateCheck] ==  
NO) {

   // suspend thread
   [NSThread sleepForTimeInterval: 1.0];
   i++;

   /// stupid, but effective safety measure:
   if(i = timeout) {
   break;
   }
   }
}


___

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: NSURLConnection unhappiness

2009-04-29 Thread Kyle Sluder
On Thu, Apr 30, 2009 at 1:44 AM, Jeff Johnson
publicpost...@lapcatsoftware.com wrote:
 On an unrelated note, your use of self-connection, etc., is non-standard
 and not advised. You should be using direct ivar access connection,
 properties self.connection, or accessor methods [self connection].

Sure, `self-connection` is redundant with just plain old
`connection`, but they amount to the same thing.  Unless there's some
new-runtime trickery going on that I'm not aware of.

--Kyle Sluder
___

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