需要重新加载/重新计算NSArray

时间:2015-05-11 04:06:12

标签: ios arrays database nsdictionary

我希望有人可以提供帮助,我最终想要解决这个问题!

基本上我有一个JSON提要将数据拉入NSDictionary,并附加一个文件(.h和.m),添加一些与当前时间相关的条件字段。我需要每隔x分钟更新/刷新条件字段,而无需重新加载/从JSON提要中提取数据。相反,我想重新运行条件测试并使用最初创建的数据库更新数据库。

有没有人知道怎么做,我到处研究,找不到答案!!

这里的FYI是在数据库中设置额外字段/实体的条件参数:

//
//  Entity.m
//  tabbed
//
//  Created by administrator on 16.07.12.
//  Copyright (c) 2012 __HappyTimes__. All rights reserved.
//

#import "RestarauntEntity.h"
#import "NSDate+DeviceDate.h"

@implementation RestarauntEntity

@synthesize city = _city;
@synthesize closingTime = _closingTime;
@synthesize description = _description;
@synthesize lattitude = _lattitude;
@synthesize longditude = _longditude;
@synthesize openingState = _openingState;
@synthesize openingTime = _openingTime;
@synthesize phoneNumber = _phoneNumber;
@synthesize thumbnailUrl = _thumbnailUrl;
@synthesize title = _title;
@synthesize website = _website;

- (id)initWithDictionary:(NSMutableDictionary *) dictionary 
{
    self = [super init];
    if (self) {
        _title = [dictionary objectForKey:@"title"];
        _description = [dictionary objectForKey:@"content"]; 
        _thumbnailUrl = [dictionary objectForKey:@"thumbnail"];
        _city = [[[dictionary objectForKey:@"custom_fields"] objectForKey:@"City"] objectAtIndex:0];
        _phoneNumber = [[[dictionary objectForKey:@"custom_fields"] objectForKey:@"Phone Number"] objectAtIndex:0];
        _website = [[[dictionary objectForKey:@"custom_fields"] objectForKey:@"Address"] objectAtIndex:0];


        // get opening/closing time values
        NSString *currentWeekDayName = [NSDate currentDayOfTheWeekInEnglish];        
        NSString *openingTimeForCurentDay = [NSString stringWithFormat:@"%@ Opening Time", currentWeekDayName];
        NSString *closingTimeForCurentDay = [NSString stringWithFormat:@"%@ Closing Time", currentWeekDayName];

        _openingTime = [[[dictionary objectForKey:@"custom_fields"] objectForKey:openingTimeForCurentDay] objectAtIndex:0];
        _closingTime = [[[dictionary objectForKey:@"custom_fields"] objectForKey:closingTimeForCurentDay] objectAtIndex:0];

        // Prepare current day string in format yyyy-MM-dd
        NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
        [timeFormat setDateFormat:@"yyyy-MM-dd "];
        [timeFormat setPMSymbol:@"pm"];
        [timeFormat setAMSymbol:@"am"];

        NSDate *now = [[NSDate alloc] init];
        NSString *currentDay = [timeFormat stringFromDate:now];

        // Find full date for opening and close time
        [timeFormat setDateFormat:@"yyyy-MM-dd hh:mm a"];

        NSDate *openingTimeDate = [timeFormat dateFromString:[currentDay stringByAppendingString:_openingTime]];
        NSDate *closingTimeDate = [timeFormat dateFromString:[currentDay stringByAppendingString:_closingTime]];

        // Find out opening state of the restaraunt
        if(([openingTimeDate compare:now] == NSOrderedAscending) || ([openingTimeDate compare:now] == NSOrderedSame)) {
            if([closingTimeDate compare:now] == NSOrderedDescending)
            {
                _openingState = RestarauntOpeningStateOpen;
            } else {
                _openingState = RestarauntOpeningStateClosed; 
            }
        } else {
            NSDate *nowPlusTwoHours = [now dateByAddingTimeInterval:7200.0];
            if(([openingTimeDate compare:nowPlusTwoHours] == NSOrderedAscending) || ([openingTimeDate compare:nowPlusTwoHours] == NSOrderedSame))
            {
                _openingState = RestarauntOpeningStateWillBeOpenedInTwoHours;
            } else {
                _openingState = RestarauntOpeningStateClosed;
            }
        }

   //     _openingState = RestarauntOpeningStateWillBeOpenedInTwoHours;

        NSArray *lattitudeRecord = [[dictionary objectForKey:@"custom_fields"] objectForKey:@"Lattitude"]; 
        _lattitude = [[lattitudeRecord objectAtIndex:0] doubleValue];

        NSArray *longditudeRecord = [[dictionary objectForKey:@"custom_fields"] objectForKey:@"Longditude"]; 
        _longditude = [[longditudeRecord objectAtIndex:0] doubleValue];
    }

    //[setValue:@"RestarauntOpeningStateOpen" forKey:@"_openingState"];

  //  _openingState setValue:@"RestarauntOpeningStateOpen" forKeyPath:RestarauntOpeningState;

    return self;
}

//-(void)setValue:(id)value forKeyPath:(NSString *)keyPath{
//    _openingState = RestarauntOpeningStateOpen;
//}

//-(void)setValue:(id)value forKeyPath:(NSString *)keyPath{
//    _openingState = RestarauntOpeningStateOpen;
//}

@end

1 个答案:

答案 0 :(得分:1)

您不应存储开放状态。这应该是计算属性。 Create request在请求时返回此值的方法:

@RequestMapping(value = "/add", method = RequestMethod.POST, produces = "application/xml")
public @ResponseBody Catalog addCatalog(@ModelAttribute("catalog") Catalog catalog) {
    logger.info("In addCatalog with " + catalog);
    catalog.getHeader().setOrganizationId(IConstant.ORGANIZATION_ID_BSL);
    try {
        catalog = catalogService.addCatalogIntegration(catalog);
    } catch (Exception e) {
        e.printStackTrace();
    }
    logger.info("Returning from addCatalog with " + catalog);
    return catalog;
}

@ModelAttribute("catalog")
private Catalog getCatalog(HttpServletRequest request) throws Exception {
    Catalog catalog = new Catalog();
    try {
        String xml = (String) request.getAttribute("content");
        if (xml != null) {
            JAXBContext jaxbContext = JAXBContext.newInstance(Catalog.class);
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            catalog = (Catalog) unmarshaller.unmarshal(new StringReader(xml));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return catalog;
}