SIGABRT (unrecognized selector) after trying to write NSData from NSURLConnection to file


SOLVED: the file name was an auto-implemented string, not available when called in createFileAtPath:


I'm trying to track the progress of a file upload, and the code I'm trying to implement is this (edited) :

connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self]; if (connection) receivedData = [[NSMutableData data] retain]; -(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { [receivedData setLength:0]; totalBytes = [[NSNumber numberWithLongLong:[response expectedContentLength]] intValue]; NSLog(@"content-length: %i bytes", totalBytes); } -(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [receivedData appendData:data]; int resourceLength = [[NSNumber numberWithUnsignedInteger:[receivedData length]] intValue]; NSLog(@"receivedData length: %i", resourceLength); } -(void) connectionDidFinishLoading:(NSURLConnection *)connection { [fileMgr createFileAtPath:filename contents:receivedData attributes:nil]; //if instead i write only the Apple example: //NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]); //there no SIGABRT [receivedData release]; [connection release]; } 

but the createFileAtPath: contents: attribute only gives SIGABRT:

- [__ NSCFData getFileSystemRepresentation: MAXLENGTH]: unrecognized selector sent to instance 0x2ba510

* Application termination due to the uncaught exception "NSInvalidArgumentException", reason: "- [__ NSCFData getFileSystemRepresentation: MAXLENGTH]: unrecognized selector sent to instance 0x2ba510

what am I doing wrong? is it possible to download the file asynchronously?

2 more things: a) NSBS content length is correct. b) if I’m not initWithCapacity: the length of the content in didReceiveResponse and just init, the length of the resulting length increases approximately two times the length of the content ...

+4
source share
2 answers

SOLVED: the file name was an auto-implemented string, not available when called in createFileAtPath:

+2
source

I can’t say that this is better than Apple docs , so I won’t try. Follow this guide and you must be addressed.

As a side note, there are several memory problems on this line.

 self.receivedData = [[NSMutableData alloc] initWithCapacity:[response expectedContentLength]]; 

You create an NSMutableData object with a hold of count +1 and consider that you have the corresponding getters / setters created with @property (nonatomic, retain) NSMutableData *recievedData , which will add an extra +1.

To solve this problem, one of these two will work (a non-automatic version works best for iphone).

 NSMutableData *recievedData = [[NSMutableData alloc] initWithCapacity:[response expectedContentLength]]; self.recievedData = recievedData; [recievedData release]; recievedData= nil; // or the autoreleased version self.recievedData = [NSMutableData dataWithCapacity:[response expectedContentLength]]; 

In addition, I would call

 self.recievedData = nil; 

instead

 [self.recievedData release]; 

It seems a bit safer and you are using a setter, which is good practice, to get in.

+1
source

All Articles