0

in my Program, I have a NSMutableData variable that collect the information from http://www.nhara.org/scored_races-2013.htm. After about the third time it gets information from a website, when it contains 90810 bytes, it either disappears or becomes null because if I print it a NSString, it is null. Here is the code

- (void)viewWillAppear:(BOOL)animated
{
    // Create a new data container for the stuff that comes back from the service
    xmlData = [[NSMutableData alloc] initWithCapacity:180000];

    [self fetchEntries];
    [super viewWillAppear:animated];
}
- (void)fetchEntries
{
        // Construct a URL that will ask the service for what you want 
    NSURL *url = [NSURL URLWithString: @"http://www.nhara.org/scored_races-2013.htm"];//

    // Put that URL into an NSURLRequest
    NSURLRequest *req = [NSURLRequest requestWithURL:url];

    // Create a connection that will exchange this request for data from the URL 
    connection = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES];
}

- (void)connection:(NSURLConnection *)conn didReceiveData:(NSData *)data 
{
    // Add the incoming chunk of data to the container we are keeping 
    // The data always comes in the correct order 
    [xmlData appendData:data];

    NSLog(@"%@",xmlData);
    NSString *xmlCheck = [[[NSString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding]autorelease];
    NSLog(@"xmlCheck = %@", xmlCheck);

}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"error= %@",error);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)conn {

    // We are just checking to make sure we are getting the XML 
    NSString *xmlCheck = [[[NSString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding] autorelease];
    NSLog(@"xmlCheck2 = %@", xmlCheck);

}

What confuses me the most is that my NSMutableData stores data, but then loses it while claiming to have the same number of bytes.

Is there a constraint to the NSMutableData's size or is my problem just memory management?

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Guessing you're using ARC. Your variable is getting dealloc'd. Declare it as a @property with a strong atribute. – Rog Dec 07 '12 at 20:55
  • @Rog He's not using ARC - see the call to `autorelease`? – rmaddy Dec 07 '12 at 20:56
  • Where are you seeing the problem? In the `didReceiveData` method or the `didFinishLoading` method? Converting partial data (in `didReceiveData`) could result in a `nil` string just because at that moment the partial data isn't a valid UTF8 string. – rmaddy Dec 07 '12 at 20:58
  • the problem is in didReceiveData – user1883873 Dec 07 '12 at 22:41

1 Answers1

1

You need to create a property for your xmlData variable. In your header file after your @interface MyClass, make one like so

@property (nonatomic, retain) NSMutableData * xmlData;

If you are using ARC you leave it as strong if you using below ARC you change strong to retain. When you want to use your variable you do self.xmlData

Yuliani Noriega
  • 1,085
  • 12
  • 23
  • Why do you think a property is necessary? It's not. Besides, if this was the problem, why would this work the first couple of times? – rmaddy Dec 07 '12 at 21:00
  • Correct me if I am wrong, but I was under the impression that variables set in a method can't not be used outside of the scope of a method. He is clearly setting `xmlData` as a variable in `viewWillAppear`. How will he be able to access that variable in a other method if you don't create a property for it? – Yuliani Noriega Dec 07 '12 at 21:04
  • No, `xmlData` is clearly NOT a local variable to `viewWillAppear:` since there is no declaration of the variable in `viewWillAppear:`. Therefore, it must be an instance variable (or a global but not likely). – rmaddy Dec 07 '12 at 21:05
  • Forgot to menton that xmlData is an instance variable – user1883873 Dec 07 '12 at 22:18