سؤال

The Problem: This process worked perfectly fine with iOS6. Now it does not pull the latest information and replaces the data displayed. Therefor the application information is always wrong. If I reset the contents on the simulator it works for that current period of time. After which I will need to reset the content again. Any suggestions. I have included the 3 files that handle all my data processing for reference.

Reader.M

- (void) startRequest
{
    NSURL *url = [[NSURL alloc] initWithString:@"http://www.something.com/app/rss/"];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:60.0f];
    NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:YES];
    if(conn)
    {
        _receivedData = [[NSMutableData alloc]init];
    }
}

#pragma mark - NSURLConnectionDataDelegate

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

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSLog(@"received %d bytes",data.length);
    [_receivedData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:_receivedData];
    JSTRRSSParser *rssParser = [[JSTRRSSParser alloc] init];
    parser.delegate = rssParser;
    [parser parse];

    NSMutableArray *articleList = [rssParser getParsedList];

    NSLog(@"%@", articleList);
    NSString *fileName = [NSString stringWithFormat:@"%@/popular.rss", [JSTRRSSUtil getDocumentRoot]];
    [articleList writeToFile:fileName atomically:YES];
}
@end

Parser.m

#import "JSTRRSSParser.h"

@implementation JSTRRSSParser

- (id) init
{
    self = [super init];
    if (self) {
        // custom initializer
        _list = [[NSMutableArray alloc] init];
    }
    return self;
}

- (NSMutableArray *)getParsedList
{
    return _list;
}

#pragma mark - implement NSXMLParserDelegate

- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
    if ([elementName isEqualToString:@"item"]) {
        _item = [[NSMutableDictionary alloc]init];
        [_item setValue:[[NSMutableArray alloc] init] forKey:@"category"];
        [_list addObject:_item];
        _foundItem = YES;
    }

    if([elementName isEqualToString:@"image"] ||
       [elementName isEqualToString:@"slider"] ||
       [elementName isEqualToString:@"title"] ||
       [elementName isEqualToString:@"link"] ||
       [elementName isEqualToString:@"description"] ||
       [elementName isEqualToString:@"category"] ||
       [elementName isEqualToString:@"sliderphoto"] ||
       [elementName isEqualToString:@"linkId"]||
       [elementName isEqualToString:@"pubDate"]||
       [elementName isEqualToString:@"sliderpriority"]||
       [elementName isEqualToString:@"pageorder"]||
       [elementName isEqualToString:@"sectionalphoto"]) {
            _tempStore = [[NSMutableString alloc] init];
        }
    }

- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    if (_foundItem) {
        [_tempStore appendString:string];
    }
}

- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    if (_foundItem) {
        if([elementName isEqualToString:@"item"]) {
            _foundItem = NO;
            return; // don't preceed
        }

        // trim whitespace from left and right and replace commonly encoded html string
        NSString *content = [_tempStore stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];
        content = [content stringByReplacingOccurrencesOfString:@" " withString:@""];
        content = [content stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
        content = [content stringByReplacingOccurrencesOfString:@""" withString:@"\""];
        content = [content stringByReplacingOccurrencesOfString:@"­" withString:@"-"];
        content = [content stringByReplacingOccurrencesOfString:@"»" withString:@">>"];
        content = [content stringByReplacingOccurrencesOfString:@"·" withString:@"-"];
        content = [content stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
        content = [content stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];
        content = [content stringByReplacingOccurrencesOfString:@"<br /> <br />" withString:@"\n"];
        content = [content stringByReplacingOccurrencesOfString:@"&mdash;" withString:@"-"];
        content = [content stringByReplacingOccurrencesOfString:@"&rsquo;" withString:@"'"];
        content = [content stringByReplacingOccurrencesOfString:@"&ldquo;" withString:@"'"];
        content = [content stringByReplacingOccurrencesOfString:@"&lsquo;" withString:@"'"];
        content = [content stringByReplacingOccurrencesOfString:@"&ndash;" withString:@"-"];



        if( [elementName isEqualToString:@"slider"] ) {
            [_item setObject:content forKey:@"slider"];
        } else if( [elementName isEqualToString:@"image"] ) {
            [_item setObject:content forKey:@"image"];
        } else if( [elementName isEqualToString:@"title"] ) {
            [_item setObject:content forKey:@"title"];
        } else if ( [elementName isEqualToString:@"link"]) {
            [_item setObject:content forKey:@"link"];
        } else if ( [elementName isEqualToString:@"description"]) {
            [_item setObject:content forKey:@"description"];
        } else if ( [elementName isEqualToString:@"category"]) {
            NSMutableArray *temp = (NSMutableArray *)[_item objectForKey:@"category"];
            [temp addObject:content];
        } else if ( [elementName isEqualToString:@"sliderphoto"]) {
            [_item setObject:content forKey:@"sliderphoto"];
        } else if ( [elementName isEqualToString:@"linkId"]) {
            [_item setObject:content forKey:@"linkId"];
        } else if ( [elementName isEqualToString:@"pubDate"]) {
            [_item setObject:content forKey:@"pubDate"];
        } else if ( [elementName isEqualToString:@"sliderpriority"]) {
            [_item setObject:content forKey:@"sliderprioroty"];
        } else if ( [elementName isEqualToString:@"sectionalphoto"]) {
            [_item setObject:content forKey:@"sectionalphoto"];
        } else if ( [elementName isEqualToString:@"pageorder"]) {
            [_item setObject:content forKey:@"pageorder"];
        }
    }
}

UTIL.h

+ (NSString *) getDocumentRoot
{
    // search for available path and return first
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return [paths objectAtIndex:0];
}

Further to this, If the content is not reset then even the reader does not pull the correct information from the RSS Feed.

هل كانت مفيدة؟

المحلول

You are using a cachePolicy of NSURLCacheStorageNotAllowed, which is a NSURLCacheStoragePolicy value. But you really want to use one of the NSURLRequestCachePolicy values, such as NSURLRequestReloadIgnoringLocalAndRemoteCacheData or NSURLRequestReloadIgnoringLocalCacheData.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top