简体   繁体   中英

Add 1 item from xml to tableview per day

I want to take an already built xml, parse it, and add only the oldest item in the xml at first, and add 1 more each day. The code used to parse is:

- (void)parseRss:(GDataXMLElement *)rootElement entries:(NSMutableArray *)entries {

    NSArray *channels = [rootElement elementsForName:@"channel"];
    for (GDataXMLElement *channel in channels) {            

        NSString *blogTitle = [channel valueForChild:@"title"];                    

        NSArray *items = [channel elementsForName:@"item"];
        for (GDataXMLElement *item in items) {

            NSString *articleTitle = [item valueForChild:@"title"];
            NSString *articleUrl = [item valueForChild:@"guid"];            
            NSString *articleDateString = [item valueForChild:@"pubdate"];        
            NSDate *articleDate = [NSDate dateFromInternetDateTimeString:articleDateString formatHint:DateFormatHintRFC822];
            NSString *articleImage = [item valueForChild:@"description"];
            NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
            [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
            [dateFormatter setDateStyle:NSDateFormatterMediumStyle];

            RSSEntry *entry = [[[RSSEntry alloc] initWithBlogTitle:blogTitle
                                                      articleTitle:articleTitle
                                                        articleUrl:articleUrl
                                                       articleDate:articleDate
                                                      articleImage:articleImage
                                                              date:thedate] autorelease];
            [entries addObject:entry];

        }      
    }

}

How can I change this so it will only add the oldest item on the first launch of app, and so forth?

In the AppDelegate didFinishLaunchingWithOptions method I put in:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    if (![defaults integerForKey:@"totalDays"]) {

        // if there is no value for the key, set it to 0
        [defaults setInteger:0 forKey:@"totalDays"];
        [defaults synchronize];
    }
    if (![defaults objectForKey:@"currentDate"]) {
        [defaults setObject:@"32 01" forKey:@"currentDate"];
        //this will ensure that no one's start date ever matches current value
        [defaults synchronize];
    }
    // get the current date
    NSDate *date = [NSDate date];

    // format it
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
    [dateFormat setDateFormat:@"dd MM"];

    // convert it to a string
    NSString *dateString = [dateFormat stringFromDate:date];

    // free up memory
    [dateFormat release];
    if ([defaults objectForKey:@"currentDate"]){
    NSString *checkdate = [defaults objectForKey:@"currentDate"];
    if (![dateString isEqualToString:checkdate]) {
       //now if your current date is different from the last date it will add 1 to the total days key
        NSInteger currentnumber = [defaults integerForKey:@"totalDays"];
        NSLog(@"The current number is %i", currentnumber);

        [defaults setObject:dateString forKey:@"currentDate"];
        [defaults setInteger:currentnumber+1 forKey:@"totalDays"];
        [defaults synchronize];
        NSLog(@"The new number is %i", [defaults integerForKey:@"totalDays"]);
    }
    }

Then in the area for the NSPredicate I added:

NSDate *start = [NSDate dateFromRFC3339String:@"2013-01-01T00:00:00-06:00"];
            NSInteger difference = [[NSUserDefaults standardUserDefaults] integerForKey:@"totalDays"];

NSPredicate *predicate = [NSPredicate predicateWithFormat: @"articleDate < %@",  [NSDate dateWithTimeInterval:(86400*difference) sinceDate:start]];
            NSArray *filteredArray = [entries filteredArrayUsingPredicate: predicate];

This last bit of code hopefully will do what originally hoped for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM