2015-05-11 24 views
-1

我希望有人可以帮助,我在我的智慧结束试图弄清楚这一点!需要重新加载/重新计算NSArray

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

有没有人有任何线索如何做到这一点,我到处研究,找不到答案!

仅供参考这里是有条件的参数设置额外的字段/实体数据库:

// 
// 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 
+0

“代码格式化”用于代码,而不是强调。此外,您的问题是您希望其他人为您修改的代码转储,这使得它的质量很低。此外,仅仅说明你研究了你的问题并不符合研究的要求。 – dandan78

+0

嗨丹,感谢您的意见,但我担心你完全错了,我正试图在这里学习。再一次感谢你们低质量的批评,并且自负地假设/傲慢的输入(!) – user1419810

回答

1

你不应该存储开启状态。这应该是一个计算的属性。 创建在请求时返回此值的方法:

- (OpeningState)openingState { 
    OpeningState _openingState; 

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

    // Find out opening state of the restaurant 
    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; 
      } 
     } 
    return _openingState 
} 
+0

感谢你们,我不得不编辑一点,但肯定似乎做了这项工作,感谢指引我朝着正确的方向发展,重新传说! – user1419810