2015-05-11 27 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