views:

317

answers:

5

Hi, i have developed an applikation which loads a specified url with an XML Parser. This URL changes sometimes, and every 4 seconds it reloads.(via NSTIMER)

On the simulator it works perfectly but on the device it is always the same ( like a kind of caching)

-(void)parseXMLFileAtURL:(NSString *) URL{
    NSLog(@"Parsed XML URL: %@", URL);
    currentURL=[URL copy];;
    tutorials= [[NSMutableArray alloc] init];
    settings= [[NSMutableArray alloc] init];
    NSURL *xmlURL=[NSURL URLWithString:URL];
    xmlParser =[[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
    [xmlParser setDelegate:self];
    [xmlParser parse];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict{
currentElement= [elementName copy];
    if([elementName isEqualToString:@"item"]){
       item=[[NSMutableDictionary alloc] init];
     currentTitle=[[NSMutableString alloc] init];
     currentImage=[[NSMutableString alloc] init];
     currentLink=[[NSMutableString alloc] init];
     currentDetails=[[NSMutableString alloc] init];
     }
    if([elementName isEqualToString:@"setting"]){
     setting=[[NSMutableDictionary alloc] init];
     currentTitle=[[NSMutableString alloc] init];
     currentTyp=[[NSMutableString alloc] init];
     currentLink=[[NSMutableString alloc] init];
    }

}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if([elementName isEqualToString:@"item"]){
    NSLog(@"currenttitle: %@",currentTitle );
     [item setObject:currentTitle forKey:@"title"];
     [item setObject:currentImage forKey:@"image"];  
     [item setObject:currentLink forKey:@"link"];
     [item setObject:currentDetails forKey:@"details"];
     [tutorials addObject:[item copy]];
    }
    if([elementName isEqualToString:@"setting"]){
     NSLog(@"Currentsettingtitle: %@",currentTitle );
     [setting setObject:currentTitle forKey:@"title"];
     [setting setObject:currentTyp forKey:@"typ"];  
     [setting setObject:currentLink forKey:@"link"];
     [settings addObject:[setting copy]];
    }
}


}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if([currentElement isEqualToString:@"title"]){
     [currentTitle appendString:string];
    }
    if([currentElement isEqualToString:@"image"]){
     [currentImage appendString:string];
    } 
    if([currentElement isEqualToString:@"link"]){
     [currentLink appendString:string];
    }

    if([currentElement isEqualToString:@"typ"]){
     [currentTyp appendString:string];
    }
    if([currentElement isEqualToString:@"details"]){
     [currentDetails appendString:string];
    }

}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
    NSLog(@"Fertig mit Document"); 
    for (int i=0; i<[settings count]; i++) {
     NSString *typ =(NSString*)[[settings objectAtIndex:i ] objectForKey:@"typ"];
     typ=[typ stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
     if([typ isEqualToString:@"Button1"])
      button1.title=(NSString*)[[settings objectAtIndex:i ] objectForKey:@"title"];
     if([typ isEqualToString:@"Button2"])
      button2.title=(NSString*)[[settings objectAtIndex:i ] objectForKey:@"title"];
     if([typ isEqualToString:@"Button3"])
      button3.title=(NSString*)[[settings objectAtIndex:i ] objectForKey:@"title"];
     if([typ isEqualToString:@"Titel"])
     navitem.title=(NSString*)[[settings objectAtIndex:i ] objectForKey:@"title"];
    }
    //[[tutorials ] objectForKey:@"title"]
    [myTableView reloadData];
}

Anybody an Idea?

A: 

Thats a handful of code (can't avoid with xml parsing)

But, it seems from your question that there is no problem with xml parsing.

So instead, focus on the Timer related code? Perhaps add some logs when it actually gets a new URL?? Just to be sure you are getting a new request???

Prakash
+2  A: 

Hi,

You should try to init your parser with data.

And get your data with NSURLRequest and NSURLConnection :

NSURL *xmlURL=[NSURL URLWithString:URL];
NSURLRequest *request = [NSURLRequest requestWithURL:xmlURL cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:0.0f];
xmlParser =[[NSXMLParser alloc] initWithData:[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//etc.
F.Santoni
A: 

Ok i am going to try that. Thanks in advance

Ploetzeneder
A: 

Ok, this also does NOT work.

-(IBAction)goback:(id)sender

{ NSURL *xmlURL=[NSURL URLWithString:@"http://demo.komexa.com/sicherungsbereich.xml"]; NSURLRequest *request = [NSURLRequest requestWithURL:xmlURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:2]; NSURLResponse *theResponse; NSError *theError; NSData *myRequestResult = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&theError]; NSString *stringReply = (NSString *)[[NSString alloc] initWithData:myRequestResult encoding:NSUTF8StringEncoding]; NSLog(@"reply from server: %@", stringReply); }

On the simulator every time when the function is called, the url is loaded new. But the Iphone devices has refreshing problems. (The Content on the serverchanges sometimes and it caches)

I can also see it on the NSLog that the String content has not changed,even if the content of the url changes. But on the Simulator it works perfectly.

What can be the reason? If you also want to change the content of the sicherungsbereich.xml just call:

http://demo.komexa.com/

Ploetzeneder
A: 

This did also not work. Any idea?

Ploetzeneder