views:

951

answers:

2
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSString *theXML = [[NSString alloc] initWithBytes: [myWebData mutableBytes] length:[myWebData length] encoding:NSUTF8StringEncoding];
    NSLog(@"%@",theXML); 
    [self actualString:theXML 
      extractMyData:@"<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"&gt;&lt;soap:Body&gt;&lt;GetCategoryResponse xmlns=\"http://tempuri.org/\"&gt;&lt;GetCategoryResult&gt;"
      endingString:@"</GetCategoryResult></GetCategoryResponse></soap:Body></soap:Envelope>" 
      emptyString:@"<Prop_Category />"];
    [theXML writeToFile:[self GetMyFilePath] atomically:YES encoding:NSStringEncodingConversionAllowLossy error:nil];
    [theXML release];
}

-(NSArray*)actualString:(NSString*)theXML extractMyData:(NSString*)prefixString endingString:(NSString*)suffixString emptyString:(NSString*)noDataFromXMLString{
    // now here I want to extract data from string
    // from theXML
}

-(NSString*)GetMyFilePath{
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *documentDirectory=[paths objectAtIndex:0];
    NSString *pathToUserCopyofplist=[documentDirectory stringByAppendingPathComponent:@"myWebServiceData.plist"];
    NSLog(@"%@",pathToUserCopyofplist);
    return pathToUserCopyofplist;
}

I want to save to response of asp.net web service in a plist file.

But sometimes, when response may be in a huge size. In such situation connection has received data more then 50,000 bytes. & When I NSlogs NSString - it prints (null).

In this case I cann't store web service response to the file.

What should be solution for this? Is there any alternate way of NSString? (for this purpose)

Thanks in advance.

Sagar.

+4  A: 

If you want to store something, you already have the bytes: [myWebData mutableBytes]

Alex Reynolds
You can create a file with bytes, mutable or immutable. See http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/Reference/Reference.html#//apple_ref/doc/uid/20000172-CIAEAHFJ for one method to use.
Alex Reynolds
You could generate the byte equivalent of `<soap:Header>` and locate the associated range of bytes in the mutable data object. You could then replace those bytes with NULL: http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSMutableData_Class/Reference/NSMutableData.html#//apple_ref/doc/uid/20000173-BGBBIABA
Alex Reynolds
Sir, But, Here I am confused. Whatever web service has responded, I want to remove some part as given in the question. my Question is "is it possible with nsstring?" "is mutable data more preferable?" "how to replace it by using nsmutable data?"
sugar
This is where you should do some work on your own. Try working with smaller byte subsets to see if there is a memory constraint within your application, which might be causing problems converting the data to a string. Since you know what bytes you need to strip (the "soap" tags), try walking through the byte set and doing pattern matching on that, which might be a little more efficient. Or use a different XML parser to strip out those tags. You already have the data.
Alex Reynolds
Creating a 50 KB string should not be an issue, by the way. So the step where you have the bytes and are converting them to a UTF8 string would be a good place to start looking for problems.
Alex Reynolds
+1  A: 

It's a mobile device, remember. Memory is extremely limited and when you say that you might want to allocate more than 500000 bytes, it throws a red flag for me. This is going to be an issue for you with this design.

Consider coming up with a streaming or chunk algorithm if the file size is over n. Maybe your web service can break the response up into parts of a reasonable (and known maximum) size and the device and write them to the file as it receives them?

marcc
Sorry. But by mistake I added one more zero.
sugar
Ah. 50 K is getting better :)
marcc
Sir, I updated more details. Which clearly specifies why do I need data in nsstring format.
sugar